Repository navigation
Expand file tree
/
Copy pathRegisterRectorRule.php
More file actions
152 lines (126 loc) · 4.86 KB
/
Copy pathRegisterRectorRule.php
File metadata and controls
152 lines (126 loc) · 4.86 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
<?php
declare(strict_types=1);
namespace Rector\Utils\PHPStan\Rule;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\RectorInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
/**
* Every Rector rule must be registered in a set or level config, otherwise it can
* never run and becomes dead code. Registrations in sibling extension packages
* (rector-doctrine, rector-symfony, ...) count too. Deprecated rules are skipped,
* as they are intentionally unregistered.
*
* @implements Rule<InClassNode>
* @see \Rector\Utils\PHPStan\Tests\Rule\RegisterRectorRule\RegisterRectorRuleTest
*/
final class RegisterRectorRule implements Rule
{
private const string ERROR_MESSAGE = 'Rector rule "%s" is not registered in any set or level config, so it can never run. Register it in a set/level, or deprecate it if it is no longer wanted.';
/**
* @var string[]
*/
private readonly array $registrationDirectories;
/**
* @var array<string, true>|null
*/
private ?array $registeredShortClassNames = null;
/**
* @param string[]|null $registrationDirectories
*/
public function __construct(?array $registrationDirectories = null)
{
$root = __DIR__ . '/../../../../';
$this->registrationDirectories = $registrationDirectories ?? array_merge(
[$root . 'config', $root . 'src/Config'],
// sibling extension packages register rules in their own config sets
glob($root . 'vendor/rector/*/config') ?: []
);
}
public function getNodeType(): string
{
return InClassNode::class;
}
/**
* @param InClassNode $node
* @return list<IdentifierRuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
$classReflection = $node->getClassReflection();
if ($classReflection->isAbstract()) {
return [];
}
if (! $classReflection->implementsInterface(RectorInterface::class)) {
return [];
}
// test doubles are wired directly in their test, not through a set
if (str_starts_with($classReflection->getName(), 'Rector\\Tests\\')) {
return [];
}
// intentionally unregistered
if ($classReflection->implementsInterface(DeprecatedInterface::class)) {
return [];
}
$className = $classReflection->getName();
if ($this->isRegistered($className)) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $className))
->identifier('rector.registerRector')
->build(),
];
}
private function isRegistered(string $className): bool
{
$shortClassName = substr((string) strrchr($className, '\\'), 1);
// short class names are unique repo-wide (enforced by the "no duplicate short class names" check)
return isset($this->getRegisteredShortClassNames()[$shortClassName]);
}
/**
* Collect every "SomeRector::class" short name referenced in the scanned config directories once.
* The capture group stops at the preceding "\", so both "SomeRector::class" and a fully qualified
* "\Some\Ns\SomeRector::class" yield the same short name.
*
* @return array<string, true>
*/
private function getRegisteredShortClassNames(): array
{
if ($this->registeredShortClassNames !== null) {
return $this->registeredShortClassNames;
}
$registeredShortClassNames = [];
foreach ($this->registrationDirectories as $registrationDirectory) {
if (! is_dir($registrationDirectory)) {
continue;
}
$recursiveIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($registrationDirectory, RecursiveDirectoryIterator::SKIP_DOTS)
);
/** @var SplFileInfo $fileInfo */
foreach ($recursiveIterator as $fileInfo) {
if ($fileInfo->getExtension() !== 'php') {
continue;
}
$fileContents = file_get_contents($fileInfo->getRealPath());
if ($fileContents === false) {
continue;
}
preg_match_all('#([A-Za-z_][A-Za-z0-9_]*)::class#', $fileContents, $matches);
foreach ($matches[1] as $shortClassName) {
$registeredShortClassNames[$shortClassName] = true;
}
}
}
$this->registeredShortClassNames = $registeredShortClassNames;
return $registeredShortClassNames;
}
}