-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathConfigurationRuleFilter.php
More file actions
148 lines (124 loc) · 4.39 KB
/
Copy pathConfigurationRuleFilter.php
File metadata and controls
148 lines (124 loc) · 4.39 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace Rector\Configuration;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\Rector\RectorInterface;
use Rector\ValueObject\Configuration;
use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration;
/**
* Modify available rector rules based on the configuration options
* @see \Rector\Tests\Configuration\ConfigurationRuleFilterTest
*/
final class ConfigurationRuleFilter
{
private ?Configuration $configuration = null;
public function setConfiguration(Configuration $configuration): void
{
$this->configuration = $configuration;
}
/**
* @param list<RectorInterface> $rectors
* @return list<RectorInterface>
*/
public function filter(array $rectors): array
{
// deprecated rules only warn via DeprecatedRulesReporter; they must never run, as their
// refactor() throws to signal the deprecation - keep them out of the active set
$rectors = array_values(
array_filter($rectors, static fn (RectorInterface $rector): bool => ! $rector instanceof DeprecatedInterface)
);
if (! $this->configuration instanceof Configuration) {
return $rectors;
}
$onlyRules = $this->configuration->getOnlyRules();
if ($onlyRules !== []) {
return $this->filterOnlyRules($rectors, $onlyRules);
}
if ($this->configuration->isComposerBased()) {
return $this->filterComposerBased($rectors);
}
if ($this->configuration->isPhpOnly()) {
return $this->filterPhpOnly($rectors);
}
return $rectors;
}
/**
* @param list<RectorInterface> $rectors
* @param string[] $onlyRules
* @return list<RectorInterface>
*/
public function filterOnlyRules(array $rectors, array $onlyRules): array
{
$activeRectors = [];
foreach ($rectors as $rector) {
foreach ($onlyRules as $onlyRule) {
if ($rector instanceof $onlyRule) {
$activeRectors[] = $rector;
break;
}
}
}
return $activeRectors;
}
/**
* Keeps rules that declare a composer package constraint themselves, and rules whose configuration
* was registered with a composer package constraint.
*
* @param list<RectorInterface> $rectors
* @return list<RectorInterface>
*/
private function filterComposerBased(array $rectors): array
{
$composerBoundRectorClasses = $this->resolveComposerBoundRectorClasses();
$activeRectors = [];
foreach ($rectors as $rector) {
if ($rector instanceof ComposerPackageConstraintInterface) {
$activeRectors[] = $rector;
continue;
}
if (in_array($rector::class, $composerBoundRectorClasses, true)) {
$activeRectors[] = $rector;
}
}
return $activeRectors;
}
/**
* Keeps rules bound to a minimal PHP version, e.g. PHP upgrade rules
*
* @param list<RectorInterface> $rectors
* @return list<RectorInterface>
*/
private function filterPhpOnly(array $rectors): array
{
$activeRectors = [];
foreach ($rectors as $rector) {
if ($rector instanceof MinPhpVersionInterface) {
$activeRectors[] = $rector;
}
}
return $activeRectors;
}
/**
* @return string[]
*/
private function resolveComposerBoundRectorClasses(): array
{
$composerBoundRuleConfigurations = SimpleParameterProvider::provideArrayParameter(
Option::COMPOSER_BOUND_RULE_CONFIGURATIONS
);
$rectorClasses = [];
foreach ($composerBoundRuleConfigurations as $composerBoundRuleConfiguration) {
if (! $composerBoundRuleConfiguration instanceof ComposerBoundRuleConfiguration) {
continue;
}
if (! $composerBoundRuleConfiguration->isActive()) {
continue;
}
$rectorClasses[] = $composerBoundRuleConfiguration->getRectorClass();
}
return $rectorClasses;
}
}