Skip to content

Commit 988c945

Browse files
committed
Added new checks for 0 spaces after the reference and variadic operators (ref #750)
1 parent 4e14760 commit 988c945

File tree

6 files changed

+112
-39
lines changed

6 files changed

+112
-39
lines changed

package.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
4949
-- If a default is specified, the position of the first token in the default value will be set in a "default_token" array index
5050
-- If a default is specified, the position of the equals sign will be set in a "default_equal_token" array index
5151
-- If the paramater is not the last, the position of the comma will be set in a "comma_token" array index
52+
-- If the param is passed by reference, the position of the reference operator will be set in a "reference_token" array index
53+
-- If the param is variable length, the position of the variadic operator will be set in a "variadic_token" array index
5254
- The T_LIST token and it's opening and closing parentheses now contain references to each other in the tokens array
5355
-- Uses the same parenthesis_opener/closer/owner indexes as other tokens
5456
-- Thanks to Juliette Reinders Folmer for the patch
@@ -100,6 +102,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
100102
- Squiz.Commenting.BlockComment no longer requires blank line before comment if it's the first content after the PHP open tag
101103
-- Thanks to Juliette Reinders Folmer for the patch
102104
- Squiz.Functions.FunctionDeclarationArgumentSpacing now has more accurate error messages
105+
- Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a reference operator
106+
-- If you don't want this new behaviour, exclude the SpacingAfterReference error message in a ruleset.xml file
107+
- Squiz.Functions.FunctionDeclarationArgumentSpacing now checks for no space after a variadic operator
108+
-- If you don't want this new behaviour, exclude the SpacingAfterVariadic error message in a ruleset.xml file
103109
- Squiz.Operators.IncrementDecrementUsage now suggests pre-increment of variables instead of post-increment
104110
-- This change does not enforce pre-increment over post-increment; only the suggestion has changed
105111
-- Thanks to Juliette Reinders Folmer for the patch

src/Standards/PSR12/ruleset.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@
168168
<!-- 4.5 Method and Function Arguments -->
169169

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

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

193-
<!-- When using the reference operator & before an argument, there MUST NOT be a space after it. -->
194-
195-
<!-- There MUST NOT be a space between the variadic three dot operator and the argument name. -->
196-
197-
<!-- When combining both the reference operator and the variadic three dot operator, there MUST NOT be any space between the two of them. -->
198-
199193
<!-- 4.6 abstract, final, and static -->
200194

201195
<!-- When present, the abstract and final declarations MUST precede the visibility declaration. -->

src/Standards/Squiz/Sniffs/Functions/FunctionDeclarationArgumentSpacingSniff.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,48 @@ public function processBracket($phpcsFile, $openBracket)
133133
}
134134

