@@ -12,6 +12,11 @@ class Symfony3Custom_Sniffs_Arrays_ArrayDeclarationSniff implements PHP_CodeSnif
12
12
*/
13
13
public $ indent = 4 ;
14
14
15
+ /**
16
+ * @var bool
17
+ */
18
+ public $ ignoreNewLine = true ;
19
+
15
20
/**
16
21
* Returns an array of tokens this test wants to listen for.
17
22
*
@@ -23,7 +28,6 @@ public function register()
23
28
T_ARRAY ,
24
29
T_OPEN_SHORT_ARRAY ,
25
30
);
26
-
27
31
}
28
32
29
33
/**
@@ -294,7 +298,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
294
298
if ($ fix === true ) {
295
299
$ phpcsFile ->fixer ->addNewlineBefore ($ arrayEnd );
296
300
}
297
- } else if ($ tokens [$ arrayEnd ]['column ' ] !== $ currentIndent + 1 ) {
301
+ } elseif ($ tokens [$ arrayEnd ]['column ' ] !== $ currentIndent + 1 ) {
298
302
// Check the closing bracket is lined up under the "a" in array.
299
303
$ expected = ($ currentIndent );
300
304
$ found = ($ tokens [$ arrayEnd ]['column ' ] - 1 );
@@ -348,7 +352,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
348
352
349
353
if ($ tokens [$ nextToken ]['code ' ] === T_ARRAY ) {
350
354
$ nextToken = $ tokens [$ tokens [$ nextToken ]['parenthesis_opener ' ]]['parenthesis_closer ' ];
351
- } else if ($ tokens [$ nextToken ]['code ' ] === T_OPEN_SHORT_ARRAY ) {
355
+ } elseif ($ tokens [$ nextToken ]['code ' ] === T_OPEN_SHORT_ARRAY ) {
352
356
$ nextToken = $ tokens [$ nextToken ]['bracket_closer ' ];
353
357
} else {
354
358
// T_CLOSURE.
@@ -398,6 +402,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
398
402
if ($ keyUsed === true && $ tokens [$ lastToken ]['code ' ] === T_COMMA ) {
399
403
$ error = 'No key specified for array entry; first entry specifies key ' ;
400
404
$ phpcsFile ->addError ($ error , $ nextToken , 'NoKeySpecified ' );
405
+
401
406
return ;
402
407
}
403
408
@@ -441,6 +446,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
441
446
if ($ singleUsed === true ) {
442
447
$ error = 'Key specified for array entry; first entry has no key ' ;
443
448
$ phpcsFile ->addError ($ error , $ nextToken , 'KeySpecified ' );
449
+
444
450
return ;
445
451
}
446
452
@@ -456,7 +462,10 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
456
462
$ currentEntry ['index_content ' ] = $ tokens [$ indexEnd ]['content ' ];
457
463
} else {
458
464
$ currentEntry ['index ' ] = $ indexStart ;
459
- $ currentEntry ['index_content ' ] = $ phpcsFile ->getTokensAsString ($ indexStart , ($ indexEnd - $ indexStart + 1 ));
465
+ $ currentEntry ['index_content ' ] = $ phpcsFile ->getTokensAsString (
466
+ $ indexStart ,
467
+ ($ indexEnd - $ indexStart + 1 )
468
+ );
460
469
}
461
470
462
471
$ indexLength = strlen ($ currentEntry ['index_content ' ]);
@@ -530,7 +539,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
530
539
531
540
$ phpcsFile ->fixer ->addNewlineBefore ($ value ['value ' ]);
532
541
}
533
- } else if ($ tokens [($ value ['value ' ] - 1 )]['code ' ] === T_WHITESPACE ) {
542
+ } elseif ($ tokens [($ value ['value ' ] - 1 )]['code ' ] === T_WHITESPACE ) {
534
543
$ expected = $ currentIndent + $ this ->indent ;
535
544
536
545
$ first = $ phpcsFile ->findFirstOnLine (T_WHITESPACE , $ value ['value ' ], true );
@@ -641,7 +650,13 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
641
650
$ found ,
642
651
);
643
652
644
- $ fix = $ phpcsFile ->addFixableError ($ error , $ index ['index ' ], 'KeyNotAligned ' , $ data );
653
+ if ($ found < 0 ) {
654
+ $ phpcsFile ->addError ($ error , $ index ['index ' ], 'KeyNotAligned ' , $ data );
655
+ $ fix = false ;
656
+ } else {
657
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ index ['index ' ], 'KeyNotAligned ' , $ data );
658
+ }
659
+
645
660
if ($ fix === true ) {
646
661
if ($ found === 0 ) {
647
662
$ phpcsFile ->fixer ->addContent (($ index ['index ' ] - 1 ), str_repeat (' ' , $ expected ));
@@ -681,30 +696,32 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
681
696
$ found = 'newline ' ;
682
697
}
683
698
684
- $ error = 'Array value not aligned correctly; expected %s space(s) but found %s ' ;
685
- $ data = array (
686
- $ expected ,
687
- $ found ,
688
- );
699
+ if ($ found !== 'newline ' || $ this ->ignoreNewLine === false ) {
700
+ $ error = 'Array value not aligned correctly; expected %s space(s) but found %s ' ;
701
+ $ data = [
702
+ $ expected ,
703
+ $ found ,
704
+ ];
689
705
690
- $ fix = $ phpcsFile ->addFixableError ($ error , $ index ['arrow ' ], 'ValueNotAligned ' , $ data );
691
- if ($ fix === true ) {
692
- if ($ found === 'newline ' ) {
693
- $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ index ['value ' ] - 1 ), null , true );
694
- $ phpcsFile ->fixer ->beginChangeset ();
695
- for ($ i = ($ prev + 1 ); $ i < $ index ['value ' ]; $ i ++) {
696
- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
697
- }
706
+ $ fix = $ phpcsFile ->addFixableError ($ error , $ index ['arrow ' ], 'ValueNotAligned ' , $ data );
707
+ if ($ fix === true ) {
708
+ if ($ found === 'newline ' ) {
709
+ $ prev = $ phpcsFile ->findPrevious (T_WHITESPACE , ($ index ['value ' ] - 1 ), null , true );
710
+ $ phpcsFile ->fixer ->beginChangeset ();
711
+ for ($ i = ($ prev + 1 ); $ i < $ index ['value ' ]; $ i ++) {
712
+ $ phpcsFile ->fixer ->replaceToken ($ i , '' );
713
+ }
698
714
699
- $ phpcsFile ->fixer ->replaceToken (($ index ['value ' ] - 1 ), str_repeat (' ' , $ expected ));
700
- $ phpcsFile ->fixer ->endChangeset ();
701
- } else if ($ found === 0 ) {
702
- $ phpcsFile ->fixer ->addContent (($ index ['value ' ] - 1 ), str_repeat (' ' , $ expected ));
703
- } else {
704
- $ phpcsFile ->fixer ->replaceToken (($ index ['value ' ] - 1 ), str_repeat (' ' , $ expected ));
715
+ $ phpcsFile ->fixer ->replaceToken (($ index ['value ' ] - 1 ), str_repeat (' ' , $ expected ));
716
+ $ phpcsFile ->fixer ->endChangeset ();
717
+ } elseif ($ found === 0 ) {
718
+ $ phpcsFile ->fixer ->addContent (($ index ['value ' ] - 1 ), str_repeat (' ' , $ expected ));
719
+ } else {
720
+ $ phpcsFile ->fixer ->replaceToken (($ index ['value ' ] - 1 ), str_repeat (' ' , $ expected ));
721
+ }
705
722
}
706
723
}
707
- }//end if
724
+ }
708
725
709
726
// Check each line ends in a comma.
710
727
$ valueLine = $ tokens [$ index ['value ' ]]['line ' ];
@@ -747,7 +764,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
747
764
$ nextComma = $ i ;
748
765
break ;
749
766
}
750
- }//end for
767
+ }
751
768
752
769
if ($ nextComma === false || ($ tokens [$ nextComma ]['line ' ] !== $ valueLine )) {
753
770
$ error = 'Each line in an array declaration must end in a comma ' ;
@@ -780,9 +797,6 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
780
797
$ phpcsFile ->fixer ->replaceToken (($ nextComma - 1 ), '' );
781
798
}
782
799
}
783
- }//end foreach
784
-
785
- }//end processMultiLineArray()
786
-
787
-
788
- }//end class
800
+ }
801
+ }
802
+ }
0 commit comments