|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Nette\Loaders\RobotLoader; |
| 6 | +use Rector\Bridge\SetRectorsResolver; |
| 7 | +use Symfony\Component\Console\Input\ArrayInput; |
| 8 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 9 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 10 | +use Symfony\Component\Finder\Finder; |
| 11 | +use Symfony\Component\Finder\SplFileInfo; |
| 12 | +use Webmozart\Assert\Assert; |
| 13 | + |
| 14 | +require __DIR__ . '/../vendor/autoload.php'; |
| 15 | + |
| 16 | +// 1. find all rector rules in here and in all vendor/rector dirs |
| 17 | +$rectorClassFinder = new RectorClassFinder(); |
| 18 | + |
| 19 | +$rectorClasses = $rectorClassFinder->find([ |
| 20 | + __DIR__ . '/../rules', |
| 21 | + __DIR__ . '/../vendor/rector/rector-doctrine', |
| 22 | + __DIR__ . '/../vendor/rector/rector-phpunit', |
| 23 | + __DIR__ . '/../vendor/rector/rector-symfony', |
| 24 | + __DIR__ . '/../vendor/rector/rector-downgrade-php', |
| 25 | +]); |
| 26 | + |
| 27 | +$symfonyStyle = new SymfonyStyle(new ArrayInput([]), new ConsoleOutput()); |
| 28 | +$symfonyStyle->writeln(sprintf('<fg=green>Found Rector %d rules</>', count($rectorClasses))); |
| 29 | + |
| 30 | +$rectorSeFinder = new RectorSetFinder(); |
| 31 | + |
| 32 | +$rectorSetFiles = $rectorSeFinder->find([ |
| 33 | + __DIR__ . '/../config/set', |
| 34 | + __DIR__ . '/../vendor/rector/rector-symfony/config/sets', |
| 35 | + __DIR__ . '/../vendor/rector/rector-doctrine/config/sets', |
| 36 | + __DIR__ . '/../vendor/rector/rector-phpunit/config/sets', |
| 37 | + __DIR__ . '/../vendor/rector/rector-downgrade-php/config/set', |
| 38 | +]); |
| 39 | + |
| 40 | +$symfonyStyle->writeln(sprintf('<fg=green>Found %d sets</>', count($rectorSetFiles))); |
| 41 | + |
| 42 | +$usedRectorClassResolver = new UsedRectorClassResolver(); |
| 43 | +$usedRectorRules = $usedRectorClassResolver->resolve($rectorSetFiles); |
| 44 | + |
| 45 | +$symfonyStyle->newLine(); |
| 46 | +$symfonyStyle->writeln(sprintf('<fg=yellow>Found %d used Rector rules in sets</>', count($usedRectorRules))); |
| 47 | + |
| 48 | +$unusedRectorRules = array_diff($rectorClasses, $usedRectorRules); |
| 49 | +$symfonyStyle->writeln( |
| 50 | + sprintf('<fg=yellow;options=bold>Found %d Rector rules not in any set</>', count($unusedRectorRules)) |
| 51 | +); |
| 52 | + |
| 53 | +$symfonyStyle->newLine(); |
| 54 | +$symfonyStyle->listing($unusedRectorRules); |
| 55 | + |
| 56 | +final class RectorClassFinder |
| 57 | +{ |
| 58 | + /** |
| 59 | + * @param string[] $dirs |
| 60 | + * @return string[] |
| 61 | + */ |
| 62 | + public function find(array $dirs): array |
| 63 | + { |
| 64 | + $robotLoader = new RobotLoader(); |
| 65 | + $robotLoader->acceptFiles = ['*Rector.php']; |
| 66 | + $robotLoader->addDirectory(...$dirs); |
| 67 | + |
| 68 | + $robotLoader->setTempDirectory(sys_get_temp_dir() . '/rector-rules'); |
| 69 | + $robotLoader->refresh(); |
| 70 | + |
| 71 | + return array_keys($robotLoader->getIndexedClasses()); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +final class RectorSetFinder |
| 76 | +{ |
| 77 | + /** |
| 78 | + * @param string[] $configDirs |
| 79 | + * @return string[] |
| 80 | + */ |
| 81 | + public function find(array $configDirs): array |
| 82 | + { |
| 83 | + Assert::allString($configDirs); |
| 84 | + Assert::allDirectory($configDirs); |
| 85 | + |
| 86 | + // find set files |
| 87 | + $finder = (new Finder())->in($configDirs) |
| 88 | + ->files() |
| 89 | + ->name('*.php'); |
| 90 | + |
| 91 | + /** @var SplFileInfo[] $setFileInfos */ |
| 92 | + $setFileInfos = iterator_to_array($finder->getIterator()); |
| 93 | + |
| 94 | + $setFiles = []; |
| 95 | + foreach ($setFileInfos as $setFileInfo) { |
| 96 | + $setFiles[] = $setFileInfo->getRealPath(); |
| 97 | + } |
| 98 | + |
| 99 | + return $setFiles; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +final class UsedRectorClassResolver |
| 104 | +{ |
| 105 | + /** |
| 106 | + * @param string[] $rectorSetFiles |
| 107 | + * @return string[] |
| 108 | + */ |
| 109 | + public function resolve(array $rectorSetFiles): array |
| 110 | + { |
| 111 | + $setRectorsResolver = new SetRectorsResolver(); |
| 112 | + $rulesConfiguration = $setRectorsResolver->resolveFromFilePathsIncludingConfiguration($rectorSetFiles); |
| 113 | + |
| 114 | + $usedRectorRules = []; |
| 115 | + foreach ($rulesConfiguration as $ruleConfiguration) { |
| 116 | + $usedRectorRules[] = is_string($ruleConfiguration) ? $ruleConfiguration : array_keys($ruleConfiguration)[0]; |
| 117 | + } |
| 118 | + |
| 119 | + sort($usedRectorRules); |
| 120 | + |
| 121 | + return array_unique($usedRectorRules); |
| 122 | + } |
| 123 | +} |
0 commit comments