diff --git a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php index 1a10945d..357b7c2d 100644 --- a/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php +++ b/WordPressVIPMinimum/Sniffs/Hooks/AlwaysReturnInFilterSniff.php @@ -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 ); @@ -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 ); diff --git a/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc b/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc index 591e233d..4f9d6d00 100644 --- a/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.inc @@ -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. + } +} + diff --git a/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.php b/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.php index 41339fa6..ee2cd6c8 100644 --- a/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.php +++ b/WordPressVIPMinimum/Tests/Hooks/AlwaysReturnInFilterUnitTest.php @@ -28,6 +28,7 @@ public function getErrorList() { 95 => 1, 105 => 1, 129 => 1, + 163 => 1, ]; }