diff --git a/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php b/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php index 949e3f5b..3a13ea44 100644 --- a/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php @@ -8,7 +8,7 @@ namespace WordPressVIPMinimum\Sniffs\Performance; -use WordPressVIPMinimum\Sniffs\Sniff; +use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff; use PHP_CodeSniffer\Util\Tokens; /** @@ -16,7 +16,7 @@ * * @package VIPCS\WordPressVIPMinimum */ -class WPQueryParamsSniff extends Sniff { +class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff { /** * Returns an array of tokens this test wants to listen for. @@ -24,8 +24,38 @@ class WPQueryParamsSniff extends Sniff { * @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. + * + * Example: groups => array( + * 'groupname' => array( + * 'type' => 'error' | 'warning', + * 'message' => 'Dont use this one please!', + * 'keys' => array( 'key1', 'another_key' ), + * 'callback' => array( 'class', 'method' ), // Optional. + * ) + * ) + * + * @return array + */ + public function getGroups() { return [ - T_CONSTANT_ENCAPSED_STRING, + 'PostNotIn' => [ + '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.', + 'keys' => [ + 'exclude', + ], + ], ]; } @@ -54,6 +84,22 @@ public function process_token( $stackPtr ) { $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 ); + } + + /** + * Callback to process a confirmed key which doesn't need custom logic, but should always error. + * + * @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 callback( $key, $val, $line, $group ) { + return true; } } diff --git a/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.inc b/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.inc index 51590746..2aecfee5 100644 --- a/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.inc @@ -16,4 +16,8 @@ $q = new WP_Query( $query_args ); $query_args[ 'suppress_filters' ] = true; -$q = new WP_query( $query_args ); \ No newline at end of file +$q = new WP_query( $query_args ); + +get_posts( [ 'exclude' => $post_ids ] ); // Warning. + +$exclude = [ 1, 2, 3 ]; diff --git a/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.php b/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.php index 1ccbfc4c..7d09c201 100644 --- a/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.php +++ b/WordPressVIPMinimum/Tests/Performance/WPQueryParamsUnitTest.php @@ -39,6 +39,7 @@ public function getWarningList() { return [ 4 => 1, 11 => 1, + 21 => 1, ]; }