-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDisallowFqnSniff.php
More file actions
628 lines (533 loc) · 19.8 KB
/
Copy pathDisallowFqnSniff.php
File metadata and controls
628 lines (533 loc) · 19.8 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
<?php
declare(strict_types=1);
namespace WebimpressCodingStandard\Sniffs\PHP;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use WebimpressCodingStandard\CodingStandard;
use WebimpressCodingStandard\Helper\NamespacesTrait;
use function array_merge;
use function basename;
use function dirname;
use function end;
use function explode;
use function file_exists;
use function glob;
use function implode;
use function in_array;
use function ltrim;
use function preg_match;
use function preg_match_all;
use function preg_quote;
use function preg_replace;
use function sprintf;
use function str_replace;
use function stripos;
use function strlen;
use function strpos;
use function strstr;
use function strtolower;
use function strtoupper;
use function substr;
use const DIRECTORY_SEPARATOR;
use const GLOB_NOSORT;
use const T_BITWISE_AND;
use const T_BITWISE_OR;
use const T_CASE;
use const T_CATCH;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSURE;
use const T_COLON;
use const T_COMMA;
use const T_DOC_COMMENT_STRING;
use const T_DOC_COMMENT_TAG;
use const T_DOC_COMMENT_WHITESPACE;
use const T_DOUBLE_COLON;
use const T_ECHO;
use const T_ELLIPSIS;
use const T_EXTENDS;
use const T_FN;
use const T_FUNCTION;
use const T_IMPLEMENTS;
use const T_INCLUDE;
use const T_INCLUDE_ONCE;
use const T_INSTANCEOF;
use const T_INSTEADOF;
use const T_LOGICAL_AND;
use const T_LOGICAL_OR;
use const T_LOGICAL_XOR;
use const T_NAMESPACE;
use const T_NEW;
use const T_NS_SEPARATOR;
use const T_NULLABLE;
use const T_OPEN_PARENTHESIS;
use const T_PRINT;
use const T_REQUIRE;
use const T_REQUIRE_ONCE;
use const T_RETURN;
use const T_STRING;
use const T_THROW;
use const T_USE;
use const T_VARIABLE;
class DisallowFqnSniff implements Sniff
{
use NamespacesTrait;
/**
* @var array Array of imported classes, constants and functions in current namespace.
*/
private $imported;
/**
* @var array Hash map of all php built in constant names.
*/
private $builtInConstants;
/**
* @var array Hash map of all php built in function names.
*/
private $builtInFunctions;
public function __construct()
{
$this->builtInConstants = $this->getBuiltInConstants();
$this->builtInFunctions = $this->getBuiltInFunctions();
}
/**
* @return int[]
*/
public function register() : array
{
return [
T_DOC_COMMENT_TAG,
T_NS_SEPARATOR,
];
}
/**
* @param int $stackPtr
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$namespace = '';
$currentNamespacePtr = null;
$toImport = [];
$toFix = [];
do {
$namespacePtr = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr - 1) ?: null;
if ($namespacePtr !== $currentNamespacePtr) {
$namespace = $namespacePtr ? $this->getName($phpcsFile, $namespacePtr + 1) : '';
if ($toImport || $toFix) {
$phpcsFile->fixer->beginChangeset();
if ($currentNamespacePtr) {
$this->importReferences($phpcsFile, $currentNamespacePtr, $toImport);
}
$this->fixErrors($phpcsFile, $toFix);
$phpcsFile->fixer->endChangeset();
}
$currentNamespacePtr = $namespacePtr;
$toImport = [];
$toFix = [];
$this->imported = $this->getGlobalUses($phpcsFile, $stackPtr, 'all');
}
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_TAG) {
$this->processTag($phpcsFile, $stackPtr, $namespace, $toImport, $toFix);
} else {
$this->processString($phpcsFile, $stackPtr, $namespace, $toImport, $toFix);
}
} while ($stackPtr = $phpcsFile->findNext($this->register(), $stackPtr + 1));
if ($toImport || $toFix) {
$phpcsFile->fixer->beginChangeset();
if ($currentNamespacePtr) {
$this->importReferences($phpcsFile, $currentNamespacePtr, $toImport);
}
$this->fixErrors($phpcsFile, $toFix);
$phpcsFile->fixer->endChangeset();
}
return $phpcsFile->numTokens + 1;
}
private function processTag(
File $phpcsFile,
int $stackPtr,
string $namespace,
array &$toImport,
array &$toFix
) : void {
$tokens = $phpcsFile->getTokens();
if (! in_array($tokens[$stackPtr]['content'], CodingStandard::TAG_WITH_TYPE, true)
|| $tokens[$stackPtr + 1]['code'] !== T_DOC_COMMENT_WHITESPACE
|| $tokens[$stackPtr + 2]['code'] !== T_DOC_COMMENT_STRING
) {
return;
}
$string = $tokens[$stackPtr + 2]['content'];
[$type] = explode(' ', $string);
$types = [$type];
if ($tokens[$stackPtr]['content'] === '@method'
&& preg_match_all('/(?<=[\s(,])[^(\s,]+?(?=\s+\$)/', $string, $matches)
) {
$types = array_merge($types, $matches[0]);
}
foreach ($types as $typesString) {
$typesArr = explode('|', $typesString);
// Create local array with classes to import, as we want to update main one only in fix mode
$localToImport = [];
$newTypesArr = [];
foreach ($typesArr as $name) {
$newTypesArr[] = $this->getExpectedName($phpcsFile, $stackPtr + 2, $namespace, $name, $localToImport);
}
$newTypes = implode('|', $newTypesArr);
if ($newTypes !== $typesString) {
$error = 'Invalid class name references: expected %s; found %s';
$data = [
$newTypes,
$typesString,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr + 2, 'InvalidInPhpDocs', $data);
if ($fix) {
// Update array with references to import
if ($localToImport) {
$toImport = array_merge($toImport, $localToImport);
}
$string = preg_replace(
'/(^|\s|,|\()' . preg_quote($typesString, '/') . '/',
'\\1' . $newTypes,
$string
);
$toFix[$stackPtr + 2] = $string;
}
}
}
}
private function getExpectedName(
File $phpcsFile,
int $stackPtr,
string $namespace,
string $name,
array &$toImport
) : string {
if (! $namespace) {
return ltrim($name, '\\');
}
if (strpos($name, '\\') !== 0) {
return $name;
}
$suffix = strstr($name, '[');
if ($suffix && ! preg_match('/^(\[\])+$/', $suffix)) {
return $name;
}
$name = str_replace(['[', ']'], '', ltrim($name, '\\'));
if (stripos($name . '\\', $namespace . '\\') === 0) {
return substr($name, strlen($namespace) + 1) . $suffix;
}
$alias = $this->getAliasFromName($name);
foreach ($this->imported['class'] ?? [] as $class) {
// If namespace or part of it is already imported
if (stripos($name . '\\', $class['fqn'] . '\\') === 0) {
return $class['name'] . $suffix;
}
}
// We can't suggest anything in that case
if (! $this->isValidClassName($phpcsFile, $stackPtr, $alias, $name)) {
return '\\' . $name . $suffix;
}
// We need to import it
$toImport += $this->import('class', $name, $alias);
return $alias . $suffix;
}
private function processString(
File $phpcsFile,
int $stackPtr,
string $namespace,
array &$toImport,
array &$toFix
) : void {
$tokens = $phpcsFile->getTokens();
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, $stackPtr - 1, null, true);
// Part of the name
if ($tokens[$prev]['code'] === T_STRING || $tokens[$prev]['code'] === T_NAMESPACE) {
return;
}
// In the global use statement
if ($tokens[$prev]['code'] === T_USE && CodingStandard::isGlobalUse($phpcsFile, $prev)) {
return;
}
if (! $namespace) {
$error = 'FQN is not needed here, as file not have defined namespace';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NoNamespace');
if ($fix) {
$phpcsFile->fixer->replaceToken($stackPtr, '');
}
return;
}
$next = $phpcsFile->findNext(
Tokens::$emptyTokens + [T_NS_SEPARATOR => T_NS_SEPARATOR, T_STRING => T_STRING],
$stackPtr + 1,
null,
true
);
$prevClassTokens = [
T_NEW,
T_USE,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_NULLABLE,
];
$functionTokens = [
T_CLOSURE,
T_FN,
T_FUNCTION,
];
if (in_array($tokens[$prev]['code'], $prevClassTokens, true)
|| in_array($tokens[$next]['code'], [T_VARIABLE, T_ELLIPSIS, T_DOUBLE_COLON], true)
) {
$type = 'class';
} elseif ($tokens[$next]['code'] === T_OPEN_PARENTHESIS) {
$type = 'function';
} else {
$type = 'const';
if ($tokens[$prev]['code'] === T_COLON) {
$before = $phpcsFile->findPrevious(Tokens::$emptyTokens, $prev - 1, null, true);
if ($tokens[$before]['code'] === T_CLOSE_PARENTHESIS
&& isset($tokens[$before]['parenthesis_owner'])
&& in_array($tokens[$tokens[$before]['parenthesis_owner']]['code'], $functionTokens, true)
) {
$type = 'class';
}
} elseif ($tokens[$next]['code'] === T_BITWISE_AND) {
if (! empty($tokens[$stackPtr]['nested_parenthesis'])
&& ($owner = $tokens[end($tokens[$stackPtr]['nested_parenthesis'])]['parenthesis_owner'] ?? 0)
&& in_array($tokens[$owner]['code'], $functionTokens, true)
) {
$type = 'class';
}
} elseif ($tokens[$next]['code'] === T_BITWISE_OR) {
if (! empty($tokens[$stackPtr]['nested_parenthesis'])
&& ($owner = $tokens[end($tokens[$stackPtr]['nested_parenthesis'])]['parenthesis_owner'] ?? 0)
&& $tokens[$owner]['code'] === T_CATCH
) {
$type = 'class';
}
} elseif ($tokens[$prev]['code'] === T_COMMA) {
$before = $phpcsFile->findPrevious(
Tokens::$emptyTokens + [T_STRING => T_STRING, T_NS_SEPARATOR => T_NS_SEPARATOR],
$prev - 1,
null,
true
);
if ($tokens[$before]['code'] === T_IMPLEMENTS) {
$type = 'class';
}
}
}
$name = $this->getName($phpcsFile, $stackPtr);
// If the found name is in the same namespace
if (stripos($name . '\\', $namespace . '\\') === 0) {
$error = 'FQN is disallowed for %s in namespace %s';
$data = [
$name,
$namespace,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SameNamespace', $data);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($stackPtr, substr($name, strlen($namespace) + 1));
$i = $stackPtr;
while (isset($tokens[++$i])) {
if (in_array($tokens[$i]['code'], Tokens::$emptyTokens, true)) {
continue;
}
if (! in_array($tokens[$i]['code'], [T_NS_SEPARATOR, T_STRING], true)) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
}
return;
}
// If function is built-in function; skip
if ($type === 'function' && isset($this->builtInFunctions[strtolower($name)])) {
return;
}
// If constant is built-in constant; skip
if ($type === 'const' && isset($this->builtInConstants[strtoupper($name)])) {
return;
}
foreach ($this->imported['class'] ?? [] as $class) {
// If namespace or part of it is already imported
if (stripos($name . '\\', $class['fqn'] . '\\') === 0) {
$error = 'Namespace %s is already imported';
$data = [$class['fqn']];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NamespaceImported', $data);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$this->fixError($phpcsFile, $stackPtr, $class['name']);
$phpcsFile->fixer->endChangeset();
}
return;
}
}
$alias = $this->getAliasFromName($name);
if ($type === 'function') {
foreach ($this->imported['function'] ?? [] as $function) {
// If function is already imported
if (strtolower($function['fqn']) === strtolower($name)) {
$error = 'Function %s is already imported';
$data = [$function['fqn']];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'FunctionImported', $data);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$this->fixError($phpcsFile, $stackPtr, $function['name']);
$phpcsFile->fixer->endChangeset();
}
return;
}
}
// If alias is in use
if (isset($this->imported['function'][strtolower($alias)])) {
$error = 'Function %s must be imported, but alias %s is already in use';
$data = [$name, $alias];
$phpcsFile->addError($error, $stackPtr, 'FunctionAliasUsed', $data);
return;
}
}
if ($type === 'const') {
foreach ($this->imported['const'] ?? [] as $const) {
// If constant is already imported
if (strtolower($const['fqn']) === strtolower($name)) {
$error = 'Constant %s is already imported';
$data = [$const['fqn']];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'ConstantImported', $data);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$this->fixError($phpcsFile, $stackPtr, $const['name']);
$phpcsFile->fixer->endChangeset();
}
return;
}
}
// If alias is in use
if (isset($this->imported['const'][strtoupper($alias)])) {
$error = 'Constant %s must be imported, but alias %s is already in use';
$data = [$name, $alias];
$phpcsFile->addError($error, $stackPtr, 'ConstantAliasUsed', $data);
return;
}
}
if ($type === 'class' && ! $this->isValidClassName($phpcsFile, $stackPtr, $alias, $name)) {
return;
}
$error = '%s must be imported as %s';
$data = [$name, $alias];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'Import', $data);
if ($fix) {
$toFix[$stackPtr] = $alias;
$toImport += $this->import($type, $name, $alias);
}
}
private function isValidClassName(File $phpcsFile, int $stackPtr, string $alias, string $name) : bool
{
// If alias is in use
if (isset($this->imported['class'][strtolower($alias)])) {
$error = 'Class %s must be imported, but alias %s is already in use';
$data = [$name, $alias];
$phpcsFile->addError($error, $stackPtr, 'ClassAliasUsed', $data);
return false;
}
$dirname = dirname($phpcsFile->getFilename());
if (file_exists($dirname . DIRECTORY_SEPARATOR . $alias)) {
$error = '%s must be imported but directory with name %s exists in the namespace';
$data = [$name, $alias];
$phpcsFile->addError($error, $stackPtr, 'DirName', $data);
return false;
}
$files = glob($dirname . '/*', GLOB_NOSORT);
foreach ($files as $file) {
if (stripos(basename($file), $alias . '.') === 0) {
$error = '%s must be imported but file with name %s exists in the namespace';
$data = [$name, $alias];
$phpcsFile->addError($error, $stackPtr, 'FileName', $data);
return false;
}
}
return true;
}
private function fixError(File $phpcsFile, int $stackPtr, string $expected) : void
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) {
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
return;
}
if (in_array($tokens[$stackPtr - 1]['code'], [
T_NEW,
T_USE,
T_EXTENDS,
T_IMPLEMENTS,
T_INSTANCEOF,
T_INSTEADOF,
T_CASE,
T_PRINT,
T_ECHO,
T_REQUIRE,
T_REQUIRE_ONCE,
T_INCLUDE,
T_INCLUDE_ONCE,
T_RETURN,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_THROW,
], true)) {
$expected = ' ' . $expected;
}
$phpcsFile->fixer->replaceToken($stackPtr, $expected);
$i = $stackPtr;
while (isset($tokens[++$i])) {
if (in_array($tokens[$i]['code'], Tokens::$emptyTokens, true)) {
continue;
}
if (! in_array($tokens[$i]['code'], [T_NS_SEPARATOR, T_STRING], true)) {
break;
}
$phpcsFile->fixer->replaceToken($i, '');
}
}
private function import(string $type, string $fqn, string $alias) : array
{
$this->imported[$type][$type === 'const' ? strtoupper($alias) : strtolower($alias)] = [
'name' => $alias,
'fqn' => $fqn,
];
return [$fqn => $type];
}
/**
* @phpstan-param array<string, string> $references
*/
private function importReferences(File $phpcsFile, int $namespacePtr, array $references) : void
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$namespacePtr]['scope_opener'])) {
$ptr = $tokens[$namespacePtr]['scope_opener'];
} else {
$ptr = $phpcsFile->findEndOfStatement($namespacePtr);
$phpcsFile->fixer->addNewline($ptr);
}
$content = '';
foreach ($references as $fqn => $type) {
$content .= sprintf(
'%suse %s%s;',
$phpcsFile->eolChar,
$type === 'class' ? '' : $type . ' ',
$fqn
);
}
$phpcsFile->fixer->addContent($ptr, $content);
}
private function fixErrors(File $phpcsFile, array $replacements)
{
foreach ($replacements as $ptr => $alias) {
$this->fixError($phpcsFile, $ptr, $alias);
}
}
}