Repository navigation
Expand file tree
/
Copy pathAllRectorRulesProvider.php
More file actions
111 lines (91 loc) · 3.26 KB
/
Copy pathAllRectorRulesProvider.php
File metadata and controls
111 lines (91 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Rector\Console\Rule;
use Composer\Autoload\ClassLoader;
use Rector\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use ReflectionClass;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
/**
* Discovers all Rector rule classes available on disk - in core and rector-* packages -
* regardless of the current config, by scanning the registered PSR-4 prefixes.
*
* @see \Rector\Tests\Console\Rule\AllRectorRulesProviderTest
*/
final class AllRectorRulesProvider
{
/**
* @return array<class-string<RectorInterface>>
*/
public function provide(): array
{
$rectorClasses = [];
foreach ($this->resolvePsr4Prefixes() as $namespacePrefix => $directories) {
if (! str_starts_with($namespacePrefix, 'Rector\\')) {
continue;
}
foreach ($directories as $directory) {
if (! is_dir($directory)) {
continue;
}
foreach ($this->findRectorFiles($directory) as $fileInfo) {
$className = $this->resolveClassName($namespacePrefix, $directory, $fileInfo);
if ($this->isConcreteRectorRule($className)) {
$rectorClasses[] = $className;
}
}
}
}
$rectorClasses = array_unique($rectorClasses);
sort($rectorClasses);
return $rectorClasses;
}
/**
* @return array<string, string[]>
*/
private function resolvePsr4Prefixes(): array
{
$psr4Prefixes = [];
// there can be multiple registered class loaders; merge their prefixes
foreach (spl_autoload_functions() as $autoloadFunction) {
if (! is_array($autoloadFunction) || ! $autoloadFunction[0] instanceof ClassLoader) {
continue;
}
foreach ($autoloadFunction[0]->getPrefixesPsr4() as $namespacePrefix => $directories) {
$psr4Prefixes[$namespacePrefix] = array_merge($psr4Prefixes[$namespacePrefix] ?? [], $directories);
}
}
return $psr4Prefixes;
}
/**
* @return iterable<SplFileInfo>
*/
private function findRectorFiles(string $directory): iterable
{
return Finder::create()
->files()
->name('*Rector.php')
->in($directory);
}
private function resolveClassName(string $namespacePrefix, string $directory, SplFileInfo $fileInfo): string
{
$relativePath = substr($fileInfo->getPathname(), strlen($directory) + 1, -strlen('.php'));
return $namespacePrefix . str_replace('/', '\\', $relativePath);
}
/**
* @phpstan-assert-if-true class-string<RectorInterface> $className
*/
private function isConcreteRectorRule(string $className): bool
{
// is_a() autoloads the class and returns false when it does not exist
if (! is_a($className, RectorInterface::class, true)) {
return false;
}
if (is_a($className, PostRectorInterface::class, true)) {
return false;
}
$reflectionClass = new ReflectionClass($className);
return ! $reflectionClass->isAbstract();
}
}