@@ -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 /**
@@ -88,7 +88,9 @@ public function process(File $phpcsFile, $stackPtr): void
8888 '@var ' => 2 ,
8989 ];
9090 $ scanTokens = [
91- T_NS_SEPARATOR ,
91+ T_NAME_FULLY_QUALIFIED ,
92+ T_NAME_QUALIFIED ,
93+ T_NAME_RELATIVE ,
9294 T_DOC_COMMENT_OPEN_TAG ,
9395 ];
9496
@@ -100,17 +102,13 @@ public function process(File $phpcsFile, $stackPtr): void
100102
101103 while (false !== $ nsSep ) {
102104 $ classNameEnd = (int ) $ phpcsFile ->findNext (
103- $ this -> classNameTokens ,
105+ self :: CLASS_NAME_TOKENS ,
104106 $ nsSep ,
105107 null ,
106108 true
107109 );
108110
109- if (T_NS_SEPARATOR === $ tokens [$ nsSep ]['code ' ]) {
110- if (T_STRING === $ tokens [($ nsSep - 1 )]['code ' ]) {
111- --$ nsSep ;
112- }
113-
111+ if (\in_array ($ tokens [$ nsSep ]['code ' ], self ::CLASS_NAME_TOKENS , true )) {
114112 $ className = $ phpcsFile ->getTokensAsString (
115113 $ nsSep ,
116114 ($ classNameEnd - $ nsSep )
@@ -122,7 +120,6 @@ public function process(File $phpcsFile, $stackPtr): void
122120 $ className ,
123121 $ namespace ,
124122 $ nsSep ,
125- ($ classNameEnd - 1 )
126123 );
127124 } else {
128125 // Doc comment block.
@@ -193,8 +190,6 @@ public function process(File $phpcsFile, $stackPtr): void
193190 $ typeToken ,
194191 $ namespace ,
195192 $ docCommentStringPtr ,
196- $ docCommentStringPtr ,
197- true
198193 );
199194 }
200195 }
@@ -223,13 +218,13 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
223218
224219 while (false !== $ useTokenPtr ) {
225220 $ classNameStart = (int ) $ phpcsFile ->findNext (
226- PHP_CodeSniffer_Tokens::$ emptyTokens ,
221+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
227222 ($ useTokenPtr + 1 ),
228223 $ end ,
229224 true
230225 );
231226 $ classNameEnd = $ phpcsFile ->findNext (
232- $ this -> classNameTokens ,
227+ self :: CLASS_NAME_TOKENS ,
233228 ($ classNameStart + 1 ),
234229 $ end ,
235230 true
@@ -255,7 +250,7 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
255250
256251 /** @var int $aliasNamePtr */
257252 $ aliasNamePtr = $ phpcsFile ->findPrevious (
258- PHP_CodeSniffer_Tokens::$ emptyTokens ,
253+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
259254 ($ useEnd - 1 ),
260255 0 ,
261256 true
@@ -264,8 +259,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
264259 $ length = ($ classNameEnd - $ classNameStart );
265260 $ className = $ phpcsFile ->getTokensAsString ($ classNameStart , $ length );
266261
267- $ className = $ this ->getFullyQualifiedClassName ($ className );
268- $ useStatements [$ className ] = $ tokens [$ aliasNamePtr ]['content ' ];
262+ $ className = $ this ->getFullyQualifiedClassName ($ className );
263+ $ tokenContent = $ tokens [$ aliasNamePtr ]['content ' ];
264+
265+ if (\str_contains ($ tokenContent , '\\' )) {
266+ $ path = \explode ('\\' , $ tokenContent );
267+ $ tokenContent = $ path [\array_key_last ($ path )];
268+ }
269+
270+ $ useStatements [$ className ] = $ tokenContent ;
269271 $ i = ($ useEnd + 1 );
270272
271273 $ useTokenPtr = T_COMMA === $ tokens [$ useEnd ]['code ' ] ? $ i : $ phpcsFile ->findNext (T_USE , $ i , $ end );
@@ -287,7 +289,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
287289 {
288290 $ namespace = (int ) $ phpcsFile ->findNext (T_NAMESPACE , $ start , $ end );
289291 $ namespaceStart = $ phpcsFile ->findNext (
290- PHP_CodeSniffer_Tokens::$ emptyTokens ,
292+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
291293 ($ namespace + 1 ),
292294 $ end ,
293295 true
@@ -298,7 +300,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
298300 }
299301
300302 $ namespaceEnd = (int ) $ phpcsFile ->findNext (
301- $ this -> classNameTokens ,
303+ self :: CLASS_NAME_TOKENS ,
302304 ($ namespaceStart + 1 ),
303305 $ end ,
304306 true
@@ -330,18 +332,13 @@ private function getFullyQualifiedClassName(string $className): string
330332 * @param string $className class name
331333 * @param string $namespace name space
332334 * @param int $startPtr start token pointer
333- * @param int $endPtr end token pointer
334- * @param bool $isDocBlock true if fixing doc block
335335 *
336336 * @return void
337337 */
338- private function checkShorthandPossible (File $ phpcsFile , array $ useStatements , string $ className , string $ namespace , int $ startPtr, int $ endPtr , bool $ isDocBlock = false ): void
338+ private function checkShorthandPossible (File $ phpcsFile , array $ useStatements , string $ className , string $ namespace , int $ startPtr ): void
339339 {
340- $ msg = 'Shorthand possible. Replace "%s" with "%s" ' ;
341- $ code = 'UnnecessaryNamespaceUsage ' ;
342- $ fixable = false ;
343- $ replaceClassName = false ;
344- $ replacement = '' ;
340+ $ msg = 'Shorthand possible. Replace "%s" with "%s" ' ;
341+ $ code = 'UnnecessaryNamespaceUsage ' ;
345342
346343 $ fullClassName = $ this ->getFullyQualifiedClassName ($ className );
347344
@@ -353,50 +350,37 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s
353350 $ replacement ,
354351 ];
355352
356- $ fixable = $ phpcsFile ->addFixableWarning (
353+ $ phpcsFile ->addFixableWarning (
357354 $ msg ,
358355 $ startPtr ,
359356 $ code ,
360357 $ data
361358 );
362-
363- $ replaceClassName = true ;
364359 } elseif ('' !== $ namespace && \str_starts_with ($ fullClassName , $ namespace )) {
365360 $ replacement = \substr ($ fullClassName , \strlen ($ namespace ));
366361
367- $ data = [
362+ $ data = [
368363 $ className ,
369364 $ replacement ,
370365 ];
371- $ fixable = $ phpcsFile ->addFixableWarning (
366+
367+ $ phpcsFile ->addFixableWarning (
372368 $ msg ,
373369 $ startPtr ,
374370 $ code ,
375371 $ data
376372 );
377- }
378-
379- if (true !== $ fixable ) {
373+ } else {
380374 return ;
381375 }
382376
383377 $ phpcsFile ->fixer ->beginChangeset ();
384378
385- if (true === $ isDocBlock ) {
386- $ tokens = $ phpcsFile ->getTokens ();
387- $ oldContent = $ tokens [$ startPtr ]['content ' ];
388- /** @var string $newContent */
389- $ newContent = \str_replace ($ className , $ replacement , $ oldContent );
390- $ phpcsFile ->fixer ->replaceToken ($ startPtr , $ newContent );
391- } else {
392- for ($ i = $ startPtr ; $ i < $ endPtr ; $ i ++) {
393- $ phpcsFile ->fixer ->replaceToken ($ i , '' );
394- }
395-
396- if (true === $ replaceClassName ) {
397- $ phpcsFile ->fixer ->replaceToken ($ endPtr , $ replacement );
398- }
399- }
379+ $ tokens = $ phpcsFile ->getTokens ();
380+ $ oldContent = $ tokens [$ startPtr ]['content ' ];
381+ /** @var string $newContent */
382+ $ newContent = \str_replace ($ className , $ replacement , $ oldContent );
383+ $ phpcsFile ->fixer ->replaceToken ($ startPtr , $ newContent );
400384
401385 $ phpcsFile ->fixer ->endChangeset ();
402386 }
0 commit comments