Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 26 additions & 46 deletions WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,75 +8,46 @@

namespace WordPressVIPMinimum\Sniffs\Performance;

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

/**
* Flag suspicious WP_Query and get_posts params.
*
* @package VIPCS\WordPressVIPMinimum
* @link https://docs.wpvip.com/technical-references/caching/uncached-functions/
*
* @package VIPCS\WordPressVIPMinimum
*/
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.
*
* @return array
*/
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' => [
'name' => 'SuppressFilters',
'type' => 'error',
'message' => 'Setting `suppress_filters` to `true` is prohibited.',
'keys' => [
'suppress_filters',
],
],
'PostNotIn' => [
'name' => 'PostNotIn',
'type' => 'warning',
'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.',
'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.',
'keys' => [
'post__not_in',
'exclude',
],
],
];
}

/**
* 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.
*
* @return void
*/
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 ) {
// https://docs.wpvip.com/technical-references/caching/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://docs.wpvip.com/how-tos/improve-performance-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.
*
Expand All @@ -88,6 +59,15 @@ public function process_token( $stackPtr ) {
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
return true;
switch ( $group['name'] ) {
case 'SuppressFilters':
return ( $val === 'true' );

case 'PostNotIn':
return true;

default:
return false;
}
}
}