Skip to content

Commit

Permalink
Added new checks for 0 spaces after the reference and variadic operat…
Browse files Browse the repository at this point in the history
…ors (ref #750)
  • Loading branch information
gsherwood committed Aug 27, 2019
1 parent 4e14760 commit 988c945
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 39 deletions.
6 changes: 6 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 3 additions & 9 deletions src/Standards/PSR12/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@
<!-- 4.5 Method and Function Arguments -->

<!-- In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. -->
<!-- When using the reference operator & before an argument, there MUST NOT be a space after it. -->
<!-- There MUST NOT be a space between the variadic three dot operator and the argument name. -->
<!-- When combining both the reference operator and the variadic three dot operator, there MUST NOT be any space between the two of them. -->
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
<properties>
<property name="equalsSpacing" value="1"/>
</properties>
</rule>
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint">
<severity>0</severity>
</rule>

<!-- Method and function arguments with default values MUST go at the end of the argument list. -->
<rule ref="PEAR.Functions.ValidDefaultValue"/>
Expand All @@ -190,12 +190,6 @@
<!-- In nullable type declarations, there MUST NOT be a space between the question mark and the type. -->
<!-- checked by PSR12.Functions.NullableTypeDeclaration -->

<!-- When using the reference operator & before an argument, there MUST NOT be a space after it. -->

<!-- There MUST NOT be a space between the variadic three dot operator and the argument name. -->

<!-- When combining both the reference operator and the variadic three dot operator, there MUST NOT be any space between the two of them. -->

<!-- 4.6 abstract, final, and static -->

<!-- When present, the abstract and final declarations MUST precede the visibility declaration. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {};
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
) {};
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 988c945

Please sign in to comment.