@@ -132,40 +132,23 @@ public function process(File $phpcsFile, $stackPtr)
132
132
if ($ startOfType !== $ constName ) {
133
133
$ endOfType = $ phpcsFile ->findPrevious (Tokens::$ emptyTokens , ($ constName - 1 ), null , true );
134
134
135
- $ type = '' ;
136
- $ isUnionType = false ;
137
- $ isIntersectionType = false ;
138
- for ($ j = $ startOfType ; $ j <= $ endOfType ; $ j ++) {
139
- if (isset ($ ignore [$ tokens [$ j ]['code ' ]]) === true ) {
140
- continue ;
141
- }
142
-
143
- if ($ tokens [$ j ]['code ' ] === T_TYPE_UNION ) {
144
- $ isUnionType = true ;
145
- }
146
-
147
- if ($ tokens [$ j ]['code ' ] === T_TYPE_INTERSECTION ) {
148
- $ isIntersectionType = true ;
149
- }
150
-
151
- $ type .= $ tokens [$ j ]['content ' ];
152
- }
153
-
154
135
$ error = 'PHP constant type declarations must be lowercase; expected "%s" but found "%s" ' ;
155
136
$ errorCode = 'ConstantTypeFound ' ;
156
137
157
- if ($ isIntersectionType === true ) {
158
- // Intersection types don't support simple types.
159
- } else if ($ isUnionType === true ) {
138
+ if ($ startOfType !== $ endOfType ) {
139
+ // Multi-token type.
160
140
$ this ->processUnionType (
161
141
$ phpcsFile ,
162
142
$ startOfType ,
163
143
$ endOfType ,
164
144
$ error ,
165
145
$ errorCode
166
146
);
167
- } else if (isset ($ this ->phpTypes [strtolower ($ type )]) === true ) {
168
- $ this ->processType ($ phpcsFile , $ startOfType , $ type , $ error , $ errorCode );
147
+ } else {
148
+ $ type = $ tokens [$ startOfType ]['content ' ];
149
+ if (isset ($ this ->phpTypes [strtolower ($ type )]) === true ) {
150
+ $ this ->processType ($ phpcsFile , $ startOfType , $ type , $ error , $ errorCode );
151
+ }
169
152
}
170
153
}//end if
171
154
@@ -195,9 +178,8 @@ public function process(File $phpcsFile, $stackPtr)
195
178
$ error = 'PHP property type declarations must be lowercase; expected "%s" but found "%s" ' ;
196
179
$ errorCode = 'PropertyTypeFound ' ;
197
180
198
- if (strpos ($ type , '& ' ) !== false ) {
199
- // Intersection types don't support simple types.
200
- } else if (strpos ($ type , '| ' ) !== false ) {
181
+ if ($ props ['type_token ' ] !== $ props ['type_end_token ' ]) {
182
+ // Multi-token type.
201
183
$ this ->processUnionType (
202
184
$ phpcsFile ,
203
185
$ props ['type_token ' ],
@@ -227,9 +209,8 @@ public function process(File $phpcsFile, $stackPtr)
227
209
$ error = 'PHP return type declarations must be lowercase; expected "%s" but found "%s" ' ;
228
210
$ errorCode = 'ReturnTypeFound ' ;
229
211
230
- if (strpos ($ returnType , '& ' ) !== false ) {
231
- // Intersection types don't support simple types.
232
- } else if (strpos ($ returnType , '| ' ) !== false ) {
212
+ if ($ props ['return_type_token ' ] !== $ props ['return_type_end_token ' ]) {
213
+ // Multi-token type.
233
214
$ this ->processUnionType (
234
215
$ phpcsFile ,
235
216
$ props ['return_type_token ' ],
@@ -259,9 +240,8 @@ public function process(File $phpcsFile, $stackPtr)
259
240
$ error = 'PHP parameter type declarations must be lowercase; expected "%s" but found "%s" ' ;
260
241
$ errorCode = 'ParamTypeFound ' ;
261
242
262
- if (strpos ($ typeHint , '& ' ) !== false ) {
263
- // Intersection types don't support simple types.
264
- } else if (strpos ($ typeHint , '| ' ) !== false ) {
243
+ if ($ param ['type_hint_token ' ] !== $ param ['type_hint_end_token ' ]) {
244
+ // Multi-token type.
265
245
$ this ->processUnionType (
266
246
$ phpcsFile ,
267
247
$ param ['type_hint_token ' ],
@@ -279,7 +259,9 @@ public function process(File $phpcsFile, $stackPtr)
279
259
280
260
281
261
/**
282
- * Processes a union type declaration.
262
+ * Processes a multi-token type declaration.
263
+ *
264
+ * {@internal The method name is superseded by the reality, but changing it would be a BC-break.}
283
265
*
284
266
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
285
267
* @param int $typeDeclStart The position of the start of the type token.
@@ -291,37 +273,51 @@ public function process(File $phpcsFile, $stackPtr)
291
273
*/
292
274
protected function processUnionType (File $ phpcsFile , $ typeDeclStart , $ typeDeclEnd , $ error , $ errorCode )
293
275
{
294
- $ tokens = $ phpcsFile ->getTokens ();
295
- $ current = $ typeDeclStart ;
296
-
297
- do {
298
- $ endOfType = $ phpcsFile ->findNext (T_TYPE_UNION , $ current , $ typeDeclEnd );
299
- if ($ endOfType === false ) {
300
- // This must be the last type in the union.
301
- $ endOfType = ($ typeDeclEnd + 1 );
302
- }
276
+ $ tokens = $ phpcsFile ->getTokens ();
277
+ $ typeTokenCount = 0 ;
278
+ $ typeStart = null ;
279
+ $ type = '' ;
303
280
304
- $ hasNsSep = $ phpcsFile ->findNext (T_NS_SEPARATOR , $ current , $ endOfType );
305
- if ($ hasNsSep !== false ) {
306
- // Multi-token class based type. Ignore.
307
- $ current = ($ endOfType + 1 );
281
+ for ($ i = $ typeDeclStart ; $ i <= $ typeDeclEnd ; $ i ++) {
282
+ if (isset (Tokens::$ emptyTokens [$ tokens [$ i ]['code ' ]]) === true ) {
308
283
continue ;
309
284
}
310
285
311
- // Type consisting of a single token.
312
- $ startOfType = $ phpcsFile ->findNext (Tokens::$ emptyTokens , $ current , $ endOfType , true );
313
- if ($ startOfType === false ) {
314
- // Parse error.
315
- return ;
286
+ if ($ tokens [$ i ]['code ' ] === T_TYPE_UNION
287
+ || $ tokens [$ i ]['code ' ] === T_TYPE_INTERSECTION
288
+ || $ tokens [$ i ]['code ' ] === T_TYPE_OPEN_PARENTHESIS
289
+ || $ tokens [$ i ]['code ' ] === T_TYPE_CLOSE_PARENTHESIS
290
+ ) {
291
+ if ($ typeTokenCount === 1
292
+ && $ type !== ''
293
+ && isset ($ this ->phpTypes [strtolower ($ type )]) === true
294
+ ) {
295
+ $ this ->processType ($ phpcsFile , $ typeStart , $ type , $ error , $ errorCode );
296
+ }
297
+
298
+ // Reset for the next type in the type string.
299
+ $ typeTokenCount = 0 ;
300
+ $ typeStart = null ;
301
+ $ type = '' ;
302
+
303
+ continue ;
316
304
}
317
305
318
- $ type = $ tokens [$ startOfType ]['content ' ];
319
- if (isset ($ this ->phpTypes [strtolower ($ type )]) === true ) {
320
- $ this ->processType ($ phpcsFile , $ startOfType , $ type , $ error , $ errorCode );
306
+ if (isset ($ typeStart ) === false ) {
307
+ $ typeStart = $ i ;
321
308
}
322
309
323
- $ current = ($ endOfType + 1 );
324
- } while ($ current <= $ typeDeclEnd );
310
+ ++$ typeTokenCount ;
311
+ $ type .= $ tokens [$ i ]['content ' ];
312
+ }//end for
313
+
314
+ // Handle type at end of type string.
315
+ if ($ typeTokenCount === 1
316
+ && $ type !== ''
317
+ && isset ($ this ->phpTypes [strtolower ($ type )]) === true
318
+ ) {
319
+ $ this ->processType ($ phpcsFile , $ typeStart , $ type , $ error , $ errorCode );
320
+ }
325
321
326
322
}//end processUnionType()
327
323
0 commit comments