Skip to content

Commit 30319c4

Browse files
committed
Squiz/FunctionDeclarationArgumentSpacing: improve SpacingAfterReference fixer
The fixer for the `SpacingAfterReference` 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 after a reference token in one go. For the test code sample added in the previous commit, this means the difference between the fixer needing 5 loops (including one skipped loop due to possible conflicts) versus 2 loops. <details> <summary>Old:</summary> ``` => Fixing file: 1/1 violations remaining Squiz.Functions.FunctionDeclarationArgumentSpacing:164 replaced token 9 (T_WHITESPACE on line 4) "\n\n" => "\n" => Fixing file: 1/1 violations remaining [made 1 pass]... * fixed 1 violations, starting loop 2 * Squiz.Functions.FunctionDeclarationArgumentSpacing:164 replaced token 9 (T_WHITESPACE on line 4) "\n " => " " => Fixing file: 1/1 violations remaining [made 2 passes]... * fixed 1 violations, starting loop 3 * **** Squiz.Functions.FunctionDeclarationArgumentSpacing:164 has possible conflict with another sniff on loop 1; caused by the following change **** **** replaced token 9 (T_WHITESPACE on line 4) " $param" => "$param" **** **** ignoring all changes until next loop **** => Fixing file: 0/1 violations remaining [made 3 passes]... * fixed 0 violations, starting loop 4 * Squiz.Functions.FunctionDeclarationArgumentSpacing:164 replaced token 9 (T_WHITESPACE on line 4) " $param" => "$param" => Fixing file: 1/1 violations remaining [made 4 passes]... * fixed 1 violations, starting loop 5 * => Fixing file: 0/1 violations remaining [made 5 passes]... ``` </details> <details> <summary>New:</summary> ``` => Fixing file: 1/1 violations remaining => Changeset started by Squiz.Functions.FunctionDeclarationArgumentSpacing:164 Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:166 replaced token 9 (T_WHITESPACE on line 4) "\n \n" => " \n" Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:166 replaced token 10 (T_WHITESPACE on line 5) " \n " => " " Q: Squiz.Functions.FunctionDeclarationArgumentSpacing:166 replaced token 11 (T_WHITESPACE on line 6) " $param" => "$param" A: Squiz.Functions.FunctionDeclarationArgumentSpacing:169 replaced token 9 (T_WHITESPACE on line 4) "\n \n" => " \n" A: Squiz.Functions.FunctionDeclarationArgumentSpacing:169 replaced token 10 (T_WHITESPACE on line 5) " \n " => " " A: Squiz.Functions.FunctionDeclarationArgumentSpacing:169 replaced token 11 (T_WHITESPACE on line 6) " $param" => "$param" => Changeset ended: 3 changes applied => Fixing file: 3/1 violations remaining [made 1 pass]... * fixed 3 violations, starting loop 2 * => Fixing file: 0/1 violations remaining [made 2 passes]... ``` </details>
1 parent 399b1b3 commit 30319c4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,14 @@ public function processBracket($phpcsFile, $openBracket)
162162
];
163163
$fix = $phpcsFile->addFixableError($error, $refToken, 'SpacingAfterReference', $data);
164164
if ($fix === true) {
165-
$phpcsFile->fixer->replaceToken(($refToken + 1), '');
165+
$phpcsFile->fixer->beginChangeset();
166+
for ($i = ($refToken + 1); $tokens[$i]['code'] === T_WHITESPACE; $i++) {
167+
$phpcsFile->fixer->replaceToken($i, '');
168+
}
169+
170+
$phpcsFile->fixer->endChangeset();
166171
}
167-
}
172+
}//end if
168173
}//end if
169174

170175
if ($param['variable_length'] === true) {

0 commit comments

Comments
 (0)