From 060fcf17672a14a3d58fb47ea853175bd9845f17 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Tue, 28 Jul 2020 01:01:06 +0200 Subject: [PATCH] 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. --- .../Hooks/AlwaysReturnInFilterSniff.php | 11 ++++-- .../Hooks/AlwaysReturnInFilterUnitTest.inc | 35 +++++++++++++++++++ .../Hooks/AlwaysReturnInFilterUnitTest.php | 1 + 3 files changed, 45 insertions(+), 2 deletions(-) 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, ]; }