Skip to content

Commit

Permalink
Hooks/PreGetPosts: add support for hook-ins using short arrays
Browse files Browse the repository at this point in the history
As VIPCS is currently using WPCS 2.x, we can use the WPCS `Sniff::find_array_open_close()` method to get the opener/closer for an array independently of the type of array (long/short).

Once VIPCS implements PHPCSUtils, this method call should be swopped out for the PHPCSUtils `Arrays::getOpenClose()` method.

Addresses #358 for the `PreGetPosts` sniff.

Includes unit tests.
  • Loading branch information
jrfnl committed Jul 28, 2020
1 parent 84605b5 commit b159efc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
11 changes: 9 additions & 2 deletions WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public function process_token( $stackPtr ) {

if ( 'PHPCS_T_CLOSURE' === $this->tokens[ $callbackPtr ]['code'] ) {
$this->processClosure( $callbackPtr );
} elseif ( 'T_ARRAY' === $this->tokens[ $callbackPtr ]['type'] ) {
} elseif ( T_ARRAY === $this->tokens[ $callbackPtr ]['code']
|| T_OPEN_SHORT_ARRAY === $this->tokens[ $callbackPtr ]['code']
) {
$this->processArray( $callbackPtr );
} elseif ( true === in_array( $this->tokens[ $callbackPtr ]['code'], Tokens::$stringTokens, true ) ) {
$this->processString( $callbackPtr );
Expand All @@ -93,9 +95,14 @@ public function process_token( $stackPtr ) {
*/
private function processArray( $stackPtr ) {

$open_close = $this->find_array_open_close( $stackPtr );
if ( false === $open_close ) {
return;
}

$previous = $this->phpcsFile->findPrevious(
Tokens::$emptyTokens,
$this->tokens[ $stackPtr ]['parenthesis_closer'] - 1,
$open_close['closer'] - 1,
null,
true
);
Expand Down
30 changes: 30 additions & 0 deletions WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,33 @@ add_action( 'pre_get_posts', function( $wp_query ) {
}

} );

class short_array_hook_in {

public function __construct() {
add_action( 'pre_get_posts', [ $this, 'short_pre_get_posts' ] );
}

public function short_pre_get_posts( $wp_query ) {

$wp_query->set( 'cat', '-5' );

if ( $wp_query->is_main_query() ) {
$wp_query->set( 'cat', '-5' );
} else if ( $wp_query->is_search() ) {
$wp_query->set( 'cat', '-5' );
}

if ( ( ! $wp_query->is_main_query() ) ) {
return;
}

$wp_query->set( 'cat', '-5' );

if ( $wp_query->is_main_query() ) {
$wp_query->set( 'cat', '-5' );
} else if ( $wp_query->is_search() ) {
$wp_query->set( 'cat', '-5' );
}
}
}
16 changes: 9 additions & 7 deletions WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public function getErrorList() {
*/
public function getWarningList() {
return [
8 => 1,
11 => 1,
29 => 1,
32 => 1,
52 => 1,
57 => 1,
87 => 1,
8 => 1,
11 => 1,
29 => 1,
32 => 1,
52 => 1,
57 => 1,
87 => 1,
102 => 1,
107 => 1,
];
}

Expand Down

0 comments on commit b159efc

Please sign in to comment.