Skip to content

Commit

Permalink
WPQueryParams: sniff for exclude array key being set
Browse files Browse the repository at this point in the history
This:
* Switches the `WordPressVIPMinimum.Performance.WPQueryParams` sniff over to use the WordPressCS `AbstractArrayAssignmentRestrictionsSniff` as the parent sniff.
* Adds sniffing for the `exclude` array key via the `AbstractArrayAssignmentRestrictionsSniff` sniff logic.

Includes unit test.
  • Loading branch information
jrfnl committed Oct 25, 2020
1 parent d374355 commit 20fdf8b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
52 changes: 49 additions & 3 deletions WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php
Expand Up @@ -8,24 +8,54 @@

namespace WordPressVIPMinimum\Sniffs\Performance;

use WordPressVIPMinimum\Sniffs\Sniff;
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
use PHP_CodeSniffer\Util\Tokens;

/**
* Flag suspicious WP_Query and get_posts params.
*
* @package VIPCS\WordPressVIPMinimum
*/
class WPQueryParamsSniff extends Sniff {
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.
*
* 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',
],
],
];
}

Expand Down Expand Up @@ -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;
}

}
Expand Up @@ -16,4 +16,8 @@ $q = new WP_Query( $query_args );

$query_args[ 'suppress_filters' ] = true;

$q = new WP_query( $query_args );
$q = new WP_query( $query_args );

get_posts( [ 'exclude' => $post_ids ] ); // Warning.

$exclude = [ 1, 2, 3 ];
Expand Up @@ -39,6 +39,7 @@ public function getWarningList() {
return [
4 => 1,
11 => 1,
21 => 1,
];
}

Expand Down

0 comments on commit 20fdf8b

Please sign in to comment.