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