Skip to content

Commit 42052ff

Browse files
committed
Do not attempt to fix ArrayDeclaration.SpaceBeforeComma if there is a comment between
1 parent d33a6a9 commit 42052ff

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,14 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
450450
$error = 'Expected 0 spaces before comma; %s found';
451451
$data = [$spaceLength];
452452

453-
$fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data);
454-
if ($fix === true) {
455-
$phpcsFile->fixer->replaceToken(($nextToken - 1), '');
453+
// The error is only fixable if there is only whitespace between the tokens.
454+
if ($prev === $phpcsFile->findPrevious(T_WHITESPACE, ($nextToken - 1), null, true)) {
455+
$fix = $phpcsFile->addFixableError($error, $nextToken, 'SpaceBeforeComma', $data);
456+
if ($fix === true) {
457+
$phpcsFile->fixer->replaceToken(($nextToken - 1), '');
458+
}
459+
} else {
460+
$phpcsFile->addError($error, $nextToken, 'SpaceBeforeComma', $data);
456461
}
457462
}
458463
}//end if

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ $c];
436436
['a' => $a, 'b' => $b,
437437
'c' => $c];
438438

439+
[
440+
'foo',
441+
'bar'
442+
// This is a non-fixable error.
443+
,
444+
];
445+
439446
// Intentional syntax error.
440447
$a = [
441448
'a' =>

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,13 @@ $foo = [
470470
'c' => $c,
471471
];
472472

473+
[
474+
'foo',
475+
'bar'
476+
// This is a non-fixable error.
477+
,
478+
];
479+
473480
// Intentional syntax error.
474481
$a = [
475482
'a' =>

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public function getErrorList($testFile='')
204204
434 => 2,
205205
436 => 2,
206206
437 => 3,
207+
443 => 1,
207208
];
208209
default:
209210
return [];

0 commit comments

Comments
 (0)