Repository navigation
Expand file tree
/
Copy pathRectorRegistry.php
More file actions
120 lines (100 loc) · 3.46 KB
/
Copy pathRectorRegistry.php
File metadata and controls
120 lines (100 loc) · 3.46 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
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace Rector\Application;
use PhpParser\Node;
use Rector\Configuration\ConfigurationRuleFilter;
use Rector\Contract\Rector\RectorInterface;
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Application\File;
use Rector\VersionBonding\ComposerPackageConstraintFilter;
use Rector\VersionBonding\PhpVersionedFilter;
/**
* @see \Rector\Tests\Application\RectorRegistryTest
*/
final class RectorRegistry
{
private bool $areRectorsPrepared = false;
/**
* @var RectorInterface[]
*/
private array $activeRectors = [];
/**
* @var array<class-string<Node>, RectorInterface[]>
*/
private array $cache = [];
/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private readonly PhpVersionedFilter $phpVersionedFilter,
private readonly ComposerPackageConstraintFilter $composerPackageConstraintFilter,
private readonly ConfigurationRuleFilter $configurationRuleFilter,
private readonly Skipper $skipper,
private array $rectors,
) {
}
/**
* @param RectorInterface[] $rectors
*
* @internal Used only in Rector core, not supported outside. Might change any time.
*/
public function refreshRectors(array $rectors): void
{
$this->rectors = $rectors;
$this->areRectorsPrepared = false;
$this->cache = [];
}
/**
* @return RectorInterface[]
*/
public function forNode(Node $node): array
{
$this->prepareRectors();
$nodeClass = $node::class;
if (! isset($this->cache[$nodeClass])) {
$this->cache[$nodeClass] = [];
foreach ($this->activeRectors as $rector) {
foreach ($rector->getNodeTypes() as $nodeType) {
if (is_a($nodeClass, $nodeType, true)) {
$this->cache[$nodeClass][] = $rector;
continue 2;
}
}
}
}
return $this->cache[$nodeClass];
}
/**
* Filters out rectors skipped for this file before they ever run, so the skip no longer needs a
* second refactor() probe on a clone to learn whether it mattered.
*
* Known limit: a configured skip is marked used as soon as its path matches the file, even if the
* rule would not have changed it. This drops precise unused-skip reporting in exchange for not
* running the rule twice per skipped node.
*
* @return RectorInterface[]
*/
public function forNodeAndFile(Node $node, File $file): array
{
$rectors = $this->forNode($node);
$filePath = $file->getFilePath();
return array_filter(
$rectors,
fn (RectorInterface $rector): bool => ! $this->skipper->shouldSkipRectorAndFile($rector, $filePath)
);
}
/**
* Filtering must happen lazily, after the configuration is set in ProcessCommand::execute(),
* otherwise options like --only are not yet available and get ignored.
*/
private function prepareRectors(): void
{
if ($this->areRectorsPrepared) {
return;
}
$activeRectors = $this->phpVersionedFilter->filter(array_values($this->rectors));
$activeRectors = $this->composerPackageConstraintFilter->filter($activeRectors);
$this->activeRectors = $this->configurationRuleFilter->filter($activeRectors);
$this->areRectorsPrepared = true;
}
}