Skip to content

Commit

Permalink
WPQueryParams: defer to the parent sniff
Browse files Browse the repository at this point in the history
This changes over the existing keys being sniffed for by this sniff to use the logic from the new `AbstractArrayAssignmentRestrictionsSniff` parent instead of custom logic.

Note: this is a BC-break as the error codes for the existing checks change!
* `WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn` is now `WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in`
* `WordPressVIPMinimum.Performance.WPQueryParams.SuppressFiltersTrue` is now `WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters`
  • Loading branch information
jrfnl committed Oct 19, 2020
1 parent 1c9cfca commit 3e009d0
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
namespace WordPressVIPMinimum\Sniffs\Performance;

use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
use PHP_CodeSniffer\Util\Tokens;

/**
* Flag suspicious WP_Query and get_posts params.
Expand All @@ -18,20 +17,6 @@
*/
class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff {

/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register() {
$targets = parent::register();

// Add the target for the "old" implementation.
$targets[] = T_CONSTANT_ENCAPSED_STRING;

return $targets;
}

/**
* Groups of variables to restrict.
* This should be overridden in extending classes.
Expand All @@ -49,6 +34,23 @@ public function register() {
*/
public function getGroups() {
return [
// WordPress.com: https://lobby.vip.wordpress.com/wordpress-com-documentation/uncached-functions/.
// VIP Go: https://wpvip.com/documentation/vip-go/uncached-functions/.
'SuppressFilters' => [
'type' => 'error',
'message' => 'Setting `suppress_filters` to `true` is prohibited.',
'keys' => [
'suppress_filters',
],
'callback' => [ $this, 'value_is_true' ],
],
'PostNotIn' => [
'type' => 'warning',
'message' => 'Using `post__not_in` should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.',
'keys' => [
'post__not_in',
],
],
'exclude' => [
'type' => 'warning',
'message' => 'Using `exclude`, which is subsequently used by `post__not_in`, should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.',
Expand All @@ -60,32 +62,18 @@ public function getGroups() {
}

/**
* Process this test when one of its tokens is encountered
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* Callback to process each confirmed key, to check value.
* This must be extended to add the logic to check assignment value.
*
* @return void
* @param string $key Array index / key.
* @param mixed $val Assigned value.
* @param int $line Token line.
* @param array $group Group definition.
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
* with custom error message passed to ->process().
*/
public function process_token( $stackPtr ) {

if ( trim( $this->tokens[ $stackPtr ]['content'], '\'' ) === 'suppress_filters' ) {

$next_token = $this->phpcsFile->findNext( array_merge( Tokens::$emptyTokens, [ T_EQUAL, T_CLOSE_SQUARE_BRACKET, T_DOUBLE_ARROW ] ), $stackPtr + 1, null, true );

if ( $this->tokens[ $next_token ]['code'] === T_TRUE ) {
// WordPress.com: https://lobby.vip.wordpress.com/wordpress-com-documentation/uncached-functions/.
// VIP Go: https://wpvip.com/documentation/vip-go/uncached-functions/.
$message = 'Setting `suppress_filters` to `true` is prohibited.';
$this->phpcsFile->addError( $message, $stackPtr, 'SuppressFiltersTrue' );
}
}

if ( trim( $this->tokens[ $stackPtr ]['content'], '\'' ) === 'post__not_in' ) {
$message = 'Using `post__not_in` should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.';
$this->phpcsFile->addWarning( $message, $stackPtr, 'PostNotIn' );
}

parent::process_token( $stackPtr );
public function value_is_true( $key, $val, $line, $group ) {
return ( $val === 'true' );
}

/**
Expand Down

0 comments on commit 3e009d0

Please sign in to comment.