Skip to content

Commit

Permalink
Hooks/AlwaysReturnInFilter: 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 `AlwaysReturnInFilter` sniff.

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

if ( 'PHPCS_T_CLOSURE' === $this->tokens[ $callbackPtr ]['code'] ) {
$this->processFunctionBody( $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 @@ -92,9 +94,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
35 changes: 35 additions & 0 deletions WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,38 @@ function bad_example_arg( $test ) { // Error.
// Missing universal return.
}
add_filter( 'bad_example_filter', 'bad_example_arg' );

class good_example_class_short_array { // Ok.
public function __construct() {
add_filter( 'good_example_class_filter', [ $this, 'class_filter' ] );
}

public function class_filter( $param ) {
if ( 1 === 1 ) {
if ( 1 === 0 ) {
return 'whoops';
} else {
return 'here!';
}
}
return 'This is Okay';
}
}

class bad_example_class_short_array { // Error.
public function __construct() {
add_filter( 'bad_example_class_filter', [ $this, 'class_filter' ] );
}

public function class_filter( $param ) {
if ( 1 === 1 ) {
if ( 1 === 0 ) {
return 'whoops';
} else {
return 'here!';
}
}
// Missing universal return.
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getErrorList() {
95 => 1,
105 => 1,
129 => 1,
163 => 1,
];
}

Expand Down

0 comments on commit 060fcf1

Please sign in to comment.