From 988c945acb4688499c79899c9e11c422affe75b1 Mon Sep 17 00:00:00 2001 From: Greg Sherwood Date: Tue, 27 Aug 2019 11:46:14 +1000 Subject: [PATCH] Added new checks for 0 spaces after the reference and variadic operators (ref #750) --- package.xml | 6 ++ src/Standards/PSR12/ruleset.xml | 12 +--- ...unctionDeclarationArgumentSpacingSniff.php | 42 ++++++++++++ ...tionDeclarationArgumentSpacingUnitTest.inc | 12 ++++ ...clarationArgumentSpacingUnitTest.inc.fixed | 14 +++- ...tionDeclarationArgumentSpacingUnitTest.php | 65 ++++++++++--------- 6 files changed, 112 insertions(+), 39 deletions(-) diff --git a/package.xml b/package.xml index addc0ac6e7..371825f8ce 100644 --- a/package.xml +++ b/package.xml @@ -49,6 +49,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> -- If a default is specified, the position of the first token in the default value will be set in a "default_token" array index -- If a default is specified, the position of the equals sign will be set in a "default_equal_token" array index -- If the paramater is not the last, the position of the comma will be set in a "comma_token" array index + -- If the param is passed by reference, the position of the reference operator will be set in a "reference_token" array index + -- If the param is variable length, the position of the variadic operator will be set in a "variadic_token" array index - The T_LIST token and it's opening and closing parentheses now contain references to each other in the tokens array -- Uses the same parenthesis_opener/closer/owner indexes as other tokens -- Thanks to Juliette Reinders Folmer for the patch @@ -100,6 +102,10 @@ http://pear.php.net/dtd/package-2.0.xsd"> - Squiz.Commenting.BlockComment no longer requires blank line before comment if it's the first content after the PHP open tag -- Thanks to Juliette Reinders Folmer for the patch - Squiz.Functions.FunctionDeclarationArgumentSpacing now has more accurate error messages + - Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a reference operator + -- If you don't want this new behaviour, exclude the SpacingAfterReference error message in a ruleset.xml file + - Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a variadic operator + -- If you don't want this new behaviour, exclude the SpacingAfterVariadic error message in a ruleset.xml file - Squiz.Operators.IncrementDecrementUsage now suggests pre-increment of variables instead of post-increment -- This change does not enforce pre-increment over post-increment; only the suggestion has changed -- Thanks to Juliette Reinders Folmer for the patch diff --git a/src/Standards/PSR12/ruleset.xml b/src/Standards/PSR12/ruleset.xml index 6b99768e98..1d8ddb04e8 100644 --- a/src/Standards/PSR12/ruleset.xml +++ b/src/Standards/PSR12/ruleset.xml @@ -168,14 +168,14 @@ + + + - - 0 - @@ -190,12 +190,6 @@ - - - - - - diff --git a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php index fdb4854115..f407ba58ac 100644 --- a/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php +++ b/src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php @@ -133,6 +133,48 @@ public function processBracket($phpcsFile, $openBracket) } foreach ($params as $paramNumber => $param) { + if ($param['pass_by_reference'] === true) { + $refToken = $param['reference_token']; + + $gap = 0; + if ($tokens[($refToken + 1)]['code'] === T_WHITESPACE) { + $gap = $tokens[($refToken + 1)]['length']; + } + + if ($gap !== 0) { + $error = 'Expected 0 spaces after reference operator for argument "%s"; %s found'; + $data = [ + $param['name'], + $gap, + ]; + $fix = $phpcsFile->addFixableError($error, $refToken, 'SpacingAfterReference', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($refToken + 1), ''); + } + } + }//end if + + if ($param['variable_length'] === true) { + $variadicToken = $param['variadic_token']; + + $gap = 0; + if ($tokens[($variadicToken + 1)]['code'] === T_WHITESPACE) { + $gap = $tokens[($variadicToken + 1)]['length']; + } + + if ($gap !== 0) { + $error = 'Expected 0 spaces after variadic operator for argument "%s"; %s found'; + $data = [ + $param['name'], + $gap, + ]; + $fix = $phpcsFile->addFixableError($error, $variadicToken, 'SpacingAfterVariadic', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($variadicToken + 1), ''); + } + } + }//end if + if (isset($param['default_equal_token']) === true) { $equalToken = $param['default_equal_token']; diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc index 525f142ec8..b1e7d0c22b 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc @@ -95,3 +95,15 @@ function x( ?int $d ) { } + +function functionName( ?string $arg1 = 'foo' , ?int & $arg2 , $arg3 ) {} +function functionName(string $arg1, ... $arg2) {} +function functionName(string $arg1, int ... $arg2) {} +function functionName(string $arg1, & ... $arg2) {} + + +$a = function ($var1, $var2=false) use ( + $longVar1, & $longerVar1, + $longVar2 , &$longerVar2, + $muchLongerVar3 +) {}; diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed index 939354f099..e25fcf2d79 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed @@ -74,7 +74,7 @@ function MissingParamTypeInDocBlock(array $a=null, callable $c, \ArrayObject $o, function myFunc(...$args) {} function myFunc(...$args) {} -function myFunc(... $args) {} +function myFunc(...$args) {} function foo( // comment $bar, @@ -95,3 +95,15 @@ function x( ?int $d ) { } + +function functionName(?string $arg1='foo', ?int &$arg2, $arg3) {} +function functionName(string $arg1, ...$arg2) {} +function functionName(string $arg1, int ...$arg2) {} +function functionName(string $arg1, &...$arg2) {} + + +$a = function ($var1, $var2=false) use ( + $longVar1, &$longerVar1, + $longVar2, &$longerVar2, + $muchLongerVar3 +) {}; diff --git a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php index 3a7f77667e..f1c6fa65ad 100644 --- a/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php +++ b/src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php @@ -26,35 +26,42 @@ class FunctionDeclarationArgumentSpacingUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 3 => 1, - 5 => 2, - 7 => 2, - 8 => 2, - 9 => 2, - 11 => 2, - 13 => 7, - 14 => 2, - 15 => 2, - 16 => 4, - 18 => 2, - 35 => 2, - 36 => 2, - 44 => 2, - 45 => 1, - 46 => 1, - 51 => 2, - 53 => 2, - 55 => 1, - 56 => 1, - 58 => 1, - 73 => 7, - 76 => 1, - 81 => 1, - 89 => 2, - 92 => 1, - 93 => 1, - 94 => 1, - 95 => 1, + 3 => 1, + 5 => 2, + 7 => 2, + 8 => 2, + 9 => 2, + 11 => 2, + 13 => 7, + 14 => 2, + 15 => 2, + 16 => 4, + 18 => 2, + 35 => 2, + 36 => 2, + 44 => 2, + 45 => 1, + 46 => 1, + 51 => 2, + 53 => 2, + 55 => 1, + 56 => 1, + 58 => 1, + 73 => 7, + 76 => 1, + 77 => 1, + 81 => 1, + 89 => 2, + 92 => 1, + 93 => 1, + 94 => 1, + 95 => 1, + 99 => 11, + 100 => 2, + 101 => 2, + 102 => 2, + 106 => 1, + 107 => 2, ]; }//end getErrorList()