@@ -651,11 +651,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
651
651
);
652
652
653
653
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 );
659
655
}
660
656
661
657
continue ;
@@ -679,20 +675,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
679
675
);
680
676
681
677
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 );
696
679
}
697
680
698
681
continue ;
@@ -709,28 +692,40 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $start
709
692
710
693
$ fix = $ phpcsFile ->addFixableError (
711
694
'Array value not aligned correctly; expected %s space(s) but found %s ' ,
712
- $ index ['arrow ' ],
695
+ $ index ['value ' ],
713
696
'ValueNotAligned ' ,
714
697
[$ expected , $ found ]
715
698
);
716
699
717
700
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 );
732
702
}
733
703
}
734
704
}
735
705
}
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
+ }
736
731
}
0 commit comments