Skip to content

Commit ec5bd51

Browse files
committed
fix up! Squiz/FunctionDeclarationArgumentSpacing: improve SpaceBeforeComma fixer
1 parent 1169e38 commit ec5bd51

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,34 @@ public function processBracket($phpcsFile, $openBracket)
330330

331331
$fix = $phpcsFile->addFixableError($error, $commaToken, 'SpaceBeforeComma', $data);
332332
if ($fix === true) {
333+
$startOfCurrentParam = $phpcsFile->findNext(Tokens::$emptyTokens, ($commaToken + 1), null, true);
334+
333335
$phpcsFile->fixer->beginChangeset();
334336
$phpcsFile->fixer->addContent($endOfPreviousParam, ',');
335337
$phpcsFile->fixer->replaceToken($commaToken, '');
336338

337-
for ($i = ($commaToken - 1); $tokens[$i]['code'] === T_WHITESPACE; $i--) {
338-
$phpcsFile->fixer->replaceToken($i, '');
339-
}
339+
if ($tokens[$commaToken]['line'] === $tokens[$startOfCurrentParam]['line']) {
340+
for ($i = ($commaToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
341+
$phpcsFile->fixer->replaceToken($i, '');
342+
}
343+
} else {
344+
for ($i = ($commaToken - 1);
345+
$tokens[$i]['code'] === T_WHITESPACE && $tokens[$endOfPreviousParam]['line'] !== $tokens[$i]['line'];
346+
$i--
347+
) {
348+
$phpcsFile->fixer->replaceToken($i, '');
349+
}
340350

341-
if ($tokens[$i]['code'] === T_COMMENT
342-
&& substr($tokens[$i]['content'], -1) === $phpcsFile->eolChar
343-
) {
344-
$phpcsFile->fixer->replaceToken($i, substr($tokens[$i]['content'], 0, -1));
351+
for ($i = ($commaToken + 1);
352+
$tokens[$i]['code'] === T_WHITESPACE && $tokens[$commaToken]['line'] === $tokens[$i]['line'];
353+
$i++
354+
) {
355+
$phpcsFile->fixer->replaceToken($i, '');
356+
}
345357
}
346358

347359
$phpcsFile->fixer->endChangeset();
348-
}
360+
}//end if
349361
}//end if
350362

351363
// Don't check spacing after the comma if it is the last content on the line.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,7 @@ function newlineBeforeCommaFixerRespectsComments(
191191
,
192192
$paramB=10 /* comment */
193193
,
194-
$paramC=20
194+
$paramC=20 # comment
195+
, $paramC=30
196+
, string $paramC='foo'
195197
) {}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,7 @@ function newlineBeforeCommaShouldBeFixedInOneGo(
167167
function newlineBeforeCommaFixerRespectsComments(
168168
$paramA, // comment
169169
$paramB=10, /* comment */
170-
$paramC=20
170+
$paramC=20, # comment
171+
$paramC=30,
172+
string $paramC='foo'
171173
) {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function getErrorList($testFile='')
8787
185 => 1,
8888
191 => 1,
8989
193 => 1,
90+
195 => 1,
91+
196 => 1,
9092
];
9193

9294
default:

0 commit comments

Comments
 (0)