Skip to content

Commit

Permalink
Merge pull request #784 from Automattic/3.0/fix/672-wpqueryparams-pre…
Browse files Browse the repository at this point in the history
…vent-false-positives-get_users
  • Loading branch information
GaryJones authored Aug 26, 2023
2 parents 5f34bbe + 47c1c2c commit 3ab2f0c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
27 changes: 27 additions & 0 deletions WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace WordPressVIPMinimum\Sniffs\Performance;

use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
use WordPressCS\WordPress\Helpers\ContextHelper;

/**
* Flag suspicious WP_Query and get_posts params.
Expand All @@ -19,6 +20,13 @@
*/
class WPQueryParamsSniff extends AbstractArrayAssignmentRestrictionsSniff {

/**
* Whether the current $stackPtr being scanned is nested in a function call to get_users().
*
* @var bool
*/
private $in_get_users = false;

/**
* Groups of variables to restrict.
*
Expand Down Expand Up @@ -48,6 +56,20 @@ public function getGroups() {
];
}

/**
* Processes this test, when one of its tokens is encountered.
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process_token( $stackPtr ) {
$this->in_get_users = ContextHelper::is_in_function_call( $this->phpcsFile, $stackPtr, [ 'get_users' => true ], true, true );

parent::process_token( $stackPtr );
}


/**
* Callback to process a confirmed key which doesn't need custom logic, but should always error.
*
Expand All @@ -64,6 +86,11 @@ public function callback( $key, $val, $line, $group ) {
return ( $val === 'true' );

case 'PostNotIn':
if ( $key === 'exclude' && $this->in_get_users !== false ) {
// This is not an array used by get_posts(). See #672.
return false;
}

return true;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ $q = new WP_query( $query_args );
get_posts( [ 'exclude' => $post_ids ] ); // Warning.

$exclude = [ 1, 2, 3 ];

// Issue #672 / #729.
get_users( [ 'exclude' => $post_ids ] ); // OK.
get_users( My\get_args( [ 'exclude' => $post_ids ] ) ); // OK - arbitrary as the call to `My\get_args()` on its own would be flagged, but let's allow it.

$context_unknown = [ 'exclude' => $post_ids ]; // Warning.
other_fn_calls_still_throw_warning( [ 'exclude' => $post_ids ] ); // Warning.
get_users( [ 'suppress_filters' => true ] ); // Error - not necessarily valid, but the exception being made is specifically about `exclude`.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getErrorList() {
return [
5 => 1,
17 => 1,
31 => 1,
];
}

Expand All @@ -40,6 +41,8 @@ public function getWarningList() {
4 => 1,
11 => 1,
21 => 1,
29 => 1,
30 => 1,
];
}
}

0 comments on commit 3ab2f0c

Please sign in to comment.