44 * This file is part of the mo4-coding-standard (phpcs standard)
55 *
66 * @author Xaver Loppenstedt <xaver@loppenstedt.de>
7+ *
78 * @license http://spdx.org/licenses/MIT MIT License
9+ *
810 * @link https://github.com/mayflower/mo4-coding-standard
911 */
12+
1013declare (strict_types=1 );
1114
1215namespace MO4 \Sniffs \Arrays ;
2124 * '=>' must be aligned in arrays, and the key and the '=>' must be in the same line
2225 *
2326 * @author Xaver Loppenstedt <xaver@loppenstedt.de>
27+ *
2428 * @copyright 2013 Xaver Loppenstedt, some rights reserved.
29+ *
2530 * @license http://spdx.org/licenses/MIT MIT License
31+ *
2632 * @link https://github.com/mayflower/mo4-coding-standard
2733 */
2834class ArrayDoubleArrowAlignmentSniff implements Sniff
2935{
30-
3136 /**
3237 * Define all types of arrays.
3338 *
3439 * @var array
3540 */
36- protected $ arrayTokens = [
41+ protected $ arrayTokens = [
3742 // @phan-suppress-next-line PhanUndeclaredConstant
3843 T_OPEN_SHORT_ARRAY ,
3944 T_ARRAY ,
4045 ];
4146
42-
4347 /**
4448 * Registers the tokens that this sniff wants to listen for.
4549 *
4650 * @return array<int, int>
51+ *
4752 * @see Tokens.php
4853 */
4954 public function register (): array
5055 {
5156 return $ this ->arrayTokens ;
52-
53- }//end register()
54-
57+ }
5558
5659 /**
5760 * Processes this test, when one of its tokens is encountered.
5861 *
62+ * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
63+ *
5964 * @param File $phpcsFile The file being scanned.
6065 * @param int $stackPtr The position of the current token in
6166 * the stack passed in $tokens.
@@ -69,7 +74,7 @@ public function process(File $phpcsFile, $stackPtr): void
6974 $ tokens = $ phpcsFile ->getTokens ();
7075 $ current = $ tokens [$ stackPtr ];
7176
72- if ($ current ['code ' ] === T_ARRAY ) {
77+ if (T_ARRAY === $ current ['code ' ]) {
7378 $ start = $ current ['parenthesis_opener ' ];
7479 $ end = $ current ['parenthesis_closer ' ];
7580 } else {
@@ -93,82 +98,82 @@ public function process(File $phpcsFile, $stackPtr): void
9398 $ previous = $ tokens [($ i - 1 )];
9499
95100 // Skip nested arrays.
96- if (\in_array ($ current ['code ' ], $ this ->arrayTokens , true ) === true ) {
97- if ($ current ['code ' ] === T_ARRAY ) {
98- $ i = ($ current ['parenthesis_closer ' ] + 1 );
99- } else {
100- $ i = ($ current ['bracket_closer ' ] + 1 );
101- }
101+ if (true === \in_array ($ current ['code ' ], $ this ->arrayTokens , true )) {
102+ $ i = T_ARRAY === $ current ['code ' ] ? ($ current ['parenthesis_closer ' ] + 1 ) : ($ current ['bracket_closer ' ] + 1 );
102103
103104 continue ;
104105 }
105106
106107 // Skip closures in array.
107- if ($ current ['code ' ] === T_CLOSURE ) {
108+ if (T_CLOSURE === $ current ['code ' ]) {
108109 $ i = ($ current ['scope_closer ' ] + 1 );
110+
109111 continue ;
110112 }
111113
112114 $ i = (int ) $ i ;
113115
114- if ($ current ['code ' ] === T_DOUBLE_ARROW ) {
115- $ assignments [] = $ i ;
116- $ column = $ previous ['column ' ];
117- $ line = $ current ['line ' ];
118-
119- if ($ lastLine === $ line ) {
120- $ previousComma = $ this ->getPreviousComma ($ phpcsFile , $ i , $ start );
121-
122- $ msg = 'only one "=>" assignments per line is allowed in a multi line array ' ;
123-
124- if ($ previousComma !== false ) {
125- $ fixable = $ phpcsFile ->addFixableError ($ msg , $ i , 'OneAssignmentPerLine ' );
126-
127- if ($ fixable === true ) {
128- $ phpcsFile ->fixer ->beginChangeset ();
129- $ phpcsFile ->fixer ->addNewline ((int ) $ previousComma );
130- $ phpcsFile ->fixer ->endChangeset ();
131- }
132- } else {
133- // Remove current and previous '=>' from array for further processing.
134- \array_pop ($ assignments );
135- \array_pop ($ assignments );
136- $ phpcsFile ->addError ($ msg , $ i , 'OneAssignmentPerLine ' );
137- }
138- }
116+ if (T_DOUBLE_ARROW !== $ current ['code ' ]) {
117+ continue ;
118+ }
139119
140- $ hasKeyInLine = false ;
120+ $ assignments [] = $ i ;
121+ $ column = $ previous ['column ' ];
122+ $ line = $ current ['line ' ];
141123
142- $ j = ($ i - 1 );
143- while (($ j >= 0 ) && ($ tokens [$ j ]['line ' ] === $ current ['line ' ])) {
144- if (\in_array ($ tokens [$ j ]['code ' ], PHP_CodeSniffer_Tokens::$ emptyTokens , true ) === false ) {
145- $ hasKeyInLine = true ;
146- }
124+ if ($ lastLine === $ line ) {
125+ $ previousComma = $ this ->getPreviousComma ($ phpcsFile , $ i , $ start );
147126
148- $ j --;
149- }
127+ $ msg = 'only one "=>" assignments per line is allowed in a multi line array ' ;
150128
151- if ($ hasKeyInLine === false ) {
152- $ fixable = $ phpcsFile ->addFixableError (
153- 'in arrays, keys and "=>" must be on the same line ' ,
154- $ i ,
155- 'KeyAndValueNotOnSameLine '
156- );
129+ if (false !== $ previousComma ) {
130+ $ fixable = $ phpcsFile ->addFixableError ($ msg , $ i , 'OneAssignmentPerLine ' );
157131
158- if ($ fixable === true ) {
132+ if (true === $ fixable ) {
159133 $ phpcsFile ->fixer ->beginChangeset ();
160- $ phpcsFile ->fixer ->replaceToken ( $ j , '' );
134+ $ phpcsFile ->fixer ->addNewline (( int ) $ previousComma );
161135 $ phpcsFile ->fixer ->endChangeset ();
162136 }
137+ } else {
138+ // Remove current and previous '=>' from array for further processing.
139+ \array_pop ($ assignments );
140+ \array_pop ($ assignments );
141+ $ phpcsFile ->addError ($ msg , $ i , 'OneAssignmentPerLine ' );
163142 }
143+ }
144+
145+ $ hasKeyInLine = false ;
164146
165- if ($ column > $ keyEndColumn ) {
166- $ keyEndColumn = $ column ;
147+ $ j = ($ i - 1 );
148+
149+ while (($ j >= 0 ) && ($ tokens [$ j ]['line ' ] === $ current ['line ' ])) {
150+ if (false === \in_array ($ tokens [$ j ]['code ' ], PHP_CodeSniffer_Tokens::$ emptyTokens , true )) {
151+ $ hasKeyInLine = true ;
152+ }
153+
154+ $ j --;
155+ }
156+
157+ if (false === $ hasKeyInLine ) {
158+ $ fixable = $ phpcsFile ->addFixableError (
159+ 'in arrays, keys and "=>" must be on the same line ' ,
160+ $ i ,
161+ 'KeyAndValueNotOnSameLine '
162+ );
163+
164+ if (true === $ fixable ) {
165+ $ phpcsFile ->fixer ->beginChangeset ();
166+ $ phpcsFile ->fixer ->replaceToken ($ j , '' );
167+ $ phpcsFile ->fixer ->endChangeset ();
167168 }
169+ }
168170
169- $ lastLine = $ line ;
170- }//end if
171- }//end for
171+ if ($ column > $ keyEndColumn ) {
172+ $ keyEndColumn = $ column ;
173+ }
174+
175+ $ lastLine = $ line ;
176+ }
172177
173178 $ doubleArrowStartColumn = ($ keyEndColumn + 1 );
174179
@@ -179,26 +184,28 @@ public function process(File $phpcsFile, $stackPtr): void
179184 $ beforeArrowPtr = ($ ptr - 1 );
180185 $ currentIndent = \strlen ($ tokens [$ beforeArrowPtr ]['content ' ]);
181186 $ correctIndent = ($ currentIndent - $ column + $ doubleArrowStartColumn );
182- if ($ column !== $ doubleArrowStartColumn ) {
183- $ fixable = $ phpcsFile ->addFixableError ("each \"=> \" assignments must be aligned; current indentation before \"=> \" are $ currentIndent space(s), must be $ correctIndent space(s) " , $ ptr , 'AssignmentsNotAligned ' );
184187
185- if ($ fixable === false ) {
186- continue ;
187- }
188+ if ($ column === $ doubleArrowStartColumn ) {
189+ continue ;
190+ }
188191
189- $ phpcsFile ->fixer ->beginChangeset ();
190- if ($ tokens [$ beforeArrowPtr ]['code ' ] === T_WHITESPACE ) {
191- $ phpcsFile ->fixer ->replaceToken ($ beforeArrowPtr , \str_repeat (' ' , $ correctIndent ));
192- } else {
193- $ phpcsFile ->fixer ->addContent ($ beforeArrowPtr , \str_repeat (' ' , $ correctIndent ));
194- }
192+ $ fixable = $ phpcsFile ->addFixableError ("each \"=> \" assignments must be aligned; current indentation before \"=> \" are {$ currentIndent } space(s), must be {$ correctIndent } space(s) " , $ ptr , 'AssignmentsNotAligned ' );
195193
196- $ phpcsFile ->fixer ->endChangeset ();
194+ if (false === $ fixable ) {
195+ continue ;
197196 }
198- }//end foreach
199197
200- } //end process()
198+ $ phpcsFile -> fixer -> beginChangeset ();
201199
200+ if (T_WHITESPACE === $ tokens [$ beforeArrowPtr ]['code ' ]) {
201+ $ phpcsFile ->fixer ->replaceToken ($ beforeArrowPtr , \str_repeat (' ' , $ correctIndent ));
202+ } else {
203+ $ phpcsFile ->fixer ->addContent ($ beforeArrowPtr , \str_repeat (' ' , $ correctIndent ));
204+ }
205+
206+ $ phpcsFile ->fixer ->endChangeset ();
207+ }
208+ }
202209
203210 /**
204211 * Find previous comma in array.
@@ -210,28 +217,27 @@ public function process(File $phpcsFile, $stackPtr): void
210217 *
211218 * @return bool|int
212219 */
213- private function getPreviousComma (File $ phpcsFile , $ stackPtr , $ start )
220+ private function getPreviousComma (File $ phpcsFile , int $ stackPtr , int $ start )
214221 {
215222 $ previousComma = false ;
216223 $ tokens = $ phpcsFile ->getTokens ();
217224
218225 $ ptr = $ phpcsFile ->findPrevious ([T_COMMA , T_CLOSE_SHORT_ARRAY ], $ stackPtr , $ start );
219- while ($ ptr !== false ) {
220- if ($ tokens [$ ptr ]['code ' ] === T_COMMA ) {
226+
227+ while (false !== $ ptr ) {
228+ if (T_COMMA === $ tokens [$ ptr ]['code ' ]) {
221229 $ previousComma = $ ptr ;
230+
222231 break ;
223232 }
224233
225- if ($ tokens [$ ptr ]['code ' ] === T_CLOSE_SHORT_ARRAY ) {
234+ if (T_CLOSE_SHORT_ARRAY === $ tokens [$ ptr ]['code ' ]) {
226235 $ ptr = $ tokens [$ ptr ]['bracket_opener ' ];
227236 }
228237
229238 $ ptr = $ phpcsFile ->findPrevious ([T_COMMA , T_CLOSE_SHORT_ARRAY ], ($ ptr - 1 ), $ start );
230239 }
231240
232241 return $ previousComma ;
233-
234- }//end getPreviousComma()
235-
236-
237- }//end class
242+ }
243+ }
0 commit comments