diff --git a/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php b/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php index 8d05d5f8..ac0c3fae 100644 --- a/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php @@ -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. @@ -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. @@ -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.', @@ -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' ); } /**