-
-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathRectorConfigValidator.php
More file actions
145 lines (118 loc) · 3.84 KB
/
Copy pathRectorConfigValidator.php
File metadata and controls
145 lines (118 loc) · 3.84 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
<?php
declare(strict_types=1);
namespace Rector\Validation;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
final class RectorConfigValidator
{
/**
* @param string[] $rectorClasses
*/
public static function ensureNoDuplicatedClasses(array $rectorClasses): void
{
$duplicatedRectorClasses = self::resolveDuplicatedValues($rectorClasses);
if ($duplicatedRectorClasses === []) {
return;
}
throw new ShouldNotHappenException('Following rules are registered twice: ' . implode(
', ',
$duplicatedRectorClasses
));
}
/**
* @param mixed[] $skip
*/
public static function ensureRectorRulesExist(array $skip): void
{
$nonExistingRules = [];
$skippedRectorRules = [];
$skippedNonRectorClasses = [];
foreach ($skip as $key => $value) {
if (is_string($key) && self::isNonRectorClass($key)) {
$skippedNonRectorClasses[] = $key;
continue;
}
if (self::isRectorClassValue($key)) {
if (class_exists($key)) {
$skippedRectorRules[] = $key;
} else {
$nonExistingRules[] = $key;
}
continue;
}
if (! self::isRectorClassValue($value)) {
continue;
}
if (class_exists($value)) {
$skippedRectorRules[] = $value;
continue;
}
$nonExistingRules[] = $value;
}
SimpleParameterProvider::addParameter(Option::SKIPPED_RECTOR_RULES, $skippedRectorRules);
SimpleParameterProvider::addParameter(Option::SKIPPED_NON_RECTOR_CLASSES, $skippedNonRectorClasses);
if ($nonExistingRules === []) {
return;
}
$nonExistingRulesString = '';
foreach ($nonExistingRules as $nonExistingRule) {
$nonExistingRulesString .= ' * ' . $nonExistingRule . PHP_EOL;
}
throw new ShouldNotHappenException(
'These rules from "$rectorConfig->skip()" do not exist - remove them or fix their names:' . PHP_EOL . $nonExistingRulesString
);
}
/**
* Only Rector rules are matched against skipped classes, so any other class can never be skipped
*/
private static function isNonRectorClass(string $key): bool
{
// interfaces are allowed, as they can mark a group of Rector rules
if (! class_exists($key)) {
return false;
}
if (is_a($key, RectorInterface::class, true)) {
return false;
}
return ! is_a($key, PostRectorInterface::class, true);
}
private static function isRectorClassValue(mixed $value): bool
{
// only validate string
if (! is_string($value)) {
return false;
}
// not regex path
if (str_contains($value, '*')) {
return false;
}
// not if no Rector suffix
if (! str_ends_with($value, 'Rector')) {
return false;
}
// not directory
if (is_dir($value)) {
return false;
}
// not file
return ! is_file($value);
}
/**
* @param string[] $values
* @return string[]
*/
private static function resolveDuplicatedValues(array $values): array
{
$counted = array_count_values($values);
$duplicates = [];
foreach ($counted as $value => $count) {
if ($count > 1) {
$duplicates[] = $value;
}
}
return array_unique($duplicates);
}
}