@@ -45,8 +45,9 @@ class UnnecessaryNamespaceUsageSniff implements Sniff
4545 * @var array<int, int>
4646 */
4747 private $ classNameTokens = [
48- T_NS_SEPARATOR ,
49- T_STRING ,
48+ T_NAME_FULLY_QUALIFIED ,
49+ T_NAME_QUALIFIED ,
50+ T_NAME_RELATIVE ,
5051 ];
5152
5253 /**
@@ -88,7 +89,9 @@ public function process(File $phpcsFile, $stackPtr): void
8889 '@var ' => 2 ,
8990 ];
9091 $ scanTokens = [
91- T_NS_SEPARATOR ,
92+ T_NAME_FULLY_QUALIFIED ,
93+ T_NAME_QUALIFIED ,
94+ T_NAME_RELATIVE ,
9295 T_DOC_COMMENT_OPEN_TAG ,
9396 ];
9497
@@ -106,11 +109,7 @@ public function process(File $phpcsFile, $stackPtr): void
106109 true
107110 );
108111
109- if (T_NS_SEPARATOR === $ tokens [$ nsSep ]['code ' ]) {
110- if (T_STRING === $ tokens [($ nsSep - 1 )]['code ' ]) {
111- --$ nsSep ;
112- }
113-
112+ if (\in_array ($ tokens [$ nsSep ]['code ' ], [T_NAME_FULLY_QUALIFIED , T_NAME_QUALIFIED , T_NAME_RELATIVE ], true )) {
114113 $ className = $ phpcsFile ->getTokensAsString (
115114 $ nsSep ,
116115 ($ classNameEnd - $ nsSep )
@@ -264,8 +263,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
264263 $ length = ($ classNameEnd - $ classNameStart );
265264 $ className = $ phpcsFile ->getTokensAsString ($ classNameStart , $ length );
266265
267- $ className = $ this ->getFullyQualifiedClassName ($ className );
268- $ useStatements [$ className ] = $ tokens [$ aliasNamePtr ]['content ' ];
266+ $ className = $ this ->getFullyQualifiedClassName ($ className );
267+ $ tokenContent = $ tokens [$ aliasNamePtr ]['content ' ];
268+
269+ if (\str_contains ($ tokenContent , '\\' )) {
270+ $ path = \explode ('\\' , $ tokenContent );
271+ $ tokenContent = $ path [\array_key_last ($ path )];
272+ }
273+
274+ $ useStatements [$ className ] = $ tokenContent ;
269275 $ i = ($ useEnd + 1 );
270276
271277 $ useTokenPtr = T_COMMA === $ tokens [$ useEnd ]['code ' ] ? $ i : $ phpcsFile ->findNext (T_USE , $ i , $ end );
0 commit comments