Skip to content

Commit

Permalink
Merge pull request #524 from wesm87/479-tax-query-whitelisting-comment
Browse files Browse the repository at this point in the history
Add whitelisting comment for tax query
  • Loading branch information
westonruter committed Feb 5, 2016
2 parents cabd7cc + b44d7b3 commit 2a44d8c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
18 changes: 9 additions & 9 deletions WordPress/Sniffs/Arrays/ArrayAssignmentRestrictionsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
* @package PHP_CodeSniffer
* @author Shady Sharaf <[email protected]>
*/
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 = '';

/**
* 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();
Expand Down Expand Up @@ -132,7 +132,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr )


foreach ( $groups as $groupName => $group ) {

if ( in_array( $groupName, $exclude ) ) {
continue;
}
Expand Down Expand Up @@ -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 )
);
}
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions WordPress/Sniffs/VIP/SlowDBQuerySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions WordPress/Tests/VIP/SlowDBQueryUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
),
),
);
1 change: 1 addition & 0 deletions WordPress/Tests/VIP/SlowDBQueryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getWarningList()
15 => 1,
16 => 1,
19 => 2,
30 => 1,
);

}//end getWarningList()
Expand Down

0 comments on commit 2a44d8c

Please sign in to comment.