From e4b706e041c659dbf31dace209eaf97b9472b92b Mon Sep 17 00:00:00 2001 From: Wes Moberly <wesm87@gmail.com> Date: Thu, 4 Feb 2016 23:50:45 -0500 Subject: [PATCH 1/3] Add whitelisting comment for tax query Fixes #479 --- .../ArrayAssignmentRestrictionsSniff.php | 18 ++++++++--------- WordPress/Sniffs/VIP/SlowDBQuerySniff.php | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php b/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php index ded6018dd8..ec4843a325 100644 --- a/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php +++ b/WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php @@ -6,15 +6,15 @@ * @package PHP_CodeSniffer * @author Shady Sharaf <shady@x-team.com> */ -class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff implements PHP_CodeSniffer_Sniff +class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff extends WordPress_Sniff { /** * Exclude groups * * Example: 'foo,bar' - * - * @var string Comma-delimited group list + * + * @var string Comma-delimited group list */ public $exclude = ''; @@ -22,7 +22,7 @@ class WordPress_Sniffs_Arrays_ArrayAssignmentRestrictionsSniff implements PHP_Co * Groups of variable data to check against. * Don't use this in extended classes, override getGroups() instead. * This is only used for Unit tests. - * + * * @var array */ public static $groups = array(); @@ -132,7 +132,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) foreach ( $groups as $groupName => $group ) { - + if ( in_array( $groupName, $exclude ) ) { continue; } @@ -165,9 +165,9 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) call_user_func( $addWhat, - $message, - $stackPtr, - $groupName, + $message, + $stackPtr, + $groupName, array( $key, $val ) ); } @@ -183,7 +183,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) /** * Callback to process each confirmed key, to check value * This must be extended to add the logic to check assignment value - * + * * @param string $key Array index / key * @param mixed $val Assigned value * @param int $line Token line diff --git a/WordPress/Sniffs/VIP/SlowDBQuerySniff.php b/WordPress/Sniffs/VIP/SlowDBQuerySniff.php index 3912301748..06eaf46058 100644 --- a/WordPress/Sniffs/VIP/SlowDBQuerySniff.php +++ b/WordPress/Sniffs/VIP/SlowDBQuerySniff.php @@ -39,4 +39,24 @@ public function getGroups() { ) ); } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token + * in the stack passed in $tokens. + * + * @return void + */ + public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + + $this->init( $phpcsFile ); + + if ( $this->has_whitelist_comment( 'tax_query', $stackPtr ) ) { + return; + } + + parent::process( $phpcsFile, $stackPtr ); + } }//end class From 20df378acf65304fe3c89fd5c702c64980fb2fb8 Mon Sep 17 00:00:00 2001 From: Wes Moberly <wesm87@gmail.com> Date: Fri, 5 Feb 2016 14:08:28 -0500 Subject: [PATCH 2/3] Updated unit test to check whitelisting comment. --- WordPress/Tests/VIP/SlowDBQueryUnitTest.inc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/WordPress/Tests/VIP/SlowDBQueryUnitTest.inc b/WordPress/Tests/VIP/SlowDBQueryUnitTest.inc index 3d0857780f..dda39302cc 100644 --- a/WordPress/Tests/VIP/SlowDBQueryUnitTest.inc +++ b/WordPress/Tests/VIP/SlowDBQueryUnitTest.inc @@ -21,3 +21,20 @@ $query = 'foo=bar&meta_key=foo&meta_value=bar'; if ( ! isset( $widget['params'][0] ) ) { $widget['params'][0] = array(); } + + +// Testing whitelisting comments. +$test = array( + + // Single-line statements. + 'tax_query' => array(), // Bad. + 'tax_query' => array(), // WPCS: tax_query ok. + + // Multi-line statement. + 'tax_query' => array( // WPCS: tax_query ok. + array( + 'taxonomy' => 'foo', + ), + ), + ), +); From b44d7b3cef8c8c7cbb4817cde7d6fed98c504814 Mon Sep 17 00:00:00 2001 From: Wes Moberly <wesm87@gmail.com> Date: Fri, 5 Feb 2016 14:17:51 -0500 Subject: [PATCH 3/3] Updated unit test with new expected warning line --- WordPress/Tests/VIP/SlowDBQueryUnitTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/WordPress/Tests/VIP/SlowDBQueryUnitTest.php b/WordPress/Tests/VIP/SlowDBQueryUnitTest.php index 74b5755dd2..651640d6b7 100644 --- a/WordPress/Tests/VIP/SlowDBQueryUnitTest.php +++ b/WordPress/Tests/VIP/SlowDBQueryUnitTest.php @@ -42,6 +42,7 @@ public function getWarningList() 15 => 1, 16 => 1, 19 => 2, + 30 => 1, ); }//end getWarningList()