diff --git a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index efad97e4b3..f1a9095470 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -16,6 +16,15 @@ class ArrayDeclarationSniff implements Sniff { + /** + * Controls how to align the double arrow in multi-line arrays: + * - true: align the double arrow to the longest index + * - false: align the double arrow to length of the index + * + * @var boolean + */ + public bool $alignDoubleArrowToLongestIndex = true; + /** * Returns an array of tokens this test wants to listen for. @@ -823,7 +832,12 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array } }//end if - $arrowStart = ($tokens[$indexPointer]['column'] + $maxLength + 1); + $arrowStart = ($tokens[$indexPointer]['column'] + $index['index_length'] + 1); + + if ($this->alignDoubleArrowToLongestIndex === true) { + $arrowStart = ($tokens[$indexPointer]['column'] + $maxLength + 1); + } + if ($tokens[$index['arrow']]['column'] !== $arrowStart) { $expected = ($arrowStart - ($index['index_length'] + $tokens[$indexPointer]['column'])); $found = ($tokens[$index['arrow']]['column'] - ($index['index_length'] + $tokens[$indexPointer]['column'])); @@ -847,11 +861,9 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array $phpcsFile->fixer->replaceToken(($index['arrow'] - 1), str_repeat(' ', $expected)); } } - - continue; }//end if - $valueStart = ($arrowStart + 3); + $valueStart = ($tokens[$index['arrow']]['column'] + 3); if ($tokens[$valuePointer]['column'] !== $valueStart) { $expected = ($valueStart - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column'])); $found = ($tokens[$valuePointer]['column'] - ($tokens[$index['arrow']]['length'] + $tokens[$index['arrow']]['column']));