From 42052ffee3968a5e80247fb40f059401046e3f8a Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Mon, 31 Aug 2020 18:02:00 -0700 Subject: [PATCH] Do not attempt to fix ArrayDeclaration.SpaceBeforeComma if there is a comment between --- .../Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php | 11 ++++++++--- .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc | 7 +++++++ .../Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed | 7 +++++++ .../Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index de5ee8535d..cb19db696c 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -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 diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc index 60ff35d64c..8e93bf47e1 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc @@ -436,6 +436,13 @@ $c]; ['a' => $a, 'b' => $b, 'c' => $c]; +[ + 'foo', + 'bar' + // This is a non-fixable error. + , +]; + // Intentional syntax error. $a = [ 'a' => diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed index 1d17fa0c4d..b81e1face0 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed @@ -470,6 +470,13 @@ $foo = [ 'c' => $c, ]; +[ + 'foo', + 'bar' + // This is a non-fixable error. + , +]; + // Intentional syntax error. $a = [ 'a' => diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php index 818b1b5562..00dd939b85 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php @@ -204,6 +204,7 @@ public function getErrorList($testFile='') 434 => 2, 436 => 2, 437 => 3, + 443 => 1, ]; default: return [];