@@ -441,7 +441,6 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
441
441
&& $ tokens [$ prev ]['code ' ] !== T_END_NOWDOC )
442
442
|| $ tokens [($ nextToken - 1 )]['line ' ] === $ tokens [$ nextToken ]['line ' ]
443
443
) {
444
- $ content = $ tokens [($ nextToken - 2 )]['content ' ];
445
444
if ($ tokens [($ nextToken - 1 )]['content ' ] === $ phpcsFile ->eolChar ) {
446
445
$ spaceLength = 'newline ' ;
447
446
} else {
@@ -611,29 +610,40 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
611
610
$ phpcsFile ->recordMetric ($ stackPtr , 'Array end comma ' , 'yes ' );
612
611
}
613
612
614
- $ lastValueLine = false ;
615
- foreach ($ indices as $ value ) {
613
+ foreach ($ indices as $ valuePosition => $ value ) {
616
614
if (empty ($ value ['value ' ]) === true ) {
617
615
// Array was malformed and we couldn't figure out
618
616
// the array value correctly, so we have to ignore it.
619
617
// Other parts of this sniff will correct the error.
620
618
continue ;
621
619
}
622
620
623
- if ($ lastValueLine !== false && $ tokens [$ value ['value ' ]]['line ' ] === $ lastValueLine ) {
621
+ $ valuePointer = $ value ['value ' ];
622
+
623
+ $ previous = $ phpcsFile ->findPrevious ([T_WHITESPACE , T_COMMA ], ($ valuePointer - 1 ), ($ arrayStart + 1 ), true );
624
+ if ($ previous === false ) {
625
+ $ previous = $ stackPtr ;
626
+ }
627
+
628
+ $ previousIsWhitespace = $ tokens [($ valuePointer - 1 )]['code ' ] === T_WHITESPACE ;
629
+ if ($ tokens [$ previous ]['line ' ] === $ tokens [$ valuePointer ]['line ' ]) {
624
630
$ error = 'Each value in a multi-line array must be on a new line ' ;
625
- $ fix = $ phpcsFile ->addFixableError ($ error , $ value ['value ' ], 'ValueNoNewline ' );
631
+ if ($ valuePosition === 0 ) {
632
+ $ error = 'The first value in a multi-value array must be on a new line ' ;
633
+ }
634
+
635
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ valuePointer , 'ValueNoNewline ' );
626
636
if ($ fix === true ) {
627
- if ($ tokens [($ value ['value ' ] - 1 )]['code ' ] === T_WHITESPACE ) {
628
- $ phpcsFile ->fixer ->replaceToken (($ value ['value ' ] - 1 ), '' );
637
+ if ($ previousIsWhitespace === true ) {
638
+ $ phpcsFile ->fixer ->replaceToken (($ valuePointer - 1 ), $ phpcsFile ->eolChar );
639
+ } else {
640
+ $ phpcsFile ->fixer ->addNewlineBefore ($ valuePointer );
629
641
}
630
-
631
- $ phpcsFile ->fixer ->addNewlineBefore ($ value ['value ' ]);
632
642
}
633
- } else if ($ tokens [( $ value [ ' value ' ] - 1 )][ ' code ' ] === T_WHITESPACE ) {
643
+ } else if ($ previousIsWhitespace === true ) {
634
644
$ expected = $ keywordStart ;
635
645
636
- $ first = $ phpcsFile ->findFirstOnLine (T_WHITESPACE , $ value [ ' value ' ] , true );
646
+ $ first = $ phpcsFile ->findFirstOnLine (T_WHITESPACE , $ valuePointer , true );
637
647
$ found = ($ tokens [$ first ]['column ' ] - 1 );
638
648
if ($ found !== $ expected ) {
639
649
$ error = 'Array value not aligned correctly; expected %s spaces but found %s ' ;
@@ -642,18 +652,16 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
642
652
$ found ,
643
653
];
644
654
645
- $ fix = $ phpcsFile ->addFixableError ($ error , $ value [ ' value ' ] , 'ValueNotAligned ' , $ data );
655
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ valuePointer , 'ValueNotAligned ' , $ data );
646
656
if ($ fix === true ) {
647
657
if ($ found === 0 ) {
648
- $ phpcsFile ->fixer ->addContent (($ value [ ' value ' ] - 1 ), str_repeat (' ' , $ expected ));
658
+ $ phpcsFile ->fixer ->addContent (($ valuePointer - 1 ), str_repeat (' ' , $ expected ));
649
659
} else {
650
- $ phpcsFile ->fixer ->replaceToken (($ value [ ' value ' ] - 1 ), str_repeat (' ' , $ expected ));
660
+ $ phpcsFile ->fixer ->replaceToken (($ valuePointer - 1 ), str_repeat (' ' , $ expected ));
651
661
}
652
662
}
653
663
}
654
664
}//end if
655
-
656
- $ lastValueLine = $ tokens [$ value ['value ' ]]['line ' ];
657
665
}//end foreach
658
666
}//end if
659
667
@@ -684,82 +692,68 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
684
692
to be moved back one space however, then both errors would be fixed.
685
693
*/
686
694
687
- $ numValues = count ($ indices );
688
-
689
- $ indicesStart = ($ keywordStart + 1 );
690
- $ indexLine = $ tokens [$ stackPtr ]['line ' ];
691
- $ lastIndexLine = null ;
692
- foreach ($ indices as $ index ) {
693
- if ($ index ['value ' ] === false ) {
695
+ $ indicesStart = ($ keywordStart + 1 );
696
+ foreach ($ indices as $ valuePosition => $ index ) {
697
+ $ valuePointer = $ index ['value ' ];
698
+ if ($ valuePointer === false ) {
694
699
// Syntax error or live coding.
695
700
continue ;
696
701
}
697
702
698
703
if (isset ($ index ['index ' ]) === false ) {
699
704
// Array value only.
700
- if ($ tokens [$ index ['value ' ]]['line ' ] === $ tokens [$ stackPtr ]['line ' ] && $ numValues > 1 ) {
701
- $ error = 'The first value in a multi-value array must be on a new line ' ;
702
- $ fix = $ phpcsFile ->addFixableError ($ error , $ stackPtr , 'FirstValueNoNewline ' );
703
- if ($ fix === true ) {
704
- $ phpcsFile ->fixer ->addNewlineBefore ($ index ['value ' ]);
705
- }
706
- }
707
-
708
705
continue ;
709
706
}
710
707
711
- $ lastIndexLine = $ indexLine ;
712
- $ indexLine = $ tokens [$ index [ ' index ' ] ]['line ' ];
708
+ $ indexPointer = $ index [ ' index ' ] ;
709
+ $ indexLine = $ tokens [$ indexPointer ]['line ' ];
713
710
714
- if ($ indexLine === $ tokens [$ stackPtr ]['line ' ]) {
715
- $ error = 'The first index in a multi-value array must be on a new line ' ;
716
- $ fix = $ phpcsFile ->addFixableError ($ error , $ index ['index ' ], 'FirstIndexNoNewline ' );
717
- if ($ fix === true ) {
718
- $ phpcsFile ->fixer ->addNewlineBefore ($ index ['index ' ]);
719
- }
720
-
721
- continue ;
711
+ $ previous = $ phpcsFile ->findPrevious ([T_WHITESPACE , T_COMMA ], ($ indexPointer - 1 ), ($ arrayStart + 1 ), true );
712
+ if ($ previous === false ) {
713
+ $ previous = $ stackPtr ;
722
714
}
723
715
724
- if ($ indexLine === $ lastIndexLine ) {
716
+ if ($ tokens [ $ previous ][ ' line ' ] === $ indexLine ) {
725
717
$ error = 'Each index in a multi-line array must be on a new line ' ;
726
- $ fix = $ phpcsFile ->addFixableError ($ error , $ index ['index ' ], 'IndexNoNewline ' );
718
+ if ($ valuePosition === 0 ) {
719
+ $ error = 'The first index in a multi-value array must be on a new line ' ;
720
+ }
721
+
722
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ indexPointer , 'IndexNoNewline ' );
727
723
if ($ fix === true ) {
728
- if ($ tokens [($ index ['index ' ] - 1 )]['code ' ] === T_WHITESPACE ) {
729
- $ phpcsFile ->fixer ->replaceToken (($ index ['index ' ] - 1 ), '' );
724
+ if ($ tokens [($ indexPointer - 1 )]['code ' ] === T_WHITESPACE ) {
725
+ $ phpcsFile ->fixer ->replaceToken (($ indexPointer - 1 ), $ phpcsFile ->eolChar );
726
+ } else {
727
+ $ phpcsFile ->fixer ->addNewlineBefore ($ indexPointer );
730
728
}
731
-
732
- $ phpcsFile ->fixer ->addNewlineBefore ($ index ['index ' ]);
733
729
}
734
730
735
731
continue ;
736
732
}
737
733
738
- if ($ tokens [$ index ['index ' ]]['column ' ] !== $ indicesStart
739
- && ($ index ['index ' ] - 1 ) !== $ arrayStart
740
- ) {
734
+ if ($ tokens [$ indexPointer ]['column ' ] !== $ indicesStart && ($ indexPointer - 1 ) !== $ arrayStart ) {
741
735
$ expected = ($ indicesStart - 1 );
742
- $ found = ($ tokens [$ index [ ' index ' ] ]['column ' ] - 1 );
736
+ $ found = ($ tokens [$ indexPointer ]['column ' ] - 1 );
743
737
$ error = 'Array key not aligned correctly; expected %s spaces but found %s ' ;
744
738
$ data = [
745
739
$ expected ,
746
740
$ found ,
747
741
];
748
742
749
- $ fix = $ phpcsFile ->addFixableError ($ error , $ index [ ' index ' ] , 'KeyNotAligned ' , $ data );
743
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ indexPointer , 'KeyNotAligned ' , $ data );
750
744
if ($ fix === true ) {
751
- if ($ found === 0 || $ tokens [($ index [ ' index ' ] - 1 )]['code ' ] !== T_WHITESPACE ) {
752
- $ phpcsFile ->fixer ->addContent (($ index [ ' index ' ] - 1 ), str_repeat (' ' , $ expected ));
745
+ if ($ found === 0 || $ tokens [($ indexPointer - 1 )]['code ' ] !== T_WHITESPACE ) {
746
+ $ phpcsFile ->fixer ->addContent (($ indexPointer - 1 ), str_repeat (' ' , $ expected ));
753
747
} else {
754
- $ phpcsFile ->fixer ->replaceToken (($ index [ ' index ' ] - 1 ), str_repeat (' ' , $ expected ));
748
+ $ phpcsFile ->fixer ->replaceToken (($ indexPointer - 1 ), str_repeat (' ' , $ expected ));
755
749
}
756
750
}
757
751
}
758
752
759
- $ arrowStart = ($ tokens [$ index [ ' index ' ] ]['column ' ] + $ maxLength + 1 );
753
+ $ arrowStart = ($ tokens [$ indexPointer ]['column ' ] + $ maxLength + 1 );
760
754
if ($ tokens [$ index ['arrow ' ]]['column ' ] !== $ arrowStart ) {
761
- $ expected = ($ arrowStart - ($ index ['index_length ' ] + $ tokens [$ index [ ' index ' ] ]['column ' ]));
762
- $ found = ($ tokens [$ index ['arrow ' ]]['column ' ] - ($ index ['index_length ' ] + $ tokens [$ index [ ' index ' ] ]['column ' ]));
755
+ $ expected = ($ arrowStart - ($ index ['index_length ' ] + $ tokens [$ indexPointer ]['column ' ]));
756
+ $ found = ($ tokens [$ index ['arrow ' ]]['column ' ] - ($ index ['index_length ' ] + $ tokens [$ indexPointer ]['column ' ]));
763
757
$ error = 'Array double arrow not aligned correctly; expected %s space(s) but found %s ' ;
764
758
$ data = [
765
759
$ expected ,
@@ -779,9 +773,9 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
779
773
}
780
774
781
775
$ valueStart = ($ arrowStart + 3 );
782
- if ($ tokens [$ index [ ' value ' ] ]['column ' ] !== $ valueStart ) {
776
+ if ($ tokens [$ valuePointer ]['column ' ] !== $ valueStart ) {
783
777
$ expected = ($ valueStart - ($ tokens [$ index ['arrow ' ]]['length ' ] + $ tokens [$ index ['arrow ' ]]['column ' ]));
784
- $ found = ($ tokens [$ index [ ' value ' ] ]['column ' ] - ($ tokens [$ index ['arrow ' ]]['length ' ] + $ tokens [$ index ['arrow ' ]]['column ' ]));
778
+ $ found = ($ tokens [$ valuePointer ]['column ' ] - ($ tokens [$ index ['arrow ' ]]['length ' ] + $ tokens [$ index ['arrow ' ]]['column ' ]));
785
779
if ($ found < 0 ) {
786
780
$ found = 'newline ' ;
787
781
}
@@ -795,25 +789,24 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
795
789
$ fix = $ phpcsFile ->addFixableError ($ error , $ index ['arrow ' ], 'ValueNotAligned ' , $ data );
796
790
if ($ fix === true ) {
797
791
if ($ found === 'newline ' ) {
798
- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ index [ ' value ' ] - 1 ), null , true );
792
+ $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ valuePointer - 1 ), null , true );
799
793
$ phpcsFile ->fixer ->beginChangeset ();
800
- for ($ i = ($ prev + 1 ); $ i < $ index [ ' value ' ] ; $ i ++) {
794
+ for ($ i = ($ prev + 1 ); $ i < $ valuePointer ; $ i ++) {
801
795
$ phpcsFile ->fixer ->replaceToken ($ i , '' );
802
796
}
803
797
804
- $ phpcsFile ->fixer ->replaceToken (($ index [ ' value ' ] - 1 ), str_repeat (' ' , $ expected ));
798
+ $ phpcsFile ->fixer ->replaceToken (($ valuePointer - 1 ), str_repeat (' ' , $ expected ));
805
799
$ phpcsFile ->fixer ->endChangeset ();
806
800
} else if ($ found === 0 ) {
807
- $ phpcsFile ->fixer ->addContent (($ index [ ' value ' ] - 1 ), str_repeat (' ' , $ expected ));
801
+ $ phpcsFile ->fixer ->addContent (($ valuePointer - 1 ), str_repeat (' ' , $ expected ));
808
802
} else {
809
- $ phpcsFile ->fixer ->replaceToken (($ index [ ' value ' ] - 1 ), str_repeat (' ' , $ expected ));
803
+ $ phpcsFile ->fixer ->replaceToken (($ valuePointer - 1 ), str_repeat (' ' , $ expected ));
810
804
}
811
805
}
812
806
}//end if
813
807
814
808
// Check each line ends in a comma.
815
- $ valueStart = $ index ['value ' ];
816
- $ valueLine = $ tokens [$ index ['value ' ]]['line ' ];
809
+ $ valueStart = $ valuePointer ;
817
810
$ nextComma = false ;
818
811
819
812
$ end = $ phpcsFile ->findEndOfStatement ($ valueStart );
@@ -837,11 +830,11 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
837
830
838
831
if ($ nextComma === false || ($ tokens [$ nextComma ]['line ' ] !== $ valueLine )) {
839
832
$ error = 'Each line in an array declaration must end in a comma ' ;
840
- $ fix = $ phpcsFile ->addFixableError ($ error , $ index [ ' value ' ] , 'NoComma ' );
833
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ valuePointer , 'NoComma ' );
841
834
842
835
if ($ fix === true ) {
843
836
// Find the end of the line and put a comma there.
844
- for ($ i = ($ index [ ' value ' ] + 1 ); $ i <= $ arrayEnd ; $ i ++) {
837
+ for ($ i = ($ valuePointer + 1 ); $ i <= $ arrayEnd ; $ i ++) {
845
838
if ($ tokens [$ i ]['line ' ] > $ valueLine ) {
846
839
break ;
847
840
}
0 commit comments