@@ -154,6 +154,7 @@ public static function isTraitUse(File $phpcsFile, $stackPtr)
154
154
* Handles single import, multi-import and group-import use statements.
155
155
*
156
156
* @since 1.0.0
157
+ * @since 1.0.0-alpha4 Added support for PHP 8.0 identifier name tokenization.
157
158
*
158
159
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
159
160
* @param int $stackPtr The position in the stack of the `T_USE` token.
@@ -229,7 +230,7 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
229
230
continue ;
230
231
}
231
232
232
- $ tokenCode = $ tokens [$ i ]['code ' ];
233
+ $ tokenType = $ tokens [$ i ]['type ' ];
233
234
234
235
/*
235
236
* BC: Work round a tokenizer bug related to a parse error.
@@ -241,16 +242,16 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
241
242
*
242
243
* Along the same lines, the `}` T_CLOSE_USE_GROUP would also be tokenized as T_STRING.
243
244
*/
244
- if ($ tokenCode === \ T_STRING ) {
245
+ if ($ tokenType === ' T_STRING ' ) {
245
246
if ($ tokens [$ i ]['content ' ] === '; ' ) {
246
- $ tokenCode = \ T_SEMICOLON ;
247
+ $ tokenType = ' T_SEMICOLON ' ;
247
248
} elseif ($ tokens [$ i ]['content ' ] === '} ' ) {
248
- $ tokenCode = \ T_CLOSE_USE_GROUP ;
249
+ $ tokenType = ' T_CLOSE_USE_GROUP ' ;
249
250
}
250
251
}
251
252
252
- switch ($ tokenCode ) {
253
- case \ T_STRING :
253
+ switch ($ tokenType ) {
254
+ case ' T_STRING ' :
254
255
// Only when either at the start of the statement or at the start of a new sub within a group.
255
256
if ($ start === true && $ fixedType === false ) {
256
257
$ content = \strtolower ($ tokens [$ i ]['content ' ]);
@@ -276,23 +277,50 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
276
277
}
277
278
278
279
$ alias = $ tokens [$ i ]['content ' ];
280
+
281
+ /*
282
+ * BC: work around PHPCS tokenizer issue in PHPCS < 3.5.7 where anything directly after
283
+ * a `function` or `const` keyword would be retokenized to `T_STRING`, including the
284
+ * PHP 8 identifier name tokens.
285
+ */
286
+ $ hasSlash = \strrpos ($ tokens [$ i ]['content ' ], '\\' );
287
+ if ($ hasSlash !== false ) {
288
+ $ alias = \substr ($ tokens [$ i ]['content ' ], ($ hasSlash + 1 ));
289
+ }
290
+
291
+ break ;
292
+
293
+ case 'T_NAME_QUALIFIED ' :
294
+ case 'T_NAME_FULLY_QUALIFIED ' : // This would be a parse error, but handle it anyway.
295
+ // Only when either at the start of the statement or at the start of a new sub within a group.
296
+ if ($ start === true && $ fixedType === false ) {
297
+ $ type = 'name ' ;
298
+ }
299
+
300
+ $ start = false ;
301
+
302
+ if ($ hasAlias === false ) {
303
+ $ name .= $ tokens [$ i ]['content ' ];
304
+ }
305
+
306
+ $ alias = \substr ($ tokens [$ i ]['content ' ], (\strrpos ($ tokens [$ i ]['content ' ], '\\' ) + 1 ));
279
307
break ;
280
308
281
- case \ T_AS :
309
+ case ' T_AS ' :
282
310
$ hasAlias = true ;
283
311
break ;
284
312
285
- case \ T_OPEN_USE_GROUP :
313
+ case ' T_OPEN_USE_GROUP ' :
286
314
$ start = true ;
287
315
$ useGroup = true ;
288
316
$ baseName = $ name ;
289
317
$ name = '' ;
290
318
break ;
291
319
292
- case \ T_SEMICOLON :
293
- case \ T_CLOSE_TAG :
294
- case \ T_CLOSE_USE_GROUP :
295
- case \ T_COMMA :
320
+ case ' T_SEMICOLON ' :
321
+ case ' T_CLOSE_TAG ' :
322
+ case ' T_CLOSE_USE_GROUP ' :
323
+ case ' T_COMMA ' :
296
324
if ($ name !== '' ) {
297
325
if ($ useGroup === true ) {
298
326
$ statements [$ type ][$ alias ] = $ baseName . $ name ;
@@ -301,7 +329,7 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
301
329
}
302
330
}
303
331
304
- if ($ tokenCode !== \ T_COMMA ) {
332
+ if ($ tokenType !== ' T_COMMA ' ) {
305
333
break 2 ;
306
334
}
307
335
@@ -314,12 +342,12 @@ public static function splitImportUseStatement(File $phpcsFile, $stackPtr)
314
342
}
315
343
break ;
316
344
317
- case \ T_NS_SEPARATOR :
345
+ case ' T_NS_SEPARATOR ' :
318
346
$ name .= $ tokens [$ i ]['content ' ];
319
347
break ;
320
348
321
- case \ T_FUNCTION :
322
- case \ T_CONST :
349
+ case ' T_FUNCTION ' :
350
+ case ' T_CONST ' :
323
351
/*
324
352
* BC: Work around tokenizer bug in PHPCS < 3.4.1.
325
353
*
0 commit comments