Skip to content

Commit

Permalink
fix up! Squiz/FunctionDeclarationArgumentSpacing: improve `SpaceBefor…
Browse files Browse the repository at this point in the history
…eComma` fixer
  • Loading branch information
jrfnl committed Jan 17, 2025
1 parent 1169e38 commit ec5bd51
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,34 @@ public function processBracket($phpcsFile, $openBracket)

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

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

for ($i = ($commaToken - 1); $tokens[$i]['code'] === T_WHITESPACE; $i--) {
$phpcsFile->fixer->replaceToken($i, '');
}
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, '');
}

if ($tokens[$i]['code'] === T_COMMENT
&& substr($tokens[$i]['content'], -1) === $phpcsFile->eolChar
) {
$phpcsFile->fixer->replaceToken($i, substr($tokens[$i]['content'], 0, -1));
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,7 @@ function newlineBeforeCommaFixerRespectsComments(
,
$paramB=10 /* comment */
,
$paramC=20
$paramC=20 # comment
, $paramC=30
, string $paramC='foo'
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,7 @@ function newlineBeforeCommaShouldBeFixedInOneGo(
function newlineBeforeCommaFixerRespectsComments(
$paramA, // comment
$paramB=10, /* comment */
$paramC=20
$paramC=20, # comment
$paramC=30,
string $paramC='foo'
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function getErrorList($testFile='')
185 => 1,
191 => 1,
193 => 1,
195 => 1,
196 => 1,
];

default:
Expand Down

0 comments on commit ec5bd51

Please sign in to comment.