|
| 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, and in third-party extensions |
| 23 | + * (rector-laravel, drupal-rector) when they are installed. Deprecated rules are |
| 24 | + * skipped, as they are intentionally unregistered. |
| 25 | + * |
| 26 | + * @implements Rule<InClassNode> |
| 27 | + * @see \Rector\Utils\PHPStan\Tests\Rule\RegisterRectorRule\RegisterRectorRuleTest |
| 28 | + */ |
| 29 | +final class RegisterRectorRule implements Rule |
| 30 | +{ |
| 31 | + 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.'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string[] |
| 35 | + */ |
| 36 | + private readonly array $registrationDirectories; |
| 37 | + |
| 38 | + private ?string $registrationContents = null; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param string[]|null $registrationDirectories |
| 42 | + */ |
| 43 | + public function __construct(?array $registrationDirectories = null) |
| 44 | + { |
| 45 | + $root = __DIR__ . '/../../../../'; |
| 46 | + |
| 47 | + $this->registrationDirectories = $registrationDirectories ?? array_merge( |
| 48 | + [$root . 'config', $root . 'src/Config'], |
| 49 | + // sibling extension packages register rules in their own config sets |
| 50 | + glob($root . 'vendor/rector/*/config') ?: [], |
| 51 | + // third-party extension packages, e.g. rector-laravel and drupal-rector, when installed |
| 52 | + glob($root . 'vendor/driftingly/rector-laravel/config') ?: [], |
| 53 | + glob($root . 'vendor/palantirnet/drupal-rector/config') ?: [] |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function getNodeType(): string |
| 58 | + { |
| 59 | + return InClassNode::class; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param InClassNode $node |
| 64 | + * @return list<IdentifierRuleError> |
| 65 | + */ |
| 66 | + public function processNode(Node $node, Scope $scope): array |
| 67 | + { |
| 68 | + $classReflection = $node->getClassReflection(); |
| 69 | + |
| 70 | + if ($classReflection->isAbstract()) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + if (! $classReflection->implementsInterface(RectorInterface::class)) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + // test doubles are wired directly in their test, not through a set |
| 79 | + if (str_starts_with($classReflection->getName(), 'Rector\\Tests\\')) { |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + // intentionally unregistered |
| 84 | + if ($classReflection->implementsInterface(DeprecatedInterface::class)) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + $className = $classReflection->getName(); |
| 89 | + if ($this->isRegistered($className)) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + return [ |
| 94 | + RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $className)) |
| 95 | + ->identifier('rector.registerRector') |
| 96 | + ->build(), |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + private function isRegistered(string $className): bool |
| 101 | + { |
| 102 | + $shortClassName = substr((string) strrchr($className, '\\'), 1); |
| 103 | + |
| 104 | + // match "ShortName::class", also as a fully qualified "\Some\Ns\ShortName::class", |
| 105 | + // but not a longer class name ending with it, e.g. "SubShortName::class" |
| 106 | + $pattern = '#(?<![a-zA-Z0-9_])' . preg_quote($shortClassName, '#') . '::class#'; |
| 107 | + |
| 108 | + return preg_match($pattern, $this->getRegistrationContents()) === 1; |
| 109 | + } |
| 110 | + |
| 111 | + private function getRegistrationContents(): string |
| 112 | + { |
| 113 | + if ($this->registrationContents !== null) { |
| 114 | + return $this->registrationContents; |
| 115 | + } |
| 116 | + |
| 117 | + $contents = ''; |
| 118 | + foreach ($this->registrationDirectories as $registrationDirectory) { |
| 119 | + if (! is_dir($registrationDirectory)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + $recursiveIterator = new RecursiveIteratorIterator( |
| 124 | + new RecursiveDirectoryIterator($registrationDirectory, RecursiveDirectoryIterator::SKIP_DOTS) |
| 125 | + ); |
| 126 | + |
| 127 | + /** @var SplFileInfo $fileInfo */ |
| 128 | + foreach ($recursiveIterator as $fileInfo) { |
| 129 | + if ($fileInfo->getExtension() !== 'php') { |
| 130 | + continue; |
| 131 | + } |
| 132 | + |
| 133 | + $fileContents = file_get_contents($fileInfo->getRealPath()); |
| 134 | + if ($fileContents === false) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + $contents .= $fileContents; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + $this->registrationContents = $contents; |
| 143 | + |
| 144 | + return $contents; |
| 145 | + } |
| 146 | +} |
0 commit comments