Skip to content

Commit 1686286

Browse files
committed
Performance/WPQueryParams: defer to the parent sniff
This commit removes the custom token target + custom logic from this sniff in favour of deferring to the logic in the parent sniff - as discussed in 589. To that end, the keys which were handled in the custom `process_token()` logic have now been added to the `getGroups()` array. As the logic for whether or not an error message should be thrown is different for each group, an extra `'name'` key has been added to each group to allow the `callback()` function to distinguish what group the detected key came from. It also updates the error message being used to better cover both keys being looked for, as well as mention this only applies when the array is passed to `get_posts()` (in an attempt to remove some of the confusion reported in 672). Note: this is a BC-break as the error codes for the two out of the three 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`. * The `WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude` error code, as introduced in 589, remains the same. Fixes 594
1 parent 0fd1c85 commit 1686286

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,46 @@
88

99
namespace WordPressVIPMinimum\Sniffs\Performance;
1010

11-
use PHP_CodeSniffer\Util\Tokens;
1211
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
1312

1413
/**
1514
* Flag suspicious WP_Query and get_posts params.
1615
*
17-
* @package VIPCS\WordPressVIPMinimum
16+
* @link https://docs.wpvip.com/technical-references/caching/uncached-functions/
17+
*
18+
* @package VIPCS\WordPressVIPMinimum
1819
*/
1920
class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff {
2021

21-
/**
22-
* Returns an array of tokens this test wants to listen for.
23-
*
24-
* @return array
25-
*/
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-
3522
/**
3623
* Groups of variables to restrict.
3724
*
3825
* @return array
3926
*/
4027
public function getGroups() {
4128
return [
29+
// WordPress.com: https://lobby.vip.wordpress.com/wordpress-com-documentation/uncached-functions/.
30+
// VIP Go: https://wpvip.com/documentation/vip-go/uncached-functions/.
31+
'SuppressFilters' => [
32+
'name' => 'SuppressFilters',
33+
'type' => 'error',
34+
'message' => 'Setting `suppress_filters` to `true` is prohibited.',
35+
'keys' => [
36+
'suppress_filters',
37+
],
38+
],
4239
'PostNotIn' => [
40+
'name' => 'PostNotIn',
4341
'type' => 'warning',
44-
'message' => 'Using `exclude`, which is subsequently used by `post__not_in`, should be done with caution, see https://docs.wpvip.com/how-tos/improve-performance-by-removing-usage-of-post__not_in/ for more information.',
42+
'message' => 'Using exclusionary parameters, like %s, in calls to get_posts() should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.',
4543
'keys' => [
44+
'post__not_in',
4645
'exclude',
4746
],
4847
],
4948
];
5049
}
5150

52-
/**
53-
* Process this test when one of its tokens is encountered
54-
*
55-
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
56-
*
57-
* @return void
58-
*/
59-
public function process_token( $stackPtr ) {
60-
61-
if ( trim( $this->tokens[ $stackPtr ]['content'], '\'' ) === 'suppress_filters' ) {
62-
63-
$next_token = $this->phpcsFile->findNext( array_merge( Tokens::$emptyTokens, [ T_EQUAL, T_CLOSE_SQUARE_BRACKET, T_DOUBLE_ARROW ] ), $stackPtr + 1, null, true );
64-
65-
if ( $this->tokens[ $next_token ]['code'] === T_TRUE ) {
66-
// https://docs.wpvip.com/technical-references/caching/uncached-functions/.
67-
$message = 'Setting `suppress_filters` to `true` is prohibited.';
68-
$this->phpcsFile->addError( $message, $stackPtr, 'SuppressFiltersTrue' );
69-
}
70-
}
71-
72-
if ( trim( $this->tokens[ $stackPtr ]['content'], '\'' ) === 'post__not_in' ) {
73-
$message = 'Using `post__not_in` should be done with caution, see https://docs.wpvip.com/how-tos/improve-performance-by-removing-usage-of-post__not_in/ for more information.';
74-
$this->phpcsFile->addWarning( $message, $stackPtr, 'PostNotIn' );
75-
}
76-
77-
parent::process_token( $stackPtr );
78-
}
79-
8051
/**
8152
* Callback to process a confirmed key which doesn't need custom logic, but should always error.
8253
*
@@ -88,6 +59,15 @@ public function process_token( $stackPtr ) {
8859
* @return bool FALSE if no match, TRUE if matches.
8960
*/
9061
public function callback( $key, $val, $line, $group ) {
91-
return true;
62+
switch ( $group['name'] ) {
63+
case 'SuppressFilters':
64+
return ( $val === 'true' );
65+
66+
case 'PostNotIn':
67+
return true;
68+
69+
default:
70+
return false;
71+
}
9272
}
9373
}

0 commit comments

Comments
 (0)