Skip to content

Commit 0e271eb

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: bug fix / SpaceBeforeEquals + SpaceAfterEquals do not flag new lines for spacing 0
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 before/after the equal sign and the `$equalsSpacing` property was set to the default value of `0`, the sniff would not report it. When the `$equalsSpacing` property was set to `1`, the sniff already flagged this correctly (though there are other problems with that in the fixer - see later commit). Fixed now. Includes tests.
1 parent 432e5b9 commit 0e271eb

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ public function processBracket($phpcsFile, $openBracket)
202202
$equalToken = $param['default_equal_token'];
203203

204204
$spacesBefore = 0;
205-
if (($equalToken - $param['token']) > 1) {
205+
if ($tokens[$param['token']]['line'] !== $tokens[$equalToken]['line']) {
206+
$spacesBefore = 'newline';
207+
} else if ($tokens[($param['token'] + 1)]['code'] === T_WHITESPACE) {
206208
$spacesBefore = $tokens[($param['token'] + 1)]['length'];
207209
}
208210

@@ -225,7 +227,9 @@ public function processBracket($phpcsFile, $openBracket)
225227
}//end if
226228

227229
$spacesAfter = 0;
228-
if ($tokens[($equalToken + 1)]['code'] === T_WHITESPACE) {
230+
if ($tokens[$equalToken]['line'] !== $tokens[$param['default_token']]['line']) {
231+
$spacesAfter = 'newline';
232+
} else if ($tokens[($equalToken + 1)]['code'] === T_WHITESPACE) {
229233
$spacesAfter = $tokens[($equalToken + 1)]['length'];
230234
}
231235

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,11 @@ function newlineAfterVariadicFixerRespectsComment(
142142
//comment
143143
$param
144144
) {}
145+
146+
function newlineBeforeAndAfterEqualsSignShouldBeFixedForSpacing0(
147+
$param
148+
149+
=
150+
151+
true
152+
) {}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,7 @@ function newlineAfterVariadicFixerRespectsComment(
131131
...//comment
132132
$param
133133
) {}
134+
135+
function newlineBeforeAndAfterEqualsSignShouldBeFixedForSpacing0(
136+
$param=true
137+
) {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function getErrorList()
7474
129 => 1,
7575
135 => 1,
7676
141 => 1,
77+
149 => 2,
7778
];
7879

7980
}//end getErrorList()

0 commit comments

Comments
 (0)