-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUnnecessaryNamespaceUsageSniff.php
401 lines (349 loc) · 12.1 KB
/
UnnecessaryNamespaceUsageSniff.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/**
* This file is part of the mo4-coding-standard (phpcs standard)
*
* @author Xaver Loppenstedt <[email protected]>
*
* @license http://spdx.org/licenses/MIT MIT License
*
* @link https://github.com/mayflower/mo4-coding-standard
*/
declare(strict_types=1);
namespace MO4\Sniffs\Formatting;
use MO4\Library\PregLibrary;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens as PHP_CodeSniffer_Tokens;
/**
* Unnecessary Namespace Usage sniff.
*
* Full namespace declaration should be skipped in favour of the short declaration.
*
* @author Xaver Loppenstedt <[email protected]>
* @author Marco Jantke <[email protected]>
* @author Steffen Ritter <[email protected]>
*
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
*
* @license http://spdx.org/licenses/MIT MIT License
*
* @link https://github.com/mayflower/mo4-coding-standard
*
* @psalm-api
*/
class UnnecessaryNamespaceUsageSniff implements Sniff
{
/**
* Tokens used in full class name.
*
* @var array<int, int>
*/
private $classNameTokens = [
T_NS_SEPARATOR,
T_STRING,
];
/**
* Registers the tokens that this sniff wants to listen for.
*
* @return array<int, int>
*
* @see Tokens.php
*/
public function register(): array
{
return [T_CLASS];
}
/**
* Called when one of the token types that this sniff is listening for
* is found.
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint
*
* @param File $phpcsFile The PHP_CodeSniffer file where the
* token was found.
* @param int $stackPtr The position in the PHP_CodeSniffer
* file's token stack where the token
* was found.
*
* @return void
*
* @throws RuntimeException
*/
public function process(File $phpcsFile, $stackPtr): void
{
$docCommentTags = [
'@param' => 1,
'@return' => 1,
'@throws' => 1,
'@var' => 2,
];
$scanTokens = [
T_NS_SEPARATOR,
T_DOC_COMMENT_OPEN_TAG,
];
$tokens = $phpcsFile->getTokens();
$useStatements = $this->getUseStatements($phpcsFile, 0, ($stackPtr - 1));
$namespace = $this->getNamespace($phpcsFile, 0, ($stackPtr - 1));
$nsSep = $phpcsFile->findNext($scanTokens, ($stackPtr + 1));
while (false !== $nsSep) {
$classNameEnd = (int) $phpcsFile->findNext(
$this->classNameTokens,
$nsSep,
null,
true
);
if (T_NS_SEPARATOR === $tokens[$nsSep]['code']) {
if (T_STRING === $tokens[($nsSep - 1)]['code']) {
--$nsSep;
}
$className = $phpcsFile->getTokensAsString(
$nsSep,
($classNameEnd - $nsSep)
);
$this->checkShorthandPossible(
$phpcsFile,
$useStatements,
$className,
$namespace,
$nsSep,
($classNameEnd - 1)
);
} else {
// Doc comment block.
foreach ($tokens[$nsSep]['comment_tags'] as $tag) {
$content = $tokens[$tag]['content'];
if (!\array_key_exists($content, $docCommentTags)) {
continue;
}
$next = ($tag + 1);
// PHP Code Sniffer will magically add T_DOC_COMMENT_CLOSE_TAG with empty string content.
$lineEnd = $phpcsFile->findNext(
[
T_DOC_COMMENT_CLOSE_TAG,
T_DOC_COMMENT_STAR,
],
$next
);
$docCommentStringPtr = $phpcsFile->findNext(
[T_DOC_COMMENT_STRING],
$next,
(int) $lineEnd
);
if (false === $docCommentStringPtr) {
continue;
}
$docLine = $tokens[$docCommentStringPtr]['content'];
$docLineTokens = PregLibrary::MO4PregSplit(
'/\s+/',
$docLine,
-1,
PREG_SPLIT_NO_EMPTY
);
// phpcs:disable
/** @var array<string> $docLineTokens */
$docLineTokens = \array_slice(
$docLineTokens,
0,
$docCommentTags[$content]
);
// phpcs:enable
foreach ($docLineTokens as $docLineToken) {
// phpcs:disable
/** @var array<string> $typeTokens */
$typeTokens = PregLibrary::MO4PregSplit(
'/\|/',
$docLineToken,
-1,
PREG_SPLIT_NO_EMPTY
);
// phpcs:enable
foreach ($typeTokens as $typeToken) {
if (\in_array($typeToken, $useStatements, true)) {
continue;
}
$this->checkShorthandPossible(
$phpcsFile,
$useStatements,
$typeToken,
$namespace,
$docCommentStringPtr,
$docCommentStringPtr,
true
);
}
}
}
}
$nsSep = $phpcsFile->findNext($scanTokens, ($classNameEnd + 1));
}
}
/**
* Get all use statements in range
*
* @param File $phpcsFile PHP CS File
* @param int $start start pointer
* @param int $end end pointer
*
* @return array
*/
protected function getUseStatements(File $phpcsFile, int $start, int $end): array
{
$useStatements = [];
$i = $start;
$tokens = $phpcsFile->getTokens();
$useTokenPtr = $phpcsFile->findNext(T_USE, $i, $end);
while (false !== $useTokenPtr) {
$classNameStart = (int) $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
($useTokenPtr + 1),
$end,
true
);
$classNameEnd = $phpcsFile->findNext(
$this->classNameTokens,
($classNameStart + 1),
$end,
true
);
if (false === $classNameEnd) {
break;
}
$useEnd = $phpcsFile->findNext(
[
T_SEMICOLON,
T_COMMA,
],
$classNameEnd,
$end
);
// Prevent endless loop when 'use ;' is the last use statement.
if (false === $useEnd) {
break;
}
/** @var int $aliasNamePtr */
$aliasNamePtr = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
($useEnd - 1),
0,
true
);
$length = ($classNameEnd - $classNameStart);
$className = $phpcsFile->getTokensAsString($classNameStart, $length);
$className = $this->getFullyQualifiedClassName($className);
$useStatements[$className] = $tokens[$aliasNamePtr]['content'];
$i = ($useEnd + 1);
$useTokenPtr = T_COMMA === $tokens[$useEnd]['code'] ? $i : $phpcsFile->findNext(T_USE, $i, $end);
}
return $useStatements;
}
/**
* Get the namespace of the current class file
*
* @param File $phpcsFile PHP CS File
* @param int $start start pointer
* @param int $end end pointer
*
* @return string
*/
protected function getNamespace(File $phpcsFile, int $start, int $end): string
{
$namespace = (int) $phpcsFile->findNext(T_NAMESPACE, $start, $end);
$namespaceStart = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
($namespace + 1),
$end,
true
);
if (false === $namespaceStart) {
return '';
}
$namespaceEnd = (int) $phpcsFile->findNext(
$this->classNameTokens,
($namespaceStart + 1),
$end,
true
);
$nslen = ($namespaceEnd - $namespaceStart);
$name = $phpcsFile->getTokensAsString($namespaceStart, $nslen);
return "\\{$name}\\";
}
/**
* Return the fully qualified class name, e.g. '\Foo\Bar\Faz'
*
* @param string $className class name
*
* @return string
*/
private function getFullyQualifiedClassName(string $className): string
{
return '\\' !== $className[0] ? "\\{$className}" : $className;
}
/**
* Check if short hand is possible.
*
* @param File $phpcsFile PHP CS File
* @param array $useStatements array with class use statements
* @param string $className class name
* @param string $namespace name space
* @param int $startPtr start token pointer
* @param int $endPtr end token pointer
* @param bool $isDocBlock true if fixing doc block
*
* @return void
*/
private function checkShorthandPossible(File $phpcsFile, array $useStatements, string $className, string $namespace, int $startPtr, int $endPtr, bool $isDocBlock = false): void
{
$msg = 'Shorthand possible. Replace "%s" with "%s"';
$code = 'UnnecessaryNamespaceUsage';
$fixable = false;
$replaceClassName = false;
$replacement = '';
$fullClassName = $this->getFullyQualifiedClassName($className);
if (\array_key_exists($fullClassName, $useStatements)) {
$replacement = $useStatements[$fullClassName];
$data = [
$className,
$replacement,
];
$fixable = $phpcsFile->addFixableWarning(
$msg,
$startPtr,
$code,
$data
);
$replaceClassName = true;
} elseif ('' !== $namespace && \str_starts_with($fullClassName, $namespace)) {
$replacement = \substr($fullClassName, \strlen($namespace));
$data = [
$className,
$replacement,
];
$fixable = $phpcsFile->addFixableWarning(
$msg,
$startPtr,
$code,
$data
);
}
if (true !== $fixable) {
return;
}
$phpcsFile->fixer->beginChangeset();
if (true === $isDocBlock) {
$tokens = $phpcsFile->getTokens();
$oldContent = $tokens[$startPtr]['content'];
/** @var string $newContent */
$newContent = \str_replace($className, $replacement, $oldContent);
$phpcsFile->fixer->replaceToken($startPtr, $newContent);
} else {
for ($i = $startPtr; $i < $endPtr; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
if (true === $replaceClassName) {
$phpcsFile->fixer->replaceToken($endPtr, $replacement);
}
}
$phpcsFile->fixer->endChangeset();
}
}