135135
foreach ($params as $paramNumber => $param) {
136+
if ($param['pass_by_reference'] === true) {
137+
$refToken = $param['reference_token'];
138+
139+
$gap = 0;
140+
if ($tokens[($refToken + 1)]['code'] === T_WHITESPACE) {
141+
$gap = $tokens[($refToken + 1)]['length'];
142+
}
143+
144+
if ($gap !== 0) {
145+
$error = 'Expected 0 spaces after reference operator for argument "%s"; %s found';
146+
$data = [
147+
$param['name'],
148+
$gap,
149+
];
150+
$fix = $phpcsFile->addFixableError($error, $refToken, 'SpacingAfterReference', $data);
151+
if ($fix === true) {
152+
$phpcsFile->fixer->replaceToken(($refToken + 1), '');
153+
}
154+
}
155+
}//end if
156+
157+
if ($param['variable_length'] === true) {
158+
$variadicToken = $param['variadic_token'];
159+
160+
$gap = 0;
161+
if ($tokens[($variadicToken + 1)]['code'] === T_WHITESPACE) {
162+
$gap = $tokens[($variadicToken + 1)]['length'];
163+
}
164+
165+
if ($gap !== 0) {
166+
$error = 'Expected 0 spaces after variadic operator for argument "%s"; %s found';
167+
$data = [
168+
$param['name'],
169+
$gap,
170+
];
171+
$fix = $phpcsFile->addFixableError($error, $variadicToken, 'SpacingAfterVariadic', $data);
172+
if ($fix === true) {
173+
$phpcsFile->fixer->replaceToken(($variadicToken + 1), '');
174+
}
175+
}
176+
}//end if
177+
136178
if (isset($param['default_equal_token']) === true) {
137179
$equalToken = $param['default_equal_token'];
138180

src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@ function x(
9595
?int $d
9696
) {
9797
}
98+
99+
function functionName( ?string $arg1 = 'foo' , ?int & $arg2 , $arg3 ) {}
100+
function functionName(string $arg1, ... $arg2) {}
101+
function functionName(string $arg1, int ... $arg2) {}
102+
function functionName(string $arg1, & ... $arg2) {}
103+
104+
105+
$a = function ($var1, $var2=false) use (
106+
$longVar1, & $longerVar1,
107+
$longVar2 , &$longerVar2,
108+
$muchLongerVar3
109+
) {};

src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.inc.fixed

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function MissingParamTypeInDocBlock(array $a=null, callable $c, \ArrayObject $o,
7474

7575
function myFunc(...$args) {}
7676
function myFunc(...$args) {}
77-
function myFunc(... $args) {}
77+
function myFunc(...$args) {}
7878

7979
function foo( // comment
8080
$bar,
@@ -95,3 +95,15 @@ function x(
9595
?int $d
9696
) {
9797
}
98+
99+
function functionName(?string $arg1='foo', ?int &$arg2, $arg3) {}
100+
function functionName(string $arg1, ...$arg2) {}
101+
function functionName(string $arg1, int ...$arg2) {}
102+
function functionName(string $arg1, &...$arg2) {}
103+
104+
105+
$a = function ($var1, $var2=false) use (
106+
$longVar1, &$longerVar1,
107+
$longVar2, &$longerVar2,
108+
$muchLongerVar3
109+
) {};

src/Standards/Squiz/Tests/Functions/FunctionDeclarationArgumentSpacingUnitTest.php

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,42 @@ class FunctionDeclarationArgumentSpacingUnitTest extends AbstractSniffUnitTest
2626
public function getErrorList()
2727
{
2828
return [
29-
3 => 1,
30-
5 => 2,
31-
7 => 2,
32-
8 => 2,
33-
9 => 2,
34-
11 => 2,
35-
13 => 7,
36-
14 => 2,
37-
15 => 2,
38-
16 => 4,
39-
18 => 2,
40-
35 => 2,
41-
36 => 2,
42-
44 => 2,
43-
45 => 1,
44-
46 => 1,
45-
51 => 2,
46-
53 => 2,
47-
55 => 1,
48-
56 => 1,
49-
58 => 1,
50-
73 => 7,
51-
76 => 1,
52-
81 => 1,
53-
89 => 2,
54-
92 => 1,
55-
93 => 1,
56-
94 => 1,
57-
95 => 1,
29+
3 => 1,
30+
5 => 2,
31+
7 => 2,
32+
8 => 2,
33+
9 => 2,
34+
11 => 2,
35+
13 => 7,
36+
14 => 2,
37+
15 => 2,
38+
16 => 4,
39+
18 => 2,
40+
35 => 2,
41+
36 => 2,
42+
44 => 2,
43+
45 => 1,
44+
46 => 1,
45+
51 => 2,
46+
53 => 2,
47+
55 => 1,
48+
56 => 1,
49+
58 => 1,
50+
73 => 7,
51+
76 => 1,
52+
77 => 1,
53+
81 => 1,
54+
89 => 2,
55+
92 => 1,
56+
93 => 1,
57+
94 => 1,
58+
95 => 1,
59+
99 => 11,
60+
100 => 2,
61+
101 => 2,
62+
102 => 2,
63+
106 => 1,
64+
107 => 2,
5865
];
5966

6067
}//end getErrorList()

0 commit comments

Comments
 (0)