|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Rector\Console\ExitCode; |
| 6 | +use Rector\Scripts\Finder\RectorClassFinder; |
| 7 | +use Symfony\Component\Console\Input\ArrayInput; |
| 8 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 9 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 10 | +use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface; |
| 11 | + |
| 12 | +require __DIR__ . '/../vendor/autoload.php'; |
| 13 | + |
| 14 | +$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput()); |
| 15 | + |
| 16 | +// core rules + all installed rector extensions (rector-doctrine, rector-symfony, rector-phpunit, ...) |
| 17 | +$ruleDirectories = array_merge( |
| 18 | + [__DIR__ . '/../rules'], |
| 19 | + glob(__DIR__ . '/../vendor/rector/rector-*', GLOB_ONLYDIR) ?: [] |
| 20 | +); |
| 21 | + |
| 22 | +$rectorClassFinder = new RectorClassFinder(); |
| 23 | +$rectorClasses = $rectorClassFinder->find($ruleDirectories); |
| 24 | + |
| 25 | +$errorMessages = []; |
| 26 | + |
| 27 | +foreach ($rectorClasses as $rectorClass) { |
| 28 | + $reflectionClass = new ReflectionClass($rectorClass); |
| 29 | + |
| 30 | + // rule definition does not depend on constructor dependencies, skip them |
| 31 | + $rector = $reflectionClass->newInstanceWithoutConstructor(); |
| 32 | + if (! $rector instanceof DocumentedRuleInterface) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + $ruleDefinition = $rector->getRuleDefinition(); |
| 37 | + |
| 38 | + if (trim($ruleDefinition->getDescription()) === '') { |
| 39 | + $errorMessages[] = sprintf('Rule "%s" is missing a clear description', $rectorClass); |
| 40 | + } |
| 41 | + |
| 42 | + $codeSamples = $ruleDefinition->getCodeSamples(); |
| 43 | + if ($codeSamples === []) { |
| 44 | + $errorMessages[] = sprintf('Rule "%s" is missing at least one code sample', $rectorClass); |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + foreach ($codeSamples as $codeSample) { |
| 49 | + if (trim($codeSample->getBadCode()) === '' || trim($codeSample->getGoodCode()) === '') { |
| 50 | + $errorMessages[] = sprintf('Rule "%s" has an empty code sample, fill before/after code', $rectorClass); |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +if ($errorMessages !== []) { |
| 57 | + $symfonyStyle->listing($errorMessages); |
| 58 | + $symfonyStyle->error(sprintf('Found %d rule definition error(s), see above', count($errorMessages))); |
| 59 | + |
| 60 | + exit(ExitCode::FAILURE); |
| 61 | +} |
| 62 | + |
| 63 | +$symfonyStyle->writeln('Scanned paths:'); |
| 64 | +$symfonyStyle->listing(array_map(static fn (string $directory): string => realpath($directory), $ruleDirectories)); |
| 65 | + |
| 66 | +$symfonyStyle->success(sprintf('All %d Rector rule definitions are valid!', count($rectorClasses))); |
| 67 | + |
| 68 | +exit(ExitCode::SUCCESS); |
0 commit comments