Skip to content

Commit 6804dbd

Browse files
committed
Merge branch 'issues/3065' of https://github.com/morozov/php-code-sniffer
2 parents 466297a + 42052ff commit 6804dbd

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-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.1.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,13 @@ array()
460460
(unset) array(),
461461
);
462462

463+
array(
464+
'foo',
465+
'bar'
466+
// This is a non-fixable error.
467+
,
468+
);
469+
463470
// Intentional syntax error.
464471
$a = array(
465472
'a' =>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,13 @@ array()
496496
(unset) array(),
497497
);
498498

499+
array(
500+
'foo',
501+
'bar'
502+
// This is a non-fixable error.
503+
,
504+
);
505+
499506
// Intentional syntax error.
500507
$a = array(
501508
'a' =>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,13 @@ $c];
449449
(unset) [],
450450
];
451451

452+
[
453+
'foo',
454+
'bar'
455+
// This is a non-fixable error.
456+
,
457+
];
458+
452459
// Intentional syntax error.
453460
$a = [
454461
'a' =>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,13 @@ $foo = [
483483
(unset) [],
484484
];
485485

486+
[
487+
'foo',
488+
'bar'
489+
// This is a non-fixable error.
490+
,
491+
];
492+
486493
// Intentional syntax error.
487494
$a = [
488495
'a' =>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function getErrorList($testFile='')
121121
445 => 2,
122122
447 => 2,
123123
448 => 3,
124+
467 => 1,
124125
];
125126
case 'ArrayDeclarationUnitTest.2.inc':
126127
return [
@@ -204,6 +205,7 @@ public function getErrorList($testFile='')
204205
434 => 2,
205206
436 => 2,
206207
437 => 3,
208+
456 => 1,
207209
];
208210
default:
209211
return [];

0 commit comments

Comments
 (0)