-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathRectorConfigBuilder.php
More file actions
1210 lines (969 loc) · 35.3 KB
/
RectorConfigBuilder.php
File metadata and controls
1210 lines (969 loc) · 35.3 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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Rector\Configuration;
use Rector\Bridge\SetProviderCollector;
use Rector\Bridge\SetRectorsResolver;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Composer\InstalledPackageResolver;
use Rector\Config\Level\CodeQualityLevel;
use Rector\Config\Level\CodingStyleLevel;
use Rector\Config\Level\DeadCodeLevel;
use Rector\Config\Level\TypeDeclarationLevel;
use Rector\Config\RectorConfig;
use Rector\Config\RegisteredService;
use Rector\Configuration\Levels\LevelRulesResolver;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Console\Notifier;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Enum\Config\Defaults;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Php\PhpVersionResolver\ComposerJsonPhpVersionResolver;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\Enum\SetGroup;
use Rector\Set\SetManager;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\FOSRestSetList;
use Rector\Symfony\Set\JMSSetList;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Symfony\Set\SymfonySetList;
use Rector\ValueObject\Configuration\LevelOverflow;
use Rector\ValueObject\PhpVersion;
use Symfony\Component\Finder\Finder;
use Webmozart\Assert\Assert;
/**
* @api
*/
final class RectorConfigBuilder
{
/**
* @var int
*/
private const MAX_LEVEL_GAP = 10;
/**
* @var string[]
*/
private array $paths = [];
/**
* @var string[]
*/
private array $sets = [];
/**
* @var array<mixed>
*/
private array $skip = [];
/**
* @var array<class-string<RectorInterface>>
*/
private array $rules = [];
/**
* @var array<class-string<ConfigurableRectorInterface>, mixed[]>
*/
private array $rulesWithConfigurations = [];
/**
* @var string[]
*/
private array $fileExtensions = [];
/**
* @var null|class-string<CacheStorageInterface>
*/
private ?string $cacheClass = null;
private ?string $cacheDirectory = null;
private ?string $containerCacheDirectory = null;
private ?bool $parallel = null;
private int $parallelTimeoutSeconds = 120;
private int $parallelMaxNumberOfProcess = Defaults::PARALLEL_MAX_NUMBER_OF_PROCESS;
private int $parallelJobSize = 16;
private bool $importNames = false;
private bool $importDocBlockNames = false;
private bool $importShortClasses = true;
private bool $removeUnusedImports = false;
private bool $noDiffs = false;
private ?string $memoryLimit = null;
/**
* @var string[]
*/
private array $autoloadPaths = [];
/**
* @var string[]
*/
private array $bootstrapFiles = [];
private string $indentChar = ' ';
private int $indentSize = 4;
/**
* @var string[]
*/
private array $phpstanConfigs = [];
/**
* @var null|PhpVersion::*
*/
private ?int $phpVersion = null;
private ?string $symfonyContainerXmlFile = null;
private ?string $symfonyContainerPhpFile = null;
/**
* To make sure type declarations set and level are not duplicated,
* as both contain same rules
*/
private ?bool $isTypeCoverageLevelUsed = null;
private ?bool $isDeadCodeLevelUsed = null;
private ?bool $isCodeQualityLevelUsed = null;
private ?bool $isCodingStyleLevelUsed = null;
private ?bool $isFluentNewLine = null;
/**
* @var RegisteredService[]
*/
private array $registerServices = [];
/**
* @var array<SetGroup::*>
*/
private array $setGroups = [];
private ?bool $reportingRealPath = null;
/**
* @var string[]
*/
private array $groupLoadedSets = [];
private ?string $editorUrl = null;
private ?bool $isWithPhpSetsUsed = null;
private ?bool $isWithPhpLevelUsed = null;
/**
* @var array<class-string<SetProviderInterface>,bool>
*/
private array $setProviders = [];
/**
* @var LevelOverflow[]
*/
private array $levelOverflows = [];
public function __invoke(RectorConfig $rectorConfig): void
{
if ($this->setGroups !== [] || $this->setProviders !== []) {
$setProviderCollector = new SetProviderCollector(array_map(
static fn (string $setProvider): SetProviderInterface =>
$rectorConfig->make($setProvider),
\array_keys($this->setProviders)
));
$setManager = new SetManager($setProviderCollector, new InstalledPackageResolver(getcwd()));
$this->groupLoadedSets = $setManager->matchBySetGroups($this->setGroups);
SimpleParameterProvider::addParameter(Option::COMPOSER_BASED_SETS, $this->groupLoadedSets);
}
// not to miss it by accident
if ($this->isWithPhpSetsUsed === true) {
$this->sets[] = SetList::PHP_POLYFILLS;
}
// merge sets together
$this->sets = array_merge($this->sets, $this->groupLoadedSets);
$uniqueSets = array_unique($this->sets);
if ($this->isWithPhpLevelUsed && $this->isWithPhpSetsUsed) {
throw new InvalidConfigurationException(sprintf(
'Your config uses "withPhp*()" and "withPhpLevel()" methods at the same time.%sPick one of them to avoid rule conflicts.',
PHP_EOL
));
}
if (in_array(SetList::TYPE_DECLARATION, $uniqueSets, true) && $this->isTypeCoverageLevelUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables type declarations set.%sRemove "->withTypeCoverageLevel()" as it only duplicates it, or remove type declaration set.',
PHP_EOL
));
}
if (in_array(SetList::DEAD_CODE, $uniqueSets, true) && $this->isDeadCodeLevelUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables dead code set.%sRemove "->withDeadCodeLevel()" as it only duplicates it, or remove dead code set.',
PHP_EOL
));
}
if (in_array(SetList::CODE_QUALITY, $uniqueSets, true) && $this->isCodeQualityLevelUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables code quality set.%sRemove "->withCodeQualityLevel()" as it only duplicates it, or remove code quality set.',
PHP_EOL
));
}
if (in_array(SetList::CODING_STYLE, $uniqueSets, true) && $this->isCodingStyleLevelUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables coding style set.%sRemove "->withCodingStyleLevel()" as it only duplicates it, or remove coding style set.',
PHP_EOL
));
}
if ($uniqueSets !== []) {
$rectorConfig->sets($uniqueSets);
}
// log rules from sets and compare them with explicit rules
$setRegisteredRectorClasses = $rectorConfig->getRectorClasses();
SimpleParameterProvider::addParameter(Option::SET_REGISTERED_RULES, $setRegisteredRectorClasses);
if ($this->paths !== []) {
$rectorConfig->paths($this->paths);
}
// must be in upper part, as these services might be used by rule registered bellow
foreach ($this->registerServices as $registerService) {
$rectorConfig->singleton($registerService->getClassName());
if ($registerService->getAlias()) {
$rectorConfig->alias($registerService->getClassName(), $registerService->getAlias());
}
if ($registerService->getTag()) {
$rectorConfig->tag($registerService->getClassName(), $registerService->getTag());
}
}
if ($this->skip !== []) {
$rectorConfig->skip($this->skip);
}
if ($this->rules !== []) {
$rectorConfig->rules($this->rules);
}
foreach ($this->rulesWithConfigurations as $rectorClass => $configurations) {
foreach ($configurations as $configuration) {
$rectorConfig->ruleWithConfiguration($rectorClass, $configuration);
}
}
if ($this->fileExtensions !== []) {
$rectorConfig->fileExtensions($this->fileExtensions);
}
if ($this->cacheClass !== null) {
$rectorConfig->cacheClass($this->cacheClass);
}
if ($this->cacheDirectory !== null) {
$rectorConfig->cacheDirectory($this->cacheDirectory);
}
if ($this->containerCacheDirectory !== null) {
$rectorConfig->containerCacheDirectory($this->containerCacheDirectory);
}
if ($this->importNames || $this->importDocBlockNames) {
$rectorConfig->importNames($this->importNames, $this->importDocBlockNames);
$rectorConfig->importShortClasses($this->importShortClasses);
}
if ($this->removeUnusedImports) {
$rectorConfig->removeUnusedImports($this->removeUnusedImports);
}
if ($this->noDiffs) {
$rectorConfig->noDiffs();
}
if ($this->memoryLimit !== null) {
$rectorConfig->memoryLimit($this->memoryLimit);
}
if ($this->autoloadPaths !== []) {
$rectorConfig->autoloadPaths($this->autoloadPaths);
}
if ($this->bootstrapFiles !== []) {
$rectorConfig->bootstrapFiles($this->bootstrapFiles);
}
if ($this->indentChar !== ' ' || $this->indentSize !== 4) {
$rectorConfig->indent($this->indentChar, $this->indentSize);
}
if ($this->phpstanConfigs !== []) {
$rectorConfig->phpstanConfigs($this->phpstanConfigs);
}
if ($this->phpVersion !== null) {
$rectorConfig->phpVersion($this->phpVersion);
}
if ($this->parallel !== null) {
if ($this->parallel) {
$rectorConfig->parallel(
processTimeout: $this->parallelTimeoutSeconds,
maxNumberOfProcess: $this->parallelMaxNumberOfProcess,
jobSize: $this->parallelJobSize
);
} else {
$rectorConfig->disableParallel();
}
}
if ($this->symfonyContainerXmlFile !== null) {
$rectorConfig->symfonyContainerXml($this->symfonyContainerXmlFile);
}
if ($this->symfonyContainerPhpFile !== null) {
$rectorConfig->symfonyContainerPhp($this->symfonyContainerPhpFile);
}
if ($this->isFluentNewLine !== null) {
$rectorConfig->newLineOnFluentCall($this->isFluentNewLine);
}
if ($this->reportingRealPath !== null) {
$rectorConfig->reportingRealPath($this->reportingRealPath);
}
if ($this->editorUrl !== null) {
$rectorConfig->editorUrl($this->editorUrl);
}
if ($this->levelOverflows !== []) {
$rectorConfig->setOverflowLevels($this->levelOverflows);
}
}
/**
* @param string[] $paths
*/
public function withPaths(array $paths): self
{
$this->paths = $paths;
return $this;
}
/**
* @param array<mixed> $skip
*/
public function withSkip(array $skip): self
{
$this->skip = array_merge($this->skip, $skip);
return $this;
}
public function withSkipPath(string $skipPath): self
{
if (! str_contains($skipPath, '*')) {
Assert::fileExists($skipPath);
}
return $this->withSkip([$skipPath]);
}
/**
* Include PHP files from the root directory (including hidden ones),
* typically ecs.php, rector.php, .php-cs-fixer.dist.php etc.
*/
public function withRootFiles(): self
{
$rootPhpFilesFinder = (new Finder())->files()
->in(getcwd())
->depth(0)
->ignoreDotFiles(false)
->ignoreVCSIgnored(true)
->name('*.php')
->name('.*.php')
// this file cannot be interpreted as PHP file
// https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html#expected-arguments
->notName('.phpstorm.meta.php')
;
foreach ($rootPhpFilesFinder as $rootPhpFileFinder) {
$path = $rootPhpFileFinder->getRealPath();
$this->paths[] = $path;
}
return $this;
}
/**
* @param string[] $sets
*/
public function withSets(array $sets): self
{
$this->sets = array_merge($this->sets, $sets);
return $this;
}
/**
* Upgrade your annotations to attributes
*/
public function withAttributesSets(
bool $symfony = false,
bool $doctrine = false,
bool $mongoDb = false,
bool $gedmo = false,
bool $phpunit = false,
bool $fosRest = false,
bool $jms = false,
bool $sensiolabs = false,
bool $behat = false,
bool $all = false
): self {
// if nothing is passed, enable all as convention in other method
if (func_get_args() === []) {
$all = true;
}
if ($symfony || $all) {
$this->sets[] = SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES;
}
if ($doctrine || $all) {
$this->sets[] = DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES;
}
if ($mongoDb || $all) {
$this->sets[] = DoctrineSetList::MONGODB__ANNOTATIONS_TO_ATTRIBUTES;
}
if ($gedmo || $all) {
$this->sets[] = DoctrineSetList::GEDMO_ANNOTATIONS_TO_ATTRIBUTES;
}
if ($phpunit || $all) {
$this->sets[] = PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES;
}
if ($fosRest || $all) {
$this->sets[] = FOSRestSetList::ANNOTATIONS_TO_ATTRIBUTES;
}
if ($jms || $all) {
$this->sets[] = JMSSetList::ANNOTATIONS_TO_ATTRIBUTES;
}
if ($sensiolabs || $all) {
$this->sets[] = SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES;
}
if ($behat || $all) {
$this->sets[] = SetList::BEHAT_ANNOTATIONS_TO_ATTRIBUTES;
}
return $this;
}
/**
* @deprecated Already included in withPhpSets(), no need to repeat
* make use of polyfill packages in composer.json
*/
public function withPhpPolyfill(): never
{
throw new InvalidConfigurationException(sprintf(
'Method "%s()" is deprecated and is now part of ->withPhpSets() to avoid duplications and too granular configuration.',
__METHOD__,
));
}
/**
* What PHP sets should be applied? By default the same version
* as composer.json has is used
*/
public function withPhpSets(
bool $php83 = false,
bool $php82 = false,
bool $php81 = false,
bool $php80 = false,
bool $php74 = false,
bool $php73 = false,
bool $php72 = false,
bool $php71 = false,
bool $php70 = false,
bool $php56 = false,
bool $php55 = false,
bool $php54 = false,
bool $php53 = false,
bool $php84 = false, // place on later as BC break when used in php 7.x without named arg
): self {
if ($this->isWithPhpSetsUsed === true) {
throw new InvalidConfigurationException(sprintf(
'Method "%s()" can be called only once. It always includes all previous sets UP TO the defined version.%sThe best practise is to call it once with no argument. That way it will pick up PHP version from composer.json and your project will always stay up to date.',
__METHOD__,
PHP_EOL
));
}
$this->isWithPhpSetsUsed = true;
$pickedArguments = array_filter(func_get_args());
if ($pickedArguments !== []) {
Notifier::errorWithPhpSetsNotSuitableForPHP74AndLower();
}
if (count($pickedArguments) > 1) {
throw new InvalidConfigurationException(
sprintf(
'Pick only one version target in "withPhpSets()". All rules up to this version will be used.%sTo use your composer.json PHP version, keep arguments empty.',
PHP_EOL
)
);
}
if ($pickedArguments === []) {
$projectPhpVersion = ComposerJsonPhpVersionResolver::resolveFromCwdOrFail();
$phpLevelSets = PhpLevelSetResolver::resolveFromPhpVersion($projectPhpVersion);
$this->sets = array_merge($this->sets, $phpLevelSets);
return $this;
}
if ($php53) {
$this->withPhp53Sets();
return $this;
}
if ($php54) {
$this->withPhp54Sets();
return $this;
}
if ($php55) {
$this->withPhp55Sets();
return $this;
}
if ($php56) {
$this->withPhp56Sets();
return $this;
}
if ($php70) {
$this->withPhp70Sets();
return $this;
}
if ($php71) {
$this->withPhp71Sets();
return $this;
}
if ($php72) {
$this->withPhp72Sets();
return $this;
}
if ($php73) {
$this->withPhp73Sets();
return $this;
}
if ($php74) {
$this->withPhp74Sets();
return $this;
}
if ($php80) {
$targetPhpVersion = PhpVersion::PHP_80;
} elseif ($php81) {
$targetPhpVersion = PhpVersion::PHP_81;
} elseif ($php82) {
$targetPhpVersion = PhpVersion::PHP_82;
} elseif ($php83) {
$targetPhpVersion = PhpVersion::PHP_83;
} elseif ($php84) {
$targetPhpVersion = PhpVersion::PHP_84;
} else {
throw new InvalidConfigurationException('Invalid PHP version set');
}
$phpLevelSets = PhpLevelSetResolver::resolveFromPhpVersion($targetPhpVersion);
$this->sets = array_merge($this->sets, $phpLevelSets);
return $this;
}
/**
* Following methods are suitable for PHP 7.4 and lower, before named args
* Let's keep them without warning, in case Rector is run on both PHP 7.4 and PHP 8.0 in CI
*/
public function withPhp53Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_53));
return $this;
}
public function withPhp54Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_54));
return $this;
}
public function withPhp55Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_55));
return $this;
}
public function withPhp56Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_56));
return $this;
}
public function withPhp70Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_70));
return $this;
}
public function withPhp71Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_71));
return $this;
}
public function withPhp72Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_72));
return $this;
}
public function withPhp73Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_73));
return $this;
}
public function withPhp74Sets(): self
{
$this->isWithPhpSetsUsed = true;
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_74));
return $this;
}
// there is no withPhp80Sets() and above,
// as we already use PHP 8.0 and should go with withPhpSets() instead
public function withPreparedSets(
bool $deadCode = false,
bool $codeQuality = false,
bool $codingStyle = false,
bool $typeDeclarations = false,
bool $privatization = false,
bool $naming = false,
bool $instanceOf = false,
bool $earlyReturn = false,
bool $strictBooleans = false,
bool $carbon = false,
bool $rectorPreset = false,
bool $phpunitCodeQuality = false,
bool $doctrineCodeQuality = false,
bool $symfonyCodeQuality = false,
bool $symfonyConfigs = false,
): self {
Notifier::notifyNotSuitableMethodForPHP74(__METHOD__);
$setMap = [
SetList::DEAD_CODE => $deadCode,
SetList::CODE_QUALITY => $codeQuality,
SetList::CODING_STYLE => $codingStyle,
SetList::TYPE_DECLARATION => $typeDeclarations,
SetList::PRIVATIZATION => $privatization,
SetList::NAMING => $naming,
SetList::INSTANCEOF => $instanceOf,
SetList::EARLY_RETURN => $earlyReturn,
SetList::STRICT_BOOLEANS => $strictBooleans,
SetList::CARBON => $carbon,
SetList::RECTOR_PRESET => $rectorPreset,
PHPUnitSetList::PHPUNIT_CODE_QUALITY => $phpunitCodeQuality,
DoctrineSetList::DOCTRINE_CODE_QUALITY => $doctrineCodeQuality,
SymfonySetList::SYMFONY_CODE_QUALITY => $symfonyCodeQuality,
SymfonySetList::CONFIGS => $symfonyConfigs,
];
foreach ($setMap as $setPath => $isEnabled) {
if ($isEnabled) {
$this->sets[] = $setPath;
}
}
return $this;
}
public function withComposerBased(
bool $twig = false,
bool $doctrine = false,
bool $phpunit = false,
bool $symfony = \false
): self {
$setMap = [
SetGroup::TWIG => $twig,
SetGroup::DOCTRINE => $doctrine,
SetGroup::PHPUNIT => $phpunit,
SetGroup::SYMFONY => $symfony,
];
foreach ($setMap as $setPath => $isEnabled) {
if ($isEnabled) {
$this->setGroups[] = $setPath;
}
}
return $this;
}
/**
* @param array<class-string<RectorInterface>> $rules
*/
public function withRules(array $rules): self
{
$this->rules = array_merge($this->rules, $rules);
if (SimpleParameterProvider::provideBoolParameter(Option::IS_RECTORCONFIG_BUILDER_RECREATED, false) === false) {
// log all explicitly registered rules on root rector.php
// we only check the non-configurable rules, as the configurable ones might override them
$nonConfigurableRules = array_filter(
$rules,
fn (string $rule): bool => ! is_a($rule, ConfigurableRectorInterface::class, true)
);
SimpleParameterProvider::addParameter(Option::ROOT_STANDALONE_REGISTERED_RULES, $nonConfigurableRules);
}
return $this;
}
/**
* @param string[] $fileExtensions
*/
public function withFileExtensions(array $fileExtensions): self
{
$this->fileExtensions = $fileExtensions;
return $this;
}
/**
* @param class-string<CacheStorageInterface>|null $cacheClass
*/
public function withCache(
?string $cacheDirectory = null,
?string $cacheClass = null,
?string $containerCacheDirectory = null
): self {
$this->cacheDirectory = $cacheDirectory;
$this->cacheClass = $cacheClass;
$this->containerCacheDirectory = $containerCacheDirectory;
return $this;
}
/**
* @param class-string<ConfigurableRectorInterface> $rectorClass
* @param mixed[] $configuration
*/
public function withConfiguredRule(string $rectorClass, array $configuration): self
{
$this->rulesWithConfigurations[$rectorClass][] = $configuration;
return $this;
}
public function withParallel(
?int $timeoutSeconds = null,
?int $maxNumberOfProcess = null,
?int $jobSize = null
): self {
$this->parallel = true;
if (is_int($timeoutSeconds)) {
$this->parallelTimeoutSeconds = $timeoutSeconds;
}
if (is_int($maxNumberOfProcess)) {
$this->parallelMaxNumberOfProcess = $maxNumberOfProcess;
}
if (is_int($jobSize)) {
$this->parallelJobSize = $jobSize;
}
return $this;
}
public function withoutParallel(): self
{
$this->parallel = false;
return $this;
}
public function withImportNames(
bool $importNames = true,
bool $importDocBlockNames = true,
bool $importShortClasses = true,
bool $removeUnusedImports = false
): self {
$this->importNames = $importNames;
$this->importDocBlockNames = $importDocBlockNames;
$this->importShortClasses = $importShortClasses;
$this->removeUnusedImports = $removeUnusedImports;
return $this;
}
public function withNoDiffs(): self
{
$this->noDiffs = true;
return $this;
}
public function withMemoryLimit(string $memoryLimit): self
{
$this->memoryLimit = $memoryLimit;
return $this;
}
public function withIndent(string $indentChar = ' ', int $indentSize = 4): self
{
$this->indentChar = $indentChar;
$this->indentSize = $indentSize;
return $this;
}
/**
* @param string[] $autoloadPaths
*/
public function withAutoloadPaths(array $autoloadPaths): self
{
$this->autoloadPaths = $autoloadPaths;
return $this;
}
/**
* @param string[] $bootstrapFiles
*/
public function withBootstrapFiles(array $bootstrapFiles): self
{
$this->bootstrapFiles = $bootstrapFiles;
return $this;
}
/**
* @param string[] $phpstanConfigs
*/
public function withPHPStanConfigs(array $phpstanConfigs): self
{
$this->phpstanConfigs = $phpstanConfigs;
return $this;
}
/**
* @param PhpVersion::* $phpVersion
*/
public function withPhpVersion(int $phpVersion): self
{
$this->phpVersion = $phpVersion;
return $this;
}
public function withSymfonyContainerXml(string $symfonyContainerXmlFile): self
{
if (! str_ends_with($symfonyContainerXmlFile, '.xml')) {
throw new InvalidConfigurationException(sprintf(
'Provided dumped Symfony container must have "xml" suffix. "%s" given',
$symfonyContainerXmlFile
));
}
$this->symfonyContainerXmlFile = $symfonyContainerXmlFile;
return $this;
}
public function withSymfonyContainerPhp(string $symfonyContainerPhpFile): self
{
if (! str_ends_with($symfonyContainerPhpFile, '.php')) {
throw new InvalidConfigurationException(sprintf(
'Provided dumped Symfony container must have "php" suffix. "%s" given',
$symfonyContainerPhpFile
));
}
$this->symfonyContainerPhpFile = $symfonyContainerPhpFile;
return $this;
}
/**
* Raise your type coverage from the safest type rules
* to more affecting ones, one level at a time
*/
public function withTypeCoverageLevel(int $level): self
{
Assert::natural($level);
$this->isTypeCoverageLevelUsed = true;
$levelRules = LevelRulesResolver::resolve($level, TypeDeclarationLevel::RULES, __METHOD__);
// too high
$levelRulesCount = count($levelRules);
if ($levelRulesCount + self::MAX_LEVEL_GAP < $level) {
$this->levelOverflows[] = new LevelOverflow(
'withTypeCoverageLevel',
$level,
$levelRulesCount,
'typeDeclarations',
'TYPE_DECLARATION'
);
}
$this->rules = array_merge($this->rules, $levelRules);
return $this;
}
/**
* Raise your dead-code coverage from the safest rules
* to more affecting ones, one level at a time
*/
public function withDeadCodeLevel(int $level): self
{