Skip to content

Commit b159efc

Browse files
committed
Hooks/PreGetPosts: add support for hook-ins using short arrays
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.
1 parent 84605b5 commit b159efc

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public function process_token( $stackPtr ) {
7979

8080
if ( 'PHPCS_T_CLOSURE' === $this->tokens[ $callbackPtr ]['code'] ) {
8181
$this->processClosure( $callbackPtr );
82-
} elseif ( 'T_ARRAY' === $this->tokens[ $callbackPtr ]['type'] ) {
82+
} elseif ( T_ARRAY === $this->tokens[ $callbackPtr ]['code']
83+
|| T_OPEN_SHORT_ARRAY === $this->tokens[ $callbackPtr ]['code']
84+
) {
8385
$this->processArray( $callbackPtr );
8486
} elseif ( true === in_array( $this->tokens[ $callbackPtr ]['code'], Tokens::$stringTokens, true ) ) {
8587
$this->processString( $callbackPtr );
@@ -93,9 +95,14 @@ public function process_token( $stackPtr ) {
9395
*/
9496
private function processArray( $stackPtr ) {
9597

98+
$open_close = $this->find_array_open_close( $stackPtr );
99+
if ( false === $open_close ) {
100+
return;
101+
}
102+
96103
$previous = $this->phpcsFile->findPrevious(
97104
Tokens::$emptyTokens,
98-
$this->tokens[ $stackPtr ]['parenthesis_closer'] - 1,
105+
$open_close['closer'] - 1,
99106
null,
100107
true
101108
);

WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,33 @@ add_action( 'pre_get_posts', function( $wp_query ) {
9090
}
9191

9292
} );
93+
94+
class short_array_hook_in {
95+
96+
public function __construct() {
97+
add_action( 'pre_get_posts', [ $this, 'short_pre_get_posts' ] );
98+
}
99+
100+
public function short_pre_get_posts( $wp_query ) {
101+
102+
$wp_query->set( 'cat', '-5' );
103+
104+
if ( $wp_query->is_main_query() ) {
105+
$wp_query->set( 'cat', '-5' );
106+
} else if ( $wp_query->is_search() ) {
107+
$wp_query->set( 'cat', '-5' );
108+
}
109+
110+
if ( ( ! $wp_query->is_main_query() ) ) {
111+
return;
112+
}
113+
114+
$wp_query->set( 'cat', '-5' );
115+
116+
if ( $wp_query->is_main_query() ) {
117+
$wp_query->set( 'cat', '-5' );
118+
} else if ( $wp_query->is_search() ) {
119+
$wp_query->set( 'cat', '-5' );
120+
}
121+
}
122+
}

WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ public function getErrorList() {
3232
*/
3333
public function getWarningList() {
3434
return [
35-
8 => 1,
36-
11 => 1,
37-
29 => 1,
38-
32 => 1,
39-
52 => 1,
40-
57 => 1,
41-
87 => 1,
35+
8 => 1,
36+
11 => 1,
37+
29 => 1,
38+
32 => 1,
39+
52 => 1,
40+
57 => 1,
41+
87 => 1,
42+
102 => 1,
43+
107 => 1,
4244
];
4345
}
4446

0 commit comments

Comments
 (0)