@@ -12,6 +12,11 @@ class Symfony3Custom_Sniffs_Arrays_ArrayDeclarationSniff implements PHP_CodeSnif
1212 */
1313 public $ indent = 4 ;
1414
15+ /**
16+ * @var bool
17+ */
18+ public $ ignoreNewLine = true ;
19+
1520 /**
1621 * Returns an array of tokens this test wants to listen for.
1722 *
@@ -23,7 +28,6 @@ public function register()
2328 T_ARRAY ,
2429 T_OPEN_SHORT_ARRAY ,
2530 );
26-
2731 }
2832
2933 /**
@@ -294,7 +298,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
294298 if ($ fix === true ) {
295299 $ phpcsFile ->fixer ->addNewlineBefore ($ arrayEnd );
296300 }
297- } else if ($ tokens [$ arrayEnd ]['column ' ] !== $ currentIndent + 1 ) {
301+ } elseif ($ tokens [$ arrayEnd ]['column ' ] !== $ currentIndent + 1 ) {
298302 // Check the closing bracket is lined up under the "a" in array.
299303 $ expected = ($ currentIndent );
300304 $ found = ($ tokens [$ arrayEnd ]['column ' ] - 1 );
@@ -348,7 +352,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
348352
349353 if ($ tokens [$ nextToken ]['code ' ] === T_ARRAY ) {
350354 $ 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 ) {
352356 $ nextToken = $ tokens [$ nextToken ]['bracket_closer ' ];
353357 } else {
354358 // T_CLOSURE.
@@ -398,6 +402,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
398402 if ($ keyUsed === true && $ tokens [$ lastToken ]['code ' ] === T_COMMA ) {
399403 $ error = 'No key specified for array entry; first entry specifies key ' ;
400404 $ phpcsFile ->addError ($ error , $ nextToken , 'NoKeySpecified ' );
405+
401406 return ;
402407 }
403408
@@ -441,6 +446,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
441446 if ($ singleUsed === true ) {
442447 $ error = 'Key specified for array entry; first entry has no key ' ;
443448 $ phpcsFile ->addError ($ error , $ nextToken , 'KeySpecified ' );
449+
444450 return ;
445451 }
446452
@@ -456,7 +462,10 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
456462 $ currentEntry ['index_content ' ] = $ tokens [$ indexEnd ]['content ' ];
457463 } else {
458464 $ 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+ );
460469 }
461470
462471 $ indexLength = strlen ($ currentEntry ['index_content ' ]);
@@ -530,7 +539,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
530539
531540 $ phpcsFile ->fixer ->addNewlineBefore ($ value ['value ' ]);
532541 }
533- } else if ($ tokens [($ value ['value ' ] - 1 )]['code ' ] === T_WHITESPACE ) {
542+ } elseif ($ tokens [($ value ['value ' ] - 1 )]['code ' ] === T_WHITESPACE ) {
534543 $ expected = $ currentIndent + $ this ->indent ;
535544
536545 $ first = $ phpcsFile ->findFirstOnLine (T_WHITESPACE , $ value ['value ' ], true );
@@ -641,7 +650,13 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
641650 $ found ,
642651 );
643652
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+
645660 if ($ fix === true ) {
646661 if ($ found === 0 ) {
647662 $ phpcsFile ->fixer ->addContent (($ index ['index ' ] - 1 ), str_repeat (' ' , $ expected ));
@@ -681,30 +696,32 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
681696 $ found = 'newline ' ;
682697 }
683698
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+ ];
689705
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+ }
698714
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+ }
705722 }
706723 }
707- }//end if
724+ }
708725
709726 // Check each line ends in a comma.
710727 $ valueLine = $ tokens [$ index ['value ' ]]['line ' ];
@@ -747,7 +764,7 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
747764 $ nextComma = $ i ;
748765 break ;
749766 }
750- }//end for
767+ }
751768
752769 if ($ nextComma === false || ($ tokens [$ nextComma ]['line ' ] !== $ valueLine )) {
753770 $ error = 'Each line in an array declaration must end in a comma ' ;
@@ -780,9 +797,6 @@ public function processMultiLineArray(PHP_CodeSniffer_File $phpcsFile, $stackPtr
780797 $ phpcsFile ->fixer ->replaceToken (($ nextComma - 1 ), '' );
781798 }
782799 }
783- }//end foreach
784-
785- }//end processMultiLineArray()
786-
787-
788- }//end class
800+ }
801+ }
802+ }
0 commit comments