|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Utils\PHPStan\Rule; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Node\InClassNode; |
| 10 | +use PHPStan\Rules\IdentifierRuleError; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 14 | +use Rector\Contract\Rector\RectorInterface; |
| 15 | +use RecursiveDirectoryIterator; |
| 16 | +use RecursiveIteratorIterator; |
| 17 | +use SplFileInfo; |
| 18 | + |
| 19 | +/** |
| 20 | + * Every Rector rule must be registered in a set or level config, otherwise it can |
| 21 | + * never run and becomes dead code. Registrations in sibling extension packages |
| 22 | + * (rector-doctrine, rector-symfony, ...) count too. Deprecated rules are skipped, |
| 23 | + * as they are intentionally unregistered. |
| 24 | + * |
| 25 | + * @implements Rule<InClassNode> |
| 26 | + * @see \Rector\Utils\PHPStan\Tests\Rule\RegisterRectorRule\RegisterRectorRuleTest |
| 27 | + */ |
| 28 | +final class RegisterRectorRule implements Rule |
| 29 | +{ |
| 30 | + 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.'; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string[] |
| 34 | + */ |
| 35 | + private readonly array $registrationDirectories; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var array<string, true>|null |
| 39 | + */ |
| 40 | + private ?array $registeredShortClassNames = null; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param string[]|null $registrationDirectories |
| 44 | + */ |
| 45 | + public function __construct(?array $registrationDirectories = null) |
| 46 | + { |
| 47 | + $root = __DIR__ . '/../../../../'; |
| 48 | + |
| 49 | + $this->registrationDirectories = $registrationDirectories ?? array_merge( |
| 50 | + [$root . 'config', $root . 'src/Config'], |
| 51 | + // sibling extension packages register rules in their own config sets |
| 52 | + glob($root . 'vendor/rector/*/config') ?: [] |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function getNodeType(): string |
| 57 | + { |
| 58 | + return InClassNode::class; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param InClassNode $node |
| 63 | + * @return list<IdentifierRuleError> |
| 64 | + */ |
| 65 | + public function processNode(Node $node, Scope $scope): array |
| 66 | + { |
| 67 | + $classReflection = $node->getClassReflection(); |
| 68 | + |
| 69 | + if ($classReflection->isAbstract()) { |
| 70 | + return []; |
| 71 | + } |
| 72 | + |
| 73 | + if (! $classReflection->implementsInterface(RectorInterface::class)) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + // test doubles are wired directly in their test, not through a set |
| 78 | + if (str_starts_with($classReflection->getName(), 'Rector\\Tests\\')) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + // intentionally unregistered |
| 83 | + if ($classReflection->implementsInterface(DeprecatedInterface::class)) { |
| 84 | + return []; |
| 85 | + } |
| 86 | + |
| 87 | + $className = $classReflection->getName(); |
| 88 | + if ($this->isRegistered($className)) { |
| 89 | + return []; |
| 90 | + } |
| 91 | + |
| 92 | + return [ |
| 93 | + RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $className)) |
| 94 | + ->identifier('rector.registerRector') |
| 95 | + ->build(), |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + private function isRegistered(string $className): bool |
| 100 | + { |
| 101 | + $shortClassName = substr((string) strrchr($className, '\\'), 1); |
| 102 | + |
| 103 | + // short class names are unique repo-wide (enforced by the "no duplicate short class names" check) |
| 104 | + return isset($this->getRegisteredShortClassNames()[$shortClassName]); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Collect every "SomeRector::class" short name referenced in the scanned config directories once. |
| 109 | + * The capture group stops at the preceding "\", so both "SomeRector::class" and a fully qualified |
| 110 | + * "\Some\Ns\SomeRector::class" yield the same short name. |
| 111 | + * |
| 112 | + * @return array<string, true> |
| 113 | + */ |
| 114 | + private function getRegisteredShortClassNames(): array |
| 115 | + { |
| 116 | + if ($this->registeredShortClassNames !== null) { |
| 117 | + return $this->registeredShortClassNames; |
| 118 | + } |
| 119 | + |
| 120 | + $registeredShortClassNames = []; |
| 121 | + foreach ($this->registrationDirectories as $registrationDirectory) { |
| 122 | + if (! is_dir($registrationDirectory)) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + $recursiveIterator = new RecursiveIteratorIterator( |
| 127 | + new RecursiveDirectoryIterator($registrationDirectory, RecursiveDirectoryIterator::SKIP_DOTS) |
| 128 | + ); |
| 129 | + |
| 130 | + /** @var SplFileInfo $fileInfo */ |
| 131 | + foreach ($recursiveIterator as $fileInfo) { |
| 132 | + if ($fileInfo->getExtension() !== 'php') { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + $fileContents = file_get_contents($fileInfo->getRealPath()); |
| 137 | + if ($fileContents === false) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + preg_match_all('#([A-Za-z_][A-Za-z0-9_]*)::class#', $fileContents, $matches); |
| 142 | + foreach ($matches[1] as $shortClassName) { |
| 143 | + $registeredShortClassNames[$shortClassName] = true; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + $this->registeredShortClassNames = $registeredShortClassNames; |
| 149 | + |
| 150 | + return $registeredShortClassNames; |
| 151 | + } |
| 152 | +} |
0 commit comments