@@ -651,11 +651,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
651651 );
652652
653653 if ($ fix ) {
654- if (0 === $ found ) {
655- $ phpcsFile ->fixer ->addContent ($ index ['index ' ] - 1 , str_repeat (' ' , $ expected ));
656- } else {
657- $ phpcsFile ->fixer ->replaceToken ($ index ['index ' ] - 1 , str_repeat (' ' , $ expected ));
658- }
654+ $ this ->align ($ phpcsFile , $ index ['index ' ], $ expected , $ found );
659655 }
660656
661657 continue ;
@@ -679,20 +675,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
679675 );
680676
681677 if ($ fix ) {
682- if ('newline ' === $ found ) {
683- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , $ index ['value ' ] - 1 , null , true );
684- $ phpcsFile ->fixer ->beginChangeset ();
685- for ($ i = $ prev + 1 ; $ i < $ index ['value ' ]; $ i ++) {
686- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
687- }
688-
689- $ phpcsFile ->fixer ->replaceToken ($ index ['value ' ] - 1 , str_repeat (' ' , $ expected ));
690- $ phpcsFile ->fixer ->endChangeset ();
691- } elseif (0 === $ found ) {
692- $ phpcsFile ->fixer ->addContent ($ index ['arrow ' ] - 1 , str_repeat (' ' , $ expected ));
693- } else {
694- $ phpcsFile ->fixer ->replaceToken ($ index ['arrow ' ] - 1 , str_repeat (' ' , $ expected ));
695- }
678+ $ this ->align ($ phpcsFile , $ index ['arrow ' ], $ expected , $ found );
696679 }
697680
698681 continue ;
@@ -709,28 +692,40 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
709692
710693 $ fix = $ phpcsFile ->addFixableError (
711694 'Array value not aligned correctly; expected %s space(s) but found %s ' ,
712- $ index ['arrow ' ],
695+ $ index ['value ' ],
713696 'ValueNotAligned ' ,
714697 [$ expected , $ found ]
715698 );
716699
717700 if ($ fix ) {
718- if ('newline ' === $ found ) {
719- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , $ index ['value ' ] - 1 , null , true );
720- $ phpcsFile ->fixer ->beginChangeset ();
721- for ($ i = $ prev + 1 ; $ i < $ index ['value ' ]; $ i ++) {
722- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
723- }
724-
725- $ phpcsFile ->fixer ->replaceToken ($ index ['value ' ] - 1 , str_repeat (' ' , $ expected ));
726- $ phpcsFile ->fixer ->endChangeset ();
727- } elseif (0 === $ found ) {
728- $ phpcsFile ->fixer ->addContent ($ index ['value ' ] - 1 , str_repeat (' ' , $ expected ));
729- } else {
730- $ phpcsFile ->fixer ->replaceToken ($ index ['value ' ] - 1 , str_repeat (' ' , $ expected ));
731- }
701+ $ this ->align ($ phpcsFile , $ index ['value ' ], $ expected , $ found );
732702 }
733703 }
734704 }
735705 }
706+
707+ /**
708+ * @param File $phpcsFile
709+ * @param int $elementIndex
710+ * @param int $expected
711+ * @param int|string $found
712+ */
713+ private function align (File $ phpcsFile , int $ elementIndex , int $ expected , $ found ): void
714+ {
715+ if ('newline ' === $ found ) {
716+ $ phpcsFile ->fixer ->beginChangeset ();
717+
718+ $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , $ elementIndex - 1 , null , true );
719+ for ($ i = $ prev + 1 ; $ i < $ elementIndex ; $ i ++) {
720+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
721+ }
722+
723+ $ phpcsFile ->fixer ->replaceToken ($ elementIndex - 1 , str_repeat (' ' , $ expected ));
724+ $ phpcsFile ->fixer ->endChangeset ();
725+ } elseif (0 === $ found ) {
726+ $ phpcsFile ->fixer ->addContent ($ elementIndex - 1 , str_repeat (' ' , $ expected ));
727+ } else {
728+ $ phpcsFile ->fixer ->replaceToken ($ elementIndex - 1 , str_repeat (' ' , $ expected ));
729+ }
730+ }
736731}
0 commit comments