diff --git a/Sniffs/Arrays/ArrayDeclarationSniff.php b/Sniffs/Arrays/ArrayDeclarationSniff.php index b7f3bcc..a7fa367 100644 --- a/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -544,9 +544,9 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr // } // // if ($singleValue === true) { - // Array cannot be empty, so this is a multi-line array with - // a single value. It should be defined on single line. - $error = 'Multi-line array contains a single value; use single-line array instead'; + // Array cannot be empty, so this is a multi-line array with + // a single value. It should be defined on single line. + $error = 'Multi-line array contains a single value; use single-line array instead'; // $fix = $phpcsFile->addFixableError($error, $stackPtr, 'MultiLineNotAllowed'); // // if ($fix === true) { @@ -635,7 +635,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr // $phpcsFile->fixer->addNewlineBefore($value['value']); // } // } else - if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { + if ($tokens[($value['value'] - 1)]['code'] === T_WHITESPACE) { $expected = $statementStart + 4; $first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $value['value'], true); @@ -812,89 +812,57 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr // } // }//end if //ongr end - // Check each line ends in a comma or dot (string concat). + // Check each line ends in a comma. // if ($tokens[$index['value']]['code'] !== T_ARRAY // && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY // ) { - $valueLine = $tokens[$index['value']]['line']; - $nextComma = false; - $nextDot = false; - for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) { - // Skip bracketed statements, like function calls. - if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { - $i = $tokens[$i]['parenthesis_closer']; - $valueLine = $tokens[$i]['line']; - continue; - } + $valueLine = $tokens[$index['value']]['line']; + $nextComma = false; + for ($i = ($index['value'] + 1); $i < $arrayEnd; $i++) { + // Skip bracketed statements, like function calls. + if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) { + $i = $tokens[$i]['parenthesis_closer']; + $valueLine = $tokens[$i]['line']; + continue; + } - if ($tokens[$i]['code'] === T_COMMA) { - $nextComma = $i; - break; - } - else if($tokens[$i]['code'] === T_STRING_CONCAT) { - $nextDot = $i; - break; - } + if ($tokens[$i]['code'] === T_COMMA) { + $nextComma = $i; + break; } + } - if (($nextComma === false && $nextDot === false) - || ($tokens[$nextComma]['line'] !== $valueLine && $tokens[$nextDot]['line'] !== $valueLine) - && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY - ) { - $error = 'Each line in an array declaration must end in a comma'; - $fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma'); + if (($nextComma === false) || ($tokens[$nextComma]['line'] !== $valueLine) && $tokens[$index['value']]['code'] !== T_OPEN_SHORT_ARRAY) { + $error = 'Each line in an array declaration must end in a comma'; + $fix = $phpcsFile->addFixableError($error, $index['value'], 'NoComma'); - if ($fix === true) { - // Find the end of the line and put a comma there. - for ($i = ($index['value'] + 1); $i < $phpcsFile->numTokens; $i++) { - if ($tokens[$i]['line'] > $tokens[$index['value']]['line']) { - break; - } + if ($fix === true) { + // Find the end of the line and put a comma there. + for ($i = ($index['value'] + 1); $i < $phpcsFile->numTokens; $i++) { + if ($tokens[$i]['line'] > $tokens[$index['value']]['line']) { + break; } - - $phpcsFile->fixer->addContentBefore(($i - 1), ','); } - } - - // Check that there is no space before the comma. - if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) { - $content = $tokens[($nextComma - 2)]['content']; - $spaceLength = $tokens[($nextComma - 1)]['length']; - $error = 'Expected 0 spaces between "%s" and comma; %s found'; - $data = array( - $content, - $spaceLength, - ); - $fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data); - if ($fix === true) { - $phpcsFile->fixer->replaceToken(($nextComma - 1), ''); - } + $phpcsFile->fixer->addContentBefore(($i - 1), ','); } + } - if ($nextDot !== false) { - $spacesExpected = $this->getStatementStartColumn($phpcsFile, $nextDot) + 3; - - $indentation = $nextDot; - while($tokens[$nextDot]['line'] == $tokens[$indentation]['line']) { - $indentation++; - } - - $spacesFound = 0; - if ($tokens[$indentation]['code'] == T_WHITESPACE) { - $spacesFound = $tokens[$indentation]['length']; - } - - if ($spacesExpected != $spacesFound) { - $error = 'Array value not aligned correctly; expected %s spaces but found %s'; - $data = array( - $spacesExpected, - $spacesFound, - ); + // Check that there is no space before the comma. + if ($nextComma !== false && $tokens[($nextComma - 1)]['code'] === T_WHITESPACE) { + $content = $tokens[($nextComma - 2)]['content']; + $spaceLength = $tokens[($nextComma - 1)]['length']; + $error = 'Expected 0 spaces between "%s" and comma; %s found'; + $data = array( + $content, + $spaceLength, + ); - $phpcsFile->addError($error, $indentation, 'ValueNotAligned', $data); - } + $fix = $phpcsFile->addFixableError($error, $nextComma, 'SpaceBeforeComma', $data); + if ($fix === true) { + $phpcsFile->fixer->replaceToken(($nextComma - 1), ''); } + } // }//end if }//end foreach diff --git a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php index 69e0b17..fcf74d8 100644 --- a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php +++ b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.php @@ -60,7 +60,6 @@ protected function getErrorList() 135 => 1, 137 => 1, 148 => 1, - 153 => 1, ]; } diff --git a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest index 78dd84f..e527053 100644 --- a/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest +++ b/Tests/Unit/Arrays/ArrayDeclarationSniffTest.phptest @@ -148,12 +148,3 @@ $data = [ 2 => test(2, 3) , ]; -$data = [ - 0 => 'foo' . - 'bar', -]; - -$data = [ - 0 => 'foo' . - 'bar', -];