From 1c9da0ac5e9bca651057b58e6650bc27df821e83 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 26 Jan 2019 00:16:41 +0100 Subject: [PATCH 1/2] Generic/FunctionCallArgumentSpacing: minor tweaks #### Make the sniff more efficient by sniffing for fewer tokens The `Tokens::$functionNameTokens` array includes a number of tokens which will never trigger errors from this sniff as they only expect one parameter. Think: `empty()`, `exit()`. However, as the tokens are registered for the sniff, the sniff will be triggered each time those tokens are encountered. I have changed the `register()` method to only register the tokens which will/could actually trigger errors, making the sniff more efficient. #### Confirm correct behaviour with PHP 7.3 trailing commas in function calls Added an extra unit test to confirm that the sniff handles PHP 7.3 trailing commas in function calls correctly. --- .../FunctionCallArgumentSpacingSniff.php | 17 ++++++++++------- .../FunctionCallArgumentSpacingUnitTest.inc | 5 +++++ ...unctionCallArgumentSpacingUnitTest.inc.fixed | 5 +++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php b/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php index 53a512a8d4..0a1c65fc61 100644 --- a/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php @@ -24,13 +24,16 @@ class FunctionCallArgumentSpacingSniff implements Sniff */ public function register() { - $tokens = Tokens::$functionNameTokens; - - $tokens[] = T_VARIABLE; - $tokens[] = T_CLOSE_CURLY_BRACKET; - $tokens[] = T_CLOSE_PARENTHESIS; - - return $tokens; + return[ + T_STRING, + T_ISSET, + T_UNSET, + T_SELF, + T_STATIC, + T_VARIABLE, + T_CLOSE_CURLY_BRACKET, + T_CLOSE_PARENTHESIS, + ]; }//end register() diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc index 8a24fa23ff..3db37ce1f8 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc @@ -133,3 +133,8 @@ my_function_call( ,'e' // phpcs:ignore Standard.Category.Sniff -- for reasons. , 'f' ); + +$foobar = php73_function_call_trailing_comma( + $foo, + $bar, +); diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed index 4e1fae5317..44552af9b7 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed @@ -133,3 +133,8 @@ my_function_call( 'e', // phpcs:ignore Standard.Category.Sniff -- for reasons. 'f' ); + +$foobar = php73_function_call_trailing_comma( + $foo, + $bar, +); From c92b2cd075090cfccb705f344926df9ecb132a42 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Mon, 28 Jan 2019 05:54:08 +0100 Subject: [PATCH 2/2] FunctionCallArgumentSpacing: improve handling of anonymous classes Spacing around equal operators within anonymous classes should be ignored by this sniff, but wasn't. Includes unit test. --- .../Functions/FunctionCallArgumentSpacingSniff.php | 5 ++++- .../Functions/FunctionCallArgumentSpacingUnitTest.inc | 11 +++++++++++ .../FunctionCallArgumentSpacingUnitTest.inc.fixed | 11 +++++++++++ .../Functions/FunctionCallArgumentSpacingUnitTest.php | 1 + 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php b/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php index 0a1c65fc61..601ed46106 100644 --- a/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php +++ b/src/Standards/Generic/Sniffs/Functions/FunctionCallArgumentSpacingSniff.php @@ -89,11 +89,14 @@ public function process(File $phpcsFile, $stackPtr) T_COMMA, T_VARIABLE, T_CLOSURE, + T_ANON_CLASS, T_OPEN_SHORT_ARRAY, ]; while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) { - if ($tokens[$nextSeparator]['code'] === T_CLOSURE) { + if ($tokens[$nextSeparator]['code'] === T_CLOSURE + || $tokens[$nextSeparator]['code'] === T_ANON_CLASS + ) { // Skip closures. $nextSeparator = $tokens[$nextSeparator]['scope_closer']; continue; diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc index 3db37ce1f8..2747553cdb 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc @@ -138,3 +138,14 @@ $foobar = php73_function_call_trailing_comma( $foo, $bar, ); + +$foobar = functionCallAnonClassParam( + new class() { + public $foo=1; + public function methodName($param='foo',$paramTwo='bar') { + $bar=false; + $foo = array(1,2,3); + } + }, + $args=array(), +); diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed index 44552af9b7..b8bb8faef4 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.inc.fixed @@ -138,3 +138,14 @@ $foobar = php73_function_call_trailing_comma( $foo, $bar, ); + +$foobar = functionCallAnonClassParam( + new class() { + public $foo=1; + public function methodName($param='foo',$paramTwo='bar') { + $bar=false; + $foo = array(1,2,3); + } + }, + $args = array(), +); diff --git a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php index c7c5e8d002..2a9781d04c 100644 --- a/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php +++ b/src/Standards/Generic/Tests/Functions/FunctionCallArgumentSpacingUnitTest.php @@ -52,6 +52,7 @@ public function getErrorList() 132 => 2, 133 => 2, 134 => 1, + 150 => 2, ]; }//end getErrorList()