diff --git a/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php index 655bbdae..cd2c8867 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php @@ -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 ); @@ -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 ); diff --git a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc index 09c5f06e..5cb8c0d4 100644 --- a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc @@ -116,3 +116,33 @@ function inline_control_structures( $query ) { $query->set('meta_query', 'foo'); return $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' ); + } + } +} diff --git a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php index 058f2d6b..02e45189 100644 --- a/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php +++ b/WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php @@ -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, + 128 => 1, + 133 => 1, ]; }