@@ -42,11 +42,11 @@ class UnnecessaryNamespaceUsageSniff implements Sniff
42
42
/**
43
43
* Tokens used in full class name.
44
44
*
45
- * @var array<int, int>
46
45
*/
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 ,
50
50
];
51
51
52
52
/**
@@ -88,7 +88,9 @@ public function process(File $phpcsFile, $stackPtr): void
88
88
'@var ' => 2 ,
89
89
];
90
90
$ scanTokens = [
91
- T_NS_SEPARATOR ,
91
+ T_NAME_FULLY_QUALIFIED ,
92
+ T_NAME_QUALIFIED ,
93
+ T_NAME_RELATIVE ,
92
94
T_DOC_COMMENT_OPEN_TAG ,
93
95
];
94
96
@@ -100,17 +102,13 @@ public function process(File $phpcsFile, $stackPtr): void
100
102
101
103
while (false !== $ nsSep ) {
102
104
$ classNameEnd = (int ) $ phpcsFile ->findNext (
103
- $ this -> classNameTokens ,
105
+ self :: CLASS_NAME_TOKENS ,
104
106
$ nsSep ,
105
107
null ,
106
108
true
107
109
);
108
110
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 )) {
114
112
$ className = $ phpcsFile ->getTokensAsString (
115
113
$ nsSep ,
116
114
($ classNameEnd - $ nsSep )
@@ -122,7 +120,6 @@ public function process(File $phpcsFile, $stackPtr): void
122
120
$ className ,
123
121
$ namespace ,
124
122
$ nsSep ,
125
- ($ classNameEnd - 1 )
126
123
);
127
124
} else {
128
125
// Doc comment block.
@@ -193,8 +190,6 @@ public function process(File $phpcsFile, $stackPtr): void
193
190
$ typeToken ,
194
191
$ namespace ,
195
192
$ docCommentStringPtr ,
196
- $ docCommentStringPtr ,
197
- true
198
193
);
199
194
}
200
195
}
@@ -223,13 +218,13 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
223
218
224
219
while (false !== $ useTokenPtr ) {
225
220
$ classNameStart = (int ) $ phpcsFile ->findNext (
226
- PHP_CodeSniffer_Tokens::$ emptyTokens ,
221
+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
227
222
($ useTokenPtr + 1 ),
228
223
$ end ,
229
224
true
230
225
);
231
226
$ classNameEnd = $ phpcsFile ->findNext (
232
- $ this -> classNameTokens ,
227
+ self :: CLASS_NAME_TOKENS ,
233
228
($ classNameStart + 1 ),
234
229
$ end ,
235
230
true
@@ -255,7 +250,7 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
255
250
256
251
/** @var int $aliasNamePtr */
257
252
$ aliasNamePtr = $ phpcsFile ->findPrevious (
258
- PHP_CodeSniffer_Tokens::$ emptyTokens ,
253
+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
259
254
($ useEnd - 1 ),
260
255
0 ,
261
256
true
@@ -264,8 +259,15 @@ protected function getUseStatements(File $phpcsFile, int $start, int $end): arra
264
259
$ length = ($ classNameEnd - $ classNameStart );
265
260
$ className = $ phpcsFile ->getTokensAsString ($ classNameStart , $ length );
266
261
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 ;
269
271
$ i = ($ useEnd + 1 );
270
272
271
273
$ 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
287
289
{
288
290
$ namespace = (int ) $ phpcsFile ->findNext (T_NAMESPACE , $ start , $ end );
289
291
$ namespaceStart = $ phpcsFile ->findNext (
290
- PHP_CodeSniffer_Tokens::$ emptyTokens ,
292
+ PHP_CodeSniffer_Tokens::EMPTY_TOKENS ,
291
293
($ namespace + 1 ),
292
294
$ end ,
293
295
true
@@ -298,7 +300,7 @@ protected function getNamespace(File $phpcsFile, int $start, int $end): string
298
300
}
299
301
300
302
$ namespaceEnd = (int ) $ phpcsFile ->findNext (
301
- $ this -> classNameTokens ,
303
+ self :: CLASS_NAME_TOKENS ,
302
304
($ namespaceStart + 1 ),
303
305
$ end ,
304
306
true
@@ -330,18 +332,13 @@ private function getFullyQualifiedClassName(string $className): string
330
332
* @param string $className class name
331
333
* @param string $namespace name space
332
334
* @param int $startPtr start token pointer
333
- * @param int $endPtr end token pointer
334
- * @param bool $isDocBlock true if fixing doc block
335
335
*
336
336
* @return void
337
337
*/
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
339
339
{
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 ' ;
345
342
346
343
$ fullClassName = $ this ->getFullyQualifiedClassName ($ className );
347
344
@@ -353,50 +350,37 @@ private function checkShorthandPossible(File $phpcsFile, array $useStatements, s
353
350
$ replacement ,
354
351
];
355
352
356
- $ fixable = $ phpcsFile ->addFixableWarning (
353
+ $ phpcsFile ->addFixableWarning (
357
354
$ msg ,
358
355
$ startPtr ,
359
356
$ code ,
360
357
$ data
361
358
);
362
-
363
- $ replaceClassName = true ;
364
359
} elseif ('' !== $ namespace && \str_starts_with ($ fullClassName , $ namespace )) {
365
360
$ replacement = \substr ($ fullClassName , \strlen ($ namespace ));
366
361
367
- $ data = [
362
+ $ data = [
368
363
$ className ,
369
364
$ replacement ,
370
365
];
371
- $ fixable = $ phpcsFile ->addFixableWarning (
366
+
367
+ $ phpcsFile ->addFixableWarning (
372
368
$ msg ,
373
369
$ startPtr ,
374
370
$ code ,
375
371
$ data
376
372
);
377
- }
378
-
379
- if (true !== $ fixable ) {
373
+ } else {
380
374
return ;
381
375
}
382
376
383
377
$ phpcsFile ->fixer ->beginChangeset ();
384
378
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 );
400
384
401
385
$ phpcsFile ->fixer ->endChangeset ();
402
386
}
0 commit comments