@@ -42,11 +42,11 @@ class UnnecessaryNamespaceUsageSniff implements Sniff
4242 /**
4343 * Tokens used in full class name.
4444 *
45- * @var array<int, int>
4645 */
47- private $ classNameTokens = [
48- T_NS_SEPARATOR ,
49- T_STRING ,
46+ private const CLASS_NAME_TOKENS = [
47+ T_NAME_FULLY_QUALIFIED ,
48+ T_NAME_QUALIFIED ,
49+ T_NAME_RELATIVE ,
5050 ];
5151
5252 /**
@@ -87,7 +87,9 @@ public function process(File $phpcsFile, $stackPtr): void
8787 '@var ' => 2 ,
8888 ];
8989 $ scanTokens = [
90- T_NS_SEPARATOR ,
90+ T_NAME_FULLY_QUALIFIED ,
91+ T_NAME_QUALIFIED ,
92+ T_NAME_RELATIVE ,
9193 T_DOC_COMMENT_OPEN_TAG ,
9294 ];
9395
@@ -99,17 +101,13 @@ public function process(File $phpcsFile, $stackPtr): void
99101
100102 while (false !== $ nsSep ) {
101103 $ classNameEnd = (int ) $ phpcsFile ->findNext (
102- $ this -> classNameTokens ,
104+ self :: CLASS_NAME_TOKENS ,
103105 $ nsSep ,
104106 null ,
105107 true
106108 );
107109
108- if (T_NS_SEPARATOR === $ tokens [$ nsSep ]['code ' ]) {
109- if (T_STRING === $ tokens [($ nsSep - 1 )]['code ' ]) {
110- --$ nsSep ;
111- }
112-
110+ if (\in_array ($ tokens [$ nsSep ]['code ' ], self ::CLASS_NAME_TOKENS , true )) {
113111 $ className = $ phpcsFile ->getTokensAsString (
114112 $ nsSep ,
115113 ($ classNameEnd - $ nsSep )
@@ -121,7 +119,6 @@ public function process(File $phpcsFile, $stackPtr): void
121119 $ className ,
122120 $ namespace ,
123121 $ nsSep ,
124- ($ classNameEnd - 1 )
125122 );
126123 } else {
127124 // Doc comment block.
@@ -192,8 +189,6 @@ public function process(File $phpcsFile, $stackPtr): void
192189 $ typeToken ,
193190 $ namespace ,
194191 $ docCommentStringPtr ,
195- $ docCommentStringPtr ,
196- true
197192 );
198193 }
199194 }
@@ -222,13 +217,13 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
222217
223218 while (false !== $ useTokenPtr ) {
224219 $ classNameStart = (int ) $ phpcsFile ->findNext (
225- PHP_CodeSniffer_Tokens::$ emptyTokens ,
220+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
226221 ($ useTokenPtr + 1 ),
227222 $ end ,
228223 true
229224 );
230225 $ classNameEnd = $ phpcsFile ->findNext (
231- $ this -> classNameTokens ,
226+ self :: CLASS_NAME_TOKENS ,
232227 ($ classNameStart + 1 ),
233228 $ end ,
234229 true
@@ -254,7 +249,7 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
254249
255250 /** @var int $aliasNamePtr */
256251 $ aliasNamePtr = $ phpcsFile ->findPrevious (
257- PHP_CodeSniffer_Tokens::$ emptyTokens ,
252+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
258253 ($ useEnd - 1 ),
259254 0 ,
260255 true
@@ -263,8 +258,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
263258 $ length = ($ classNameEnd - $ classNameStart );
264259 $ className = $ phpcsFile ->getTokensAsString ($ classNameStart , $ length );
265260
266- $ className = $ this ->getFullyQualifiedClassName ($ className );
267- $ useStatements [$ className ] = $ tokens [$ aliasNamePtr ]['content ' ];
261+ $ className = $ this ->getFullyQualifiedClassName ($ className );
262+ $ tokenContent = $ tokens [$ aliasNamePtr ]['content ' ];
263+
264+ if (\str_contains ($ tokenContent , '\\' )) {
265+ $ path = \explode ('\\' , $ tokenContent );
266+ $ tokenContent = $ path [\array_key_last ($ path )];
267+ }
268+
269+ $ useStatements [$ className ] = $ tokenContent ;
268270 $ i = ($ useEnd + 1 );
269271
270272 $ useTokenPtr = T_COMMA === $ tokens [$ useEnd ]['code ' ] ? $ i : $ phpcsFile ->findNext (T_USE , $ i , $ end );
@@ -285,7 +287,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
285287 {
286288 $ namespace = (int ) $ phpcsFile ->findNext (T_NAMESPACE , $ start , $ end );
287289 $ namespaceStart = $ phpcsFile ->findNext (
288- PHP_CodeSniffer_Tokens::$ emptyTokens ,
290+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
289291 ($ namespace + 1 ),
290292 $ end ,
291293 true
@@ -296,7 +298,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
296298 }
297299
298300 $ namespaceEnd = (int ) $ phpcsFile ->findNext (
299- $ this -> classNameTokens ,
301+ self :: CLASS_NAME_TOKENS ,
300302 ($ namespaceStart + 1 ),
301303 $ end ,
302304 true
@@ -327,17 +329,12 @@ private function getFullyQualifiedClassName(string $className): string
327329 * @param string $className class name
328330 * @param string $namespace name space
329331 * @param int $startPtr start token pointer
330- * @param int $endPtr end token pointer
331- * @param bool $isDocBlock true if fixing doc block
332332 *
333333 */
334- private function checkShorthandPossible (File $ phpcsFile , array $ useStatements , string $ className , string $ namespace , int $ startPtr, int $ endPtr , bool $ isDocBlock = false ): void
334+ private function checkShorthandPossible (File $ phpcsFile , array $ useStatements , string $ className , string $ namespace , int $ startPtr ): void
335335 {
336- $ msg = 'Shorthand possible. Replace "%s" with "%s" ' ;
337- $ code = 'UnnecessaryNamespaceUsage ' ;
338- $ fixable = false ;
339- $ replaceClassName = false ;
340- $ replacement = '' ;
336+ $ msg = 'Shorthand possible. Replace "%s" with "%s" ' ;
337+ $ code = 'UnnecessaryNamespaceUsage ' ;
341338
342339 $ fullClassName = $ this ->getFullyQualifiedClassName ($ className );
343340
@@ -349,50 +346,37 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s
349346 $ replacement ,
350347 ];
351348
352- $ fixable = $ phpcsFile ->addFixableWarning (
349+ $ phpcsFile ->addFixableWarning (
353350 $ msg ,
354351 $ startPtr ,
355352 $ code ,
356353 $ data
357354 );
358-
359- $ replaceClassName = true ;
360355 } elseif ('' !== $ namespace && \str_starts_with ($ fullClassName , $ namespace )) {
361356 $ replacement = \substr ($ fullClassName , \strlen ($ namespace ));
362357
363- $ data = [
358+ $ data = [
364359 $ className ,
365360 $ replacement ,
366361 ];
367- $ fixable = $ phpcsFile ->addFixableWarning (
362+
363+ $ phpcsFile ->addFixableWarning (
368364 $ msg ,
369365 $ startPtr ,
370366 $ code ,
371367 $ data
372368 );
373- }
374-
375- if (true !== $ fixable ) {
369+ } else {
376370 return ;
377371 }
378372
379373 $ phpcsFile ->fixer ->beginChangeset ();
380374
381- if (true === $ isDocBlock ) {
382- $ tokens = $ phpcsFile ->getTokens ();
383- $ oldContent = $ tokens [$ startPtr ]['content ' ];
384- /** @var string $newContent */
385- $ newContent = \str_replace ($ className , $ replacement , $ oldContent );
386- $ phpcsFile ->fixer ->replaceToken ($ startPtr , $ newContent );
387- } else {
388- for ($ i = $ startPtr ; $ i < $ endPtr ; $ i ++) {
389- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
390- }
391-
392- if (true === $ replaceClassName ) {
393- $ phpcsFile ->fixer ->replaceToken ($ endPtr , $ replacement );
394- }
395- }
375+ $ tokens = $ phpcsFile ->getTokens ();
376+ $ oldContent = $ tokens [$ startPtr ]['content ' ];
377+ /** @var string $newContent */
378+ $ newContent = \str_replace ($ className , $ replacement , $ oldContent );
379+ $ phpcsFile ->fixer ->replaceToken ($ startPtr , $ newContent );
396380
397381 $ phpcsFile ->fixer ->endChangeset ();
398382 }
0 commit comments