|
8 | 8 |
|
9 | 9 | namespace WordPressVIPMinimum\Sniffs\Performance; |
10 | 10 |
|
11 | | -use WordPressVIPMinimum\Sniffs\Sniff; |
| 11 | +use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff; |
12 | 12 | use PHP_CodeSniffer\Util\Tokens; |
13 | 13 |
|
14 | 14 | /** |
15 | 15 | * Flag suspicious WP_Query and get_posts params. |
16 | 16 | * |
17 | 17 | * @package VIPCS\WordPressVIPMinimum |
18 | 18 | */ |
19 | | -class WPQueryParamsSniff extends Sniff { |
| 19 | +class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff { |
20 | 20 |
|
21 | 21 | /** |
22 | 22 | * Returns an array of tokens this test wants to listen for. |
23 | 23 | * |
24 | 24 | * @return array |
25 | 25 | */ |
26 | 26 | public function register() { |
| 27 | + $targets = parent::register(); |
| 28 | + |
| 29 | + // Add the target for the "old" implementation. |
| 30 | + $targets[] = T_CONSTANT_ENCAPSED_STRING; |
| 31 | + |
| 32 | + return $targets; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Groups of variables to restrict. |
| 37 | + * This should be overridden in extending classes. |
| 38 | + * |
| 39 | + * Example: groups => array( |
| 40 | + * 'groupname' => array( |
| 41 | + * 'type' => 'error' | 'warning', |
| 42 | + * 'message' => 'Dont use this one please!', |
| 43 | + * 'keys' => array( 'key1', 'another_key' ), |
| 44 | + * 'callback' => array( 'class', 'method' ), // Optional. |
| 45 | + * ) |
| 46 | + * ) |
| 47 | + * |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + public function getGroups() { |
27 | 51 | return [ |
28 | | - T_CONSTANT_ENCAPSED_STRING, |
| 52 | + 'PostNotIn' => [ |
| 53 | + 'type' => 'warning', |
| 54 | + '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.', |
| 55 | + 'keys' => [ |
| 56 | + 'exclude', |
| 57 | + ], |
| 58 | + ], |
29 | 59 | ]; |
30 | 60 | } |
31 | 61 |
|
@@ -54,6 +84,22 @@ public function process_token( $stackPtr ) { |
54 | 84 | $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.'; |
55 | 85 | $this->phpcsFile->addWarning( $message, $stackPtr, 'PostNotIn' ); |
56 | 86 | } |
| 87 | + |
| 88 | + parent::process_token( $stackPtr ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Callback to process a confirmed key which doesn't need custom logic, but should always error. |
| 93 | + * |
| 94 | + * @param string $key Array index / key. |
| 95 | + * @param mixed $val Assigned value. |
| 96 | + * @param int $line Token line. |
| 97 | + * @param array $group Group definition. |
| 98 | + * @return mixed FALSE if no match, TRUE if matches, STRING if matches |
| 99 | + * with custom error message passed to ->process(). |
| 100 | + */ |
| 101 | + public function callback( $key, $val, $line, $group ) { |
| 102 | + return true; |
57 | 103 | } |
58 | 104 |
|
59 | 105 | } |
0 commit comments