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