Skip to content

Commit

Permalink
Do not attempt to fix ArrayDeclaration.SpaceBeforeComma if there is a…
Browse files Browse the repository at this point in the history
… comment between
  • Loading branch information
morozov committed Aug 31, 2020
1 parent d33a6a9 commit 42052ff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,14 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
$error = 'Expected 0 spaces before comma; %s found';
$data = [$spaceLength];

$fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($nextToken - 1), '');
// The error is only fixable if there is only whitespace between the tokens.
if ($prev === $phpcsFile->findPrevious(T_WHITESPACE, ($nextToken - 1), null, true)) {
$fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($nextToken - 1), '');
}
} else {
$phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma', $data);
}
}
}//end if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ $c];
['a' => $a, 'b' => $b,
'c' => $c];

[
'foo',
'bar'
// This is a non-fixable error.
,
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,13 @@ $foo = [
'c' => $c,
];

[
'foo',
'bar'
// This is a non-fixable error.
,
];

// Intentional syntax error.
$a = [
'a' =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ public function getErrorList($testFile='')
434 => 2,
436 => 2,
437 => 3,
443 => 1,
];
default:
return [];
Expand Down

0 comments on commit 42052ff

Please sign in to comment.