-
-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathRectorConfig.php
365 lines (311 loc) · 11.3 KB
/
RectorConfig.php
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
declare(strict_types=1);
namespace Rector\Config;
use Illuminate\Container\Container;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Contract\Rector\NonPhpRectorInterface;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ScopeAnalyzer;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Webmozart\Assert\Assert;
/**
* @api
*/
final class RectorConfig extends Container
{
/**
* @param string[] $paths
*/
public function paths(array $paths): void
{
Assert::allString($paths);
SimpleParameterProvider::setParameter(Option::PATHS, $paths);
}
/**
* @param string[] $sets
*/
public function sets(array $sets): void
{
Assert::allString($sets);
foreach ($sets as $set) {
Assert::fileExists($set);
$this->import($set);
}
}
public function disableParallel(): void
{
SimpleParameterProvider::setParameter(Option::PARALLEL, false);
}
public function parallel(int $seconds = 120, int $maxNumberOfProcess = 16, int $jobSize = 20): void
{
SimpleParameterProvider::setParameter(Option::PARALLEL, true);
SimpleParameterProvider::setParameter(Option::PARALLEL_JOB_TIMEOUT_IN_SECONDS, $seconds);
SimpleParameterProvider::setParameter(Option::PARALLEL_MAX_NUMBER_OF_PROCESSES, $maxNumberOfProcess);
SimpleParameterProvider::setParameter(Option::PARALLEL_JOB_SIZE, $jobSize);
}
public function noDiffs(): void
{
SimpleParameterProvider::setParameter(Option::NO_DIFFS, true);
}
public function memoryLimit(string $memoryLimit): void
{
SimpleParameterProvider::setParameter(Option::MEMORY_LIMIT, $memoryLimit);
}
/**
* @param array<int|string, mixed> $criteria
*/
public function skip(array $criteria): void
{
$notExistsRules = [];
foreach ($criteria as $key => $value) {
/**
* Cover define rule then list of files
*
* $rectorConfig->skip([
* RenameVariableToMatchMethodCallReturnTypeRector::class => [
* __DIR__ . '/packages/Config/RectorConfig.php'
* ],
* ]);
*/
if ($this->isRuleNoLongerExists($key)) {
$notExistsRules[] = $key;
}
if (! is_string($value)) {
continue;
}
/**
* Cover direct value without array list of files, eg:
*
* $rectorConfig->skip([
* StringClassNameToClassConstantRector::class,
* ]);
*/
if ($this->isRuleNoLongerExists($value)) {
$notExistsRules[] = $value;
}
}
if ($notExistsRules !== []) {
throw new ShouldNotHappenException(
'Following skipped rules on $rectorConfig->skip() are no longer exists or changed to different namespace: ' . implode(
', ',
$notExistsRules
)
);
}
SimpleParameterProvider::addParameter(Option::SKIP, $criteria);
}
public function removeUnusedImports(bool $removeUnusedImports = true): void
{
SimpleParameterProvider::setParameter(Option::REMOVE_UNUSED_IMPORTS, $removeUnusedImports);
}
public function importNames(bool $importNames = true, bool $importDocBlockNames = true): void
{
SimpleParameterProvider::setParameter(Option::AUTO_IMPORT_NAMES, $importNames);
SimpleParameterProvider::setParameter(Option::AUTO_IMPORT_DOC_BLOCK_NAMES, $importDocBlockNames);
}
public function importShortClasses(bool $importShortClasses = true): void
{
SimpleParameterProvider::setParameter(Option::IMPORT_SHORT_CLASSES, $importShortClasses);
}
/**
* Set PHPStan custom config to load extensions and custom configuration to Rector.
* By default, the "phpstan.neon" path is used.
*/
public function phpstanConfig(string $filePath): void
{
Assert::fileExists($filePath);
SimpleParameterProvider::setParameter(Option::PHPSTAN_FOR_RECTOR_PATH, $filePath);
}
/**
* @param class-string<ConfigurableRectorInterface&RectorInterface> $rectorClass
* @param mixed[] $configuration
*/
public function ruleWithConfiguration(string $rectorClass, array $configuration): void
{
Assert::classExists($rectorClass);
Assert::isAOf($rectorClass, RectorInterface::class);
Assert::isAOf($rectorClass, ConfigurableRectorInterface::class);
$this->singleton($rectorClass);
$this->afterResolving($rectorClass, static function (ConfigurableRectorInterface $configurableRector) use (
$configuration
): void {
$configurableRector->configure($configuration);
});
$this->tagRectorService($rectorClass);
}
/**
* @param class-string<RectorInterface> $rectorClass
*/
public function rule(string $rectorClass): void
{
Assert::classExists($rectorClass);
Assert::isAOf($rectorClass, RectorInterface::class);
$this->singleton($rectorClass);
$this->tagRectorService($rectorClass);
if (is_a($rectorClass, AbstractScopeAwareRector::class, true)) {
$this->extend(
$rectorClass,
static function (
AbstractScopeAwareRector $scopeAwareRector,
Container $container
): AbstractScopeAwareRector {
$scopeAnalyzer = $container->make(ScopeAnalyzer::class);
$scopeAwareRector->autowireAbstractScopeAwareRector($scopeAnalyzer);
return $scopeAwareRector;
}
);
}
}
public function import(string $filePath): void
{
Assert::fileExists($filePath);
$self = $this;
$callable = (require $filePath);
Assert::isCallable($callable);
/** @var callable(Container $container): void $callable */
$callable($self);
}
/**
* @param array<class-string<RectorInterface>> $rectorClasses
*/
public function rules(array $rectorClasses): void
{
Assert::allString($rectorClasses);
$this->ensureNotDuplicatedClasses($rectorClasses);
foreach ($rectorClasses as $rectorClass) {
$this->rule($rectorClass);
}
}
/**
* @param PhpVersion::* $phpVersion
*/
public function phpVersion(int $phpVersion): void
{
SimpleParameterProvider::setParameter(Option::PHP_VERSION_FEATURES, $phpVersion);
}
/**
* @param string[] $autoloadPaths
*/
public function autoloadPaths(array $autoloadPaths): void
{
Assert::allString($autoloadPaths);
SimpleParameterProvider::setParameter(Option::AUTOLOAD_PATHS, $autoloadPaths);
}
/**
* @param string[] $bootstrapFiles
*/
public function bootstrapFiles(array $bootstrapFiles): void
{
Assert::allString($bootstrapFiles);
SimpleParameterProvider::setParameter(Option::BOOTSTRAP_FILES, $bootstrapFiles);
}
public function symfonyContainerXml(string $filePath): void
{
SimpleParameterProvider::setParameter(Option::SYMFONY_CONTAINER_XML_PATH_PARAMETER, $filePath);
}
public function symfonyContainerPhp(string $filePath): void
{
SimpleParameterProvider::setParameter(Option::SYMFONY_CONTAINER_PHP_PATH_PARAMETER, $filePath);
}
/**
* @param string[] $extensions
*/
public function fileExtensions(array $extensions): void
{
Assert::allString($extensions);
SimpleParameterProvider::setParameter(Option::FILE_EXTENSIONS, $extensions);
}
public function cacheDirectory(string $directoryPath): void
{
// cache directory path is created via mkdir in CacheFactory
// when not exists, so no need to validate $directoryPath is a directory
SimpleParameterProvider::setParameter(Option::CACHE_DIR, $directoryPath);
}
public function containerCacheDirectory(string $directoryPath): void
{
// container cache directory path must be a directory on the first place
Assert::directory($directoryPath);
SimpleParameterProvider::setParameter(Option::CONTAINER_CACHE_DIRECTORY, $directoryPath);
}
/**
* @param class-string<CacheStorageInterface> $cacheClass
*/
public function cacheClass(string $cacheClass): void
{
Assert::isAOf($cacheClass, CacheStorageInterface::class);
SimpleParameterProvider::setParameter(Option::CACHE_CLASS, $cacheClass);
}
/**
* @see /~https://github.com/nikic/PHP-Parser/issues/723#issuecomment-712401963
*/
public function indent(string $character, int $count): void
{
SimpleParameterProvider::setParameter(Option::INDENT_CHAR, $character);
SimpleParameterProvider::setParameter(Option::INDENT_SIZE, $count);
}
private function isRuleNoLongerExists(mixed $skipRule): bool
{
return // only validate string
is_string($skipRule)
// not regex path
&& ! str_contains($skipRule, '*')
// not realpath
&& realpath($skipRule) === false
// a Rector end
&& str_ends_with($skipRule, 'Rector')
// class not exists
&& ! class_exists($skipRule);
}
/**
* @param string[] $values
* @return string[]
*/
private 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);
}
/**
* @param class-string<RectorInterface|PhpRectorInterface> $rectorClass
*/
private function tagRectorService(string $rectorClass): void
{
$this->tag($rectorClass, RectorInterface::class);
if (is_a($rectorClass, PhpRectorInterface::class, true)) {
$this->tag($rectorClass, PhpRectorInterface::class);
} elseif (is_a($rectorClass, NonPhpRectorInterface::class, true)) {
trigger_error(sprintf(
'The "%s" interface of "%s" rule is deprecated. Rector will only PHP code, as designed to with AST. For another file format, use custom tooling.',
NonPhpRectorInterface::class,
$rectorClass,
));
exit();
}
}
/**
* @param string[] $rectorClasses
*/
private function ensureNotDuplicatedClasses(array $rectorClasses): void
{
$duplicatedRectorClasses = $this->resolveDuplicatedValues($rectorClasses);
if ($duplicatedRectorClasses === []) {
return;
}
throw new ShouldNotHappenException('Following rules are registered twice: ' . implode(
', ',
$duplicatedRectorClasses
));
}
}