Skip to content

Commit 399b1b3

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: bug fix / SpacingAfterReference does not flag new lines
A `T_WHITESPACE` token only containing new line characters will have its token `'length'` set to `0`. This means that if there was a new line after a reference token, the sniff would not report it. Fixed now. Also note that, as the sniff demands no whitespace after a reference operator, we don't need the extra check for `$gap !== 0` as we already know that the next token is a whitespace token and needs to be flagged. Includes tests.
1 parent 604c25f commit 399b1b3

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ public function processBracket($phpcsFile, $openBracket)
149149
if ($param['pass_by_reference'] === true) {
150150
$refToken = $param['reference_token'];
151151

152-
$gap = 0;
153152
if ($tokens[($refToken + 1)]['code'] === T_WHITESPACE) {
154153
$gap = $tokens[($refToken + 1)]['length'];
155-
}
154+
if ($tokens[$refToken]['line'] !== $tokens[($refToken + 2)]['line']) {
155+
$gap = 'newline';
156+
}
156157

157-
if ($gap !== 0) {
158158
$error = 'Expected 0 spaces after reference operator for argument "%s"; %s found';
159159
$data = [
160160
$param['name'],

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,15 @@ function spacingBetweenParenthesesShouldBeFixedInOneGo(
118118

119119

120120
) {}
121+
122+
function newlineAfterReferenceShouldBeFlaggedAndFixed(
123+
&
124+
125+
$param
126+
) {}
127+
128+
function newlineAfterReferenceFixerRespectsComment(
129+
&
130+
// comment
131+
$param
132+
) {}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,12 @@ fn ($a, $b=null) => $a($b);
113113
function multipleWhitespaceTokensAfterType(int $number) {}
114114

115115
function spacingBetweenParenthesesShouldBeFixedInOneGo() {}
116+
117+
function newlineAfterReferenceShouldBeFlaggedAndFixed(
118+
&$param
119+
) {}
120+
121+
function newlineAfterReferenceFixerRespectsComment(
122+
&// comment
123+
$param
124+
) {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public function getErrorList()
7070
111 => 3,
7171
113 => 1,
7272
117 => 1,
73+
123 => 1,
74+
129 => 1,
7375
];
7476

7577
}//end getErrorList()

0 commit comments

Comments
 (0)