Skip to content

Commit 060fcf1

Browse files
committed
Hooks/AlwaysReturnInFilter: 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 `AlwaysReturnInFilter` sniff. Includes unit tests.
1 parent 84605b5 commit 060fcf1

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php

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

7979
if ( 'PHPCS_T_CLOSURE' === $this->tokens[ $callbackPtr ]['code'] ) {
8080
$this->processFunctionBody( $callbackPtr );
81-
} elseif ( 'T_ARRAY' === $this->tokens[ $callbackPtr ]['type'] ) {
81+
} elseif ( T_ARRAY === $this->tokens[ $callbackPtr ]['code']
82+
|| T_OPEN_SHORT_ARRAY === $this->tokens[ $callbackPtr ]['code']
83+
) {
8284
$this->processArray( $callbackPtr );
8385
} elseif ( true === in_array( $this->tokens[ $callbackPtr ]['code'], Tokens::$stringTokens, true ) ) {
8486
$this->processString( $callbackPtr );
@@ -92,9 +94,14 @@ public function process_token( $stackPtr ) {
9294
*/
9395
private function processArray( $stackPtr ) {
9496

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

WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,38 @@ function bad_example_arg( $test ) { // Error.
137137
// Missing universal return.
138138
}
139139
add_filter( 'bad_example_filter', 'bad_example_arg' );
140+
141+
class good_example_class_short_array { // Ok.
142+
public function __construct() {
143+
add_filter( 'good_example_class_filter', [ $this, 'class_filter' ] );
144+
}
145+
146+
public function class_filter( $param ) {
147+
if ( 1 === 1 ) {
148+
if ( 1 === 0 ) {
149+
return 'whoops';
150+
} else {
151+
return 'here!';
152+
}
153+
}
154+
return 'This is Okay';
155+
}
156+
}
157+
158+
class bad_example_class_short_array { // Error.
159+
public function __construct() {
160+
add_filter( 'bad_example_class_filter', [ $this, 'class_filter' ] );
161+
}
162+
163+
public function class_filter( $param ) {
164+
if ( 1 === 1 ) {
165+
if ( 1 === 0 ) {
166+
return 'whoops';
167+
} else {
168+
return 'here!';
169+
}
170+
}
171+
// Missing universal return.
172+
}
173+
}
174+

WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function getErrorList() {
2828
95 => 1,
2929
105 => 1,
3030
129 => 1,
31+
163 => 1,
3132
];
3233
}
3334

0 commit comments

Comments
 (0)