Skip to content

Commit

Permalink
Squiz/FunctionDeclarationArgumentSpacing: improve SpaceBeforeComma
Browse files Browse the repository at this point in the history
…fixer

The fixer for the `SpaceBeforeComma` error code would only handle one whitespace token at a time, which is inefficient and could also lead to more complex fixer conflicts.

This commit changes the fixer to handle all whitespace tokens in one go.

For the test code sample added in the previous commit, this means the difference between the fixer needing 4 loops versus 2 loops.

Additionally, this commit updates the fixer to cleanly handle moving the comma in the case of a trailing comment between the end of the previous parameter and the comma.

Tested via the `newlineBeforeCommaShouldBeFixedInOneGo()` test case and the new `newlineBeforeCommaFixerRespectsComments()` test case.
  • Loading branch information
jrfnl committed Jan 23, 2025
1 parent 93b7e2d commit 0e0e0fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,35 @@ public function processBracket($phpcsFile, $openBracket)

$fix = $phpcsFile->addFixableError($error, $commaToken, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($commaToken - 1), '');
}
}
$startOfCurrentParam = $phpcsFile->findNext(Tokens::$emptyTokens, ($commaToken + 1), null, true);

$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($endOfPreviousParam, ',');
$phpcsFile->fixer->replaceToken($commaToken, '');

if ($tokens[$commaToken]['line'] === $tokens[$startOfCurrentParam]['line']) {
for ($i = ($commaToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
} else {
for ($i = ($commaToken - 1);
$tokens[$i]['code'] === T_WHITESPACE && $tokens[$endOfPreviousParam]['line'] !== $tokens[$i]['line'];
$i--
) {
$phpcsFile->fixer->replaceToken($i, '');
}

for ($i = ($commaToken + 1);
$tokens[$i]['code'] === T_WHITESPACE && $tokens[$commaToken]['line'] === $tokens[$i]['line'];
$i++
) {
$phpcsFile->fixer->replaceToken($i, '');
}
}

$phpcsFile->fixer->endChangeset();
}//end if
}//end if

// Don't check spacing after the comma if it is the last content on the line.
$checkComma = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,13 @@ function newlineBeforeCommaShouldBeFixedInOneGo(
,
$paramC
) {}

function newlineBeforeCommaFixerRespectsComments(
$paramA // comment
,
$paramB=10 /* comment */
,
$paramC=20 # comment
, $paramC=30
, string $paramC='foo'
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,11 @@ function newlineBeforeCommaShouldBeFixedInOneGo(
$paramB,
$paramC
) {}

function newlineBeforeCommaFixerRespectsComments(
$paramA, // comment
$paramB=10, /* comment */
$paramC=20, # comment
$paramC=30,
string $paramC='foo'
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function getErrorList()
174 => 2,
182 => 1,
185 => 1,
191 => 1,
193 => 1,
195 => 1,
196 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 0e0e0fb

Please sign in to comment.