4
4
* This file is part of the mo4-coding-standard (phpcs standard)
5
5
*
6
6
* @author Xaver Loppenstedt <[email protected] >
7
+ *
7
8
* @license http://spdx.org/licenses/MIT MIT License
9
+ *
8
10
* @link https://github.com/mayflower/mo4-coding-standard
9
11
*/
12
+
10
13
declare (strict_types=1 );
11
14
12
15
namespace MO4 \Sniffs \Arrays ;
21
24
* '=>' must be aligned in arrays, and the key and the '=>' must be in the same line
22
25
*
23
26
* @author Xaver Loppenstedt <[email protected] >
27
+ *
24
28
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
29
+ *
25
30
* @license http://spdx.org/licenses/MIT MIT License
31
+ *
26
32
* @link https://github.com/mayflower/mo4-coding-standard
27
33
*/
28
34
class ArrayDoubleArrowAlignmentSniff implements Sniff
29
35
{
30
-
31
36
/**
32
37
* Define all types of arrays.
33
38
*
34
39
* @var array
35
40
*/
36
- protected $ arrayTokens = [
41
+ protected $ arrayTokens = [
37
42
// @phan-suppress-next-line PhanUndeclaredConstant
38
43
T_OPEN_SHORT_ARRAY ,
39
44
T_ARRAY ,
40
45
];
41
46
42
-
43
47
/**
44
48
* Registers the tokens that this sniff wants to listen for.
45
49
*
46
50
* @return array<int, int>
51
+ *
47
52
* @see Tokens.php
48
53
*/
49
54
public function register (): array
50
55
{
51
56
return $ this ->arrayTokens ;
52
-
53
- }//end register()
54
-
57
+ }
55
58
56
59
/**
57
60
* Processes this test, when one of its tokens is encountered.
58
61
*
62
+ * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
63
+ *
59
64
* @param File $phpcsFile The file being scanned.
60
65
* @param int $stackPtr The position of the current token in
61
66
* the stack passed in $tokens.
@@ -69,7 +74,7 @@ public function process(File $phpcsFile, $stackPtr): void
69
74
$ tokens = $ phpcsFile ->getTokens ();
70
75
$ current = $ tokens [$ stackPtr ];
71
76
72
- if ($ current ['code ' ] === T_ARRAY ) {
77
+ if (T_ARRAY === $ current ['code ' ]) {
73
78
$ start = $ current ['parenthesis_opener ' ];
74
79
$ end = $ current ['parenthesis_closer ' ];
75
80
} else {
@@ -93,82 +98,82 @@ public function process(File $phpcsFile, $stackPtr): void
93
98
$ previous = $ tokens [($ i - 1 )];
94
99
95
100
// 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 );
102
103
103
104
continue ;
104
105
}
105
106
106
107
// Skip closures in array.
107
- if ($ current ['code ' ] === T_CLOSURE ) {
108
+ if (T_CLOSURE === $ current ['code ' ]) {
108
109
$ i = ($ current ['scope_closer ' ] + 1 );
110
+
109
111
continue ;
110
112
}
111
113
112
114
$ i = (int ) $ i ;
113
115
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
+ }
139
119
140
- $ hasKeyInLine = false ;
120
+ $ assignments [] = $ i ;
121
+ $ column = $ previous ['column ' ];
122
+ $ line = $ current ['line ' ];
141
123
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 );
147
126
148
- $ j --;
149
- }
127
+ $ msg = 'only one "=>" assignments per line is allowed in a multi line array ' ;
150
128
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 ' );
157
131
158
- if ($ fixable === true ) {
132
+ if (true === $ fixable ) {
159
133
$ phpcsFile ->fixer ->beginChangeset ();
160
- $ phpcsFile ->fixer ->replaceToken ( $ j , '' );
134
+ $ phpcsFile ->fixer ->addNewline (( int ) $ previousComma );
161
135
$ phpcsFile ->fixer ->endChangeset ();
162
136
}
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 ' );
163
142
}
143
+ }
144
+
145
+ $ hasKeyInLine = false ;
164
146
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 ();
167
168
}
169
+ }
168
170
169
- $ lastLine = $ line ;
170
- }//end if
171
- }//end for
171
+ if ($ column > $ keyEndColumn ) {
172
+ $ keyEndColumn = $ column ;
173
+ }
174
+
175
+ $ lastLine = $ line ;
176
+ }
172
177
173
178
$ doubleArrowStartColumn = ($ keyEndColumn + 1 );
174
179
@@ -179,26 +184,28 @@ public function process(File $phpcsFile, $stackPtr): void
179
184
$ beforeArrowPtr = ($ ptr - 1 );
180
185
$ currentIndent = \strlen ($ tokens [$ beforeArrowPtr ]['content ' ]);
181
186
$ 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 ' );
184
187
185
- if ($ fixable === false ) {
186
- continue ;
187
- }
188
+ if ($ column === $ doubleArrowStartColumn ) {
189
+ continue ;
190
+ }
188
191
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 ' );
195
193
196
- $ phpcsFile ->fixer ->endChangeset ();
194
+ if (false === $ fixable ) {
195
+ continue ;
197
196
}
198
- }//end foreach
199
197
200
- } //end process()
198
+ $ phpcsFile -> fixer -> beginChangeset ();
201
199
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
+ }
202
209
203
210
/**
204
211
* Find previous comma in array.
@@ -210,28 +217,27 @@ public function process(File $phpcsFile, $stackPtr): void
210
217
*
211
218
* @return bool|int
212
219
*/
213
- private function getPreviousComma (File $ phpcsFile , $ stackPtr , $ start )
220
+ private function getPreviousComma (File $ phpcsFile , int $ stackPtr , int $ start )
214
221
{
215
222
$ previousComma = false ;
216
223
$ tokens = $ phpcsFile ->getTokens ();
217
224
218
225
$ 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 ' ]) {
221
229
$ previousComma = $ ptr ;
230
+
222
231
break ;
223
232
}
224
233
225
- if ($ tokens [$ ptr ]['code ' ] === T_CLOSE_SHORT_ARRAY ) {
234
+ if (T_CLOSE_SHORT_ARRAY === $ tokens [$ ptr ]['code ' ]) {
226
235
$ ptr = $ tokens [$ ptr ]['bracket_opener ' ];
227
236
}
228
237
229
238
$ ptr = $ phpcsFile ->findPrevious ([T_COMMA , T_CLOSE_SHORT_ARRAY ], ($ ptr - 1 ), $ start );
230
239
}
231
240
232
241
return $ previousComma ;
233
-
234
- }//end getPreviousComma()
235
-
236
-
237
- }//end class
242
+ }
243
+ }
0 commit comments