Repository navigation
Expand file tree
/
Copy pathRectorConfig.php
More file actions
567 lines (475 loc) · 18 KB
/
Copy pathRectorConfig.php
File metadata and controls
567 lines (475 loc) · 18 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
<?php
declare(strict_types=1);
namespace Rector\Config;
use Composer\Semver\Semver;
use Deprecated;
use Illuminate\Container\Container;
use Override;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Composer\InstalledPackageResolver;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Configuration\RectorConfigBuilder;
use Rector\Contract\DependencyInjection\RelatedConfigInterface;
use Rector\Contract\DependencyInjection\ResettableInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\DependencyInjection\Laravel\ContainerMemento;
use Rector\Enum\Config\Defaults;
use Rector\Exception\ShouldNotHappenException;
use Rector\Skipper\SkipCriteriaResolver\SkippedClassResolver;
use Rector\Validation\RectorConfigValidator;
use Rector\ValueObject\Configuration\LevelOverflow;
use Rector\ValueObject\PhpVersion;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\ValueObject\ComposerBoundRuleConfiguration;
use Symfony\Component\Console\Command\Command;
use Webmozart\Assert\Assert;
/**
* @api
* @see \Rector\Tests\Config\RectorConfigTest
*/
final class RectorConfig extends Container
{
/**
* @var array<class-string<ConfigurableRectorInterface>, mixed[]>
*/
private array $ruleConfigurations = [];
/**
* @var array<class-string<RectorInterface>, true>
*/
private array $registeredRectorClasses = [];
/**
* @var array<string, true>
*/
private array $registeredComposerBoundRuleConfigurations = [];
/**
* @var string[]
*/
private array $autotagInterfaces = [Command::class, ResettableInterface::class];
private ?InstalledPackageResolver $installedPackageResolver = null;
private static ?bool $recreated = null;
public static function configure(): RectorConfigBuilder
{
if (self::$recreated === null) {
self::$recreated = false;
} elseif (self::$recreated === false) {
self::$recreated = true;
}
SimpleParameterProvider::setParameter(Option::IS_RECTORCONFIG_BUILDER_RECREATED, self::$recreated);
return new RectorConfigBuilder();
}
/**
* @param string[] $paths
*/
public function paths(array $paths): void
{
Assert::allString($paths);
// ensure paths exist
foreach ($paths as $path) {
if (str_contains($path, '*')) {
continue;
}
Assert::fileExists($path);
}
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);
}
// for cache invalidation in case of sets change
SimpleParameterProvider::addParameter(Option::REGISTERED_RECTOR_SETS, $sets);
}
public function disableParallel(): void
{
SimpleParameterProvider::setParameter(Option::PARALLEL, false);
}
public function parallel(
int $processTimeout = 120,
int $maxNumberOfProcess = Defaults::PARALLEL_MAX_NUMBER_OF_PROCESS,
int $jobSize = 16
): void {
SimpleParameterProvider::setParameter(Option::PARALLEL, true);
SimpleParameterProvider::setParameter(Option::PARALLEL_JOB_TIMEOUT_IN_SECONDS, $processTimeout);
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);
}
/**
* @see https://getrector.com/documentation/ignoring-rules-or-paths
* @param array<int|string, mixed> $skip
*/
public function skip(array $skip): void
{
RectorConfigValidator::ensureRectorRulesExist($skip);
SimpleParameterProvider::addParameter(Option::SKIP, $skip);
}
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);
}
/**
* Add PHPStan custom config to load extensions and custom configuration to Rector.
*/
public function phpstanConfig(string $filePath): void
{
Assert::fileExists($filePath);
SimpleParameterProvider::addParameter(Option::PHPSTAN_FOR_RECTOR_PATHS, [$filePath]);
}
/**
* Add PHPStan custom configs to load extensions and custom configuration to Rector.
*
* @param string[] $filePaths
*/
public function phpstanConfigs(array $filePaths): void
{
Assert::allString($filePaths);
Assert::allFileExists($filePaths);
SimpleParameterProvider::addParameter(Option::PHPSTAN_FOR_RECTOR_PATHS, $filePaths);
}
/**
* @param class-string<ConfigurableRectorInterface> $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);
// store configuration to cache
$this->ruleConfigurations[$rectorClass] = array_merge(
$this->ruleConfigurations[$rectorClass] ?? [],
$configuration
);
$this->rule($rectorClass);
$this->afterResolving($rectorClass, function (ConfigurableRectorInterface $configurableRector) use (
$rectorClass
): void {
$ruleConfiguration = $this->ruleConfigurations[$rectorClass];
$configurableRector->configure($ruleConfiguration);
});
}
/**
* Register the rule configuration only if the package version installed in the analysed project satisfies
* the version constraint. Useful for configuration valid since a specific package version,
* e.g. an attribute added in PHPUnit 11.
*
* @param class-string<ConfigurableRectorInterface> $rectorClass
* @param mixed[] $configuration
*/
public function ruleWithConfigurationComposerVersionBound(
string $rectorClass,
array $configuration,
string $packageName,
string $versionConstraint
): void {
$packageVersion = $this->resolveInstalledPackageVersion($packageName);
$isActive = $packageVersion !== null && Semver::satisfies($packageVersion, $versionConstraint);
// the same rule configuration can be registered by multiple sets, report it only once
$configurationKey = $rectorClass . '|' . $packageName . '|' . $versionConstraint . '|' . serialize(
$configuration
);
if (! isset($this->registeredComposerBoundRuleConfigurations[$configurationKey])) {
$this->registeredComposerBoundRuleConfigurations[$configurationKey] = true;
// reported by the "composer-based" command, the inactive ones as well
SimpleParameterProvider::addParameter(Option::COMPOSER_BOUND_RULE_CONFIGURATIONS, [
new ComposerBoundRuleConfiguration(
$rectorClass,
$packageName,
$versionConstraint,
$configuration,
$isActive
),
]);
}
if (! $isActive) {
return;
}
$this->ruleWithConfiguration($rectorClass, $configuration);
}
/**
* @param class-string<RectorInterface> $rectorClass
*/
public function rule(string $rectorClass): void
{
Assert::classExists($rectorClass);
Assert::isAOf($rectorClass, RectorInterface::class);
$this->singleton($rectorClass);
// the same rule can be registered by multiple sets, tag it only once,
// otherwise it is run twice on every node and listed twice in the reports
if (! isset($this->registeredRectorClasses[$rectorClass])) {
$this->registeredRectorClasses[$rectorClass] = true;
$this->tag($rectorClass, RectorInterface::class);
// for cache invalidation in case of change
SimpleParameterProvider::addParameter(Option::REGISTERED_RECTOR_RULES, $rectorClass);
}
if (is_a($rectorClass, RelatedConfigInterface::class, true)) {
$configFile = $rectorClass::getConfigFile();
Assert::file($configFile, sprintf(
'The config path "%s" in "%s::getConfigFile()" could not be found',
$configFile,
$rectorClass
));
$this->import($configFile);
}
}
/**
* @param class-string<Command> $commandClass
*/
public function command(string $commandClass): void
{
$this->singleton($commandClass);
$this->tag($commandClass, Command::class);
}
public function import(string $filePath): void
{
/**
* Only stop when filePath realpath is false and contains glob patterns
* @see https://github.com/rectorphp/rector/issues/9156#issuecomment-2869130541
*/
if (realpath($filePath) === false && str_contains($filePath, '*')) {
throw new ShouldNotHappenException(
'Matching file paths by using glob-patterns is no longer supported. Use specific file path instead.'
);
}
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);
RectorConfigValidator::ensureNoDuplicatedClasses($rectorClasses);
foreach ($rectorClasses as $rectorClass) {
$this->rule($rectorClass);
}
}
/**
* @param PhpVersion::* $phpVersion
*/
public function phpVersion(int $phpVersion): void
{
SimpleParameterProvider::setParameter(Option::PHP_VERSION_FEATURES, $phpVersion);
}
/**
* @internal
*
* @api only for testing. It is parsed from composer.json "require" packages by default
* @param array<PolyfillPackage::*> $polyfillPackages
*/
public function polyfillPackages(array $polyfillPackages): void
{
SimpleParameterProvider::setParameter(Option::POLYFILL_PACKAGES, $polyfillPackages);
}
/**
* @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);
}
public function newLineOnFluentCall(bool $enabled = true): void
{
SimpleParameterProvider::setParameter(Option::NEW_LINE_ON_FLUENT_CALL, $enabled);
}
public function treatClassesAsFinal(bool $treatClassesAsFinal = true): void
{
SimpleParameterProvider::setParameter(Option::TREAT_CLASSES_AS_FINAL, $treatClassesAsFinal);
}
/**
* Guard the listed classes and their descendants against method signature changes that would
* break child classes - e.g. adding a return type or a param type. Only non-final classes are
* guarded, as final classes cannot be extended.
*
* @param string[] $classes
*/
public function typeGuardedClasses(array $classes): void
{
Assert::allString($classes);
SimpleParameterProvider::setParameter(Option::TYPE_GUARDED_CLASSES, $classes);
}
/**
* @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
*/
#[Deprecated(message: <<<'TXT'
Cache storage is selected automatically: file cache locally, in-memory cache in CI,
where the ephemeral workspace makes writing a cache that is never re-read wasted IO.
The passed value is ignored.
TXT)]
public function cacheClass(string $cacheClass): void
{
Assert::isAOf($cacheClass, CacheStorageInterface::class);
}
/**
* @param class-string $cacheMetaExtensionClass
*/
#[Deprecated(message: <<<'TXT'
Niche mechanism, no longer applied. Let Rector handle cache on its own. If custom
invalidation is needed, handle it in CI in a more generic way, e.g. by clearing the cache directory.
TXT)]
public function cacheMetaExtension(string $cacheMetaExtensionClass): void
{
SimpleParameterProvider::addParameter(Option::CACHE_META_EXTENSIONS, $cacheMetaExtensionClass);
}
/**
* @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);
}
/**
* @internal
* @api used only in tests
*/
public function resetRuleConfigurations(): void
{
$this->ruleConfigurations = [];
$this->registeredRectorClasses = [];
$this->registeredComposerBoundRuleConfigurations = [];
}
/**
* Compiler passes-like method
*/
public function boot(): void
{
$skippedClassResolver = new SkippedClassResolver();
$skippedElements = $skippedClassResolver->resolve();
foreach ($skippedElements as $skippedClass => $path) {
if ($path !== null) {
continue;
}
// completely forget the Rector rule only when no path specified
ContainerMemento::forgetService($this, $skippedClass);
}
}
/**
* @internal Use to add tag on service registrations
*/
public function autotagInterface(string $interface): void
{
$this->autotagInterfaces[] = $interface;
}
/**
* @param string $abstract
*/
#[Override]
public function singleton($abstract, mixed $concrete = null): void
{
parent::singleton($abstract, $concrete);
foreach ($this->autotagInterfaces as $autotagInterface) {
if (! is_a($abstract, $autotagInterface, true)) {
continue;
}
$this->tag($abstract, $autotagInterface);
}
}
public function reportingRealPath(bool $absolute = true): void
{
SimpleParameterProvider::setParameter(Option::ABSOLUTE_FILE_PATH, $absolute);
}
public function reportUnusedSkips(bool $report = true): void
{
SimpleParameterProvider::setParameter(Option::REPORT_UNUSED_SKIPS, $report);
}
public function editorUrl(string $editorUrl): void
{
SimpleParameterProvider::setParameter(Option::EDITOR_URL, $editorUrl);
}
/**
* @internal Used only for bridge
* @return array<class-string<ConfigurableRectorInterface>, mixed>
*/
public function getRuleConfigurations(): array
{
return $this->ruleConfigurations;
}
/**
* @internal Used only for bridge
* @return array<class-string<RectorInterface>>
*/
public function getMainRectorClasses(): array
{
return $this->tags[RectorInterface::class] ?? [];
}
/**
* @internal used to report level overflows in configuration
* @param LevelOverflow[] $levelOverflows
*/
public function setOverflowLevels(array $levelOverflows): void
{
SimpleParameterProvider::addParameter(Option::LEVEL_OVERFLOWS, $levelOverflows);
}
private function resolveInstalledPackageVersion(string $packageName): ?string
{
$this->installedPackageResolver ??= new InstalledPackageResolver();
return $this->installedPackageResolver->resolvePackageVersion($packageName);
}
}