Skip to content

Commit 7ef3211

Browse files
authored
Deduplicate PHP version branching in RectorConfigBuilder (#8227)
1 parent ea5d0a4 commit 7ef3211

1 file changed

Lines changed: 60 additions & 143 deletions

File tree

‎src/Configuration/RectorConfigBuilder.php‎

Lines changed: 60 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,30 @@ public function withPhpSets(
588588

589589
$this->isWithPhpSetsUsed = true;
590590

591-
$pickedArguments = array_filter(func_get_args());
592-
if ($pickedArguments !== []) {
591+
$pickedPhpVersions = array_keys(array_filter([
592+
PhpVersion::PHP_53 => $php53,
593+
PhpVersion::PHP_54 => $php54,
594+
PhpVersion::PHP_55 => $php55,
595+
PhpVersion::PHP_56 => $php56,
596+
PhpVersion::PHP_70 => $php70,
597+
PhpVersion::PHP_71 => $php71,
598+
PhpVersion::PHP_72 => $php72,
599+
PhpVersion::PHP_73 => $php73,
600+
PhpVersion::PHP_74 => $php74,
601+
PhpVersion::PHP_80 => $php80,
602+
PhpVersion::PHP_81 => $php81,
603+
PhpVersion::PHP_82 => $php82,
604+
PhpVersion::PHP_83 => $php83,
605+
PhpVersion::PHP_84 => $php84,
606+
PhpVersion::PHP_85 => $php85,
607+
PhpVersion::PHP_86 => $php86,
608+
]));
609+
610+
if ($pickedPhpVersions !== []) {
593611
Notifier::errorWithPhpSetsNotSuitableForPHP74AndLower();
594612
}
595613

596-
if (count($pickedArguments) > 1) {
614+
if (count($pickedPhpVersions) > 1) {
597615
throw new InvalidConfigurationException(
598616
sprintf(
599617
'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.',
@@ -602,82 +620,12 @@ public function withPhpSets(
602620
);
603621
}
604622

605-
if ($pickedArguments === []) {
606-
$projectPhpVersion = ComposerJsonPhpVersionResolver::resolveFromCwdOrFail();
607-
$phpLevelSets = PhpLevelSetResolver::resolveFromPhpVersion($projectPhpVersion);
608-
609-
$this->sets = array_merge($this->sets, $phpLevelSets);
610-
611-
return $this;
612-
}
613-
614-
if ($php53) {
615-
$this->withPhp53Sets();
616-
return $this;
617-
}
618-
619-
if ($php54) {
620-
$this->withPhp54Sets();
621-
return $this;
622-
}
623-
624-
if ($php55) {
625-
$this->withPhp55Sets();
626-
return $this;
627-
}
628-
629-
if ($php56) {
630-
$this->withPhp56Sets();
631-
return $this;
632-
}
633-
634-
if ($php70) {
635-
$this->withPhp70Sets();
636-
return $this;
637-
}
638-
639-
if ($php71) {
640-
$this->withPhp71Sets();
641-
return $this;
642-
}
643-
644-
if ($php72) {
645-
$this->withPhp72Sets();
646-
return $this;
647-
}
648-
649-
if ($php73) {
650-
$this->withPhp73Sets();
651-
return $this;
623+
// no version picked, resolve it from the project composer.json
624+
if ($pickedPhpVersions === []) {
625+
return $this->addPhpLevelSets(ComposerJsonPhpVersionResolver::resolveFromCwdOrFail());
652626
}
653627

654-
if ($php74) {
655-
$this->withPhp74Sets();
656-
return $this;
657-
}
658-
659-
if ($php80) {
660-
$targetPhpVersion = PhpVersion::PHP_80;
661-
} elseif ($php81) {
662-
$targetPhpVersion = PhpVersion::PHP_81;
663-
} elseif ($php82) {
664-
$targetPhpVersion = PhpVersion::PHP_82;
665-
} elseif ($php83) {
666-
$targetPhpVersion = PhpVersion::PHP_83;
667-
} elseif ($php84) {
668-
$targetPhpVersion = PhpVersion::PHP_84;
669-
} elseif ($php85) {
670-
$targetPhpVersion = PhpVersion::PHP_85;
671-
} elseif ($php86) {
672-
$targetPhpVersion = PhpVersion::PHP_86;
673-
} else {
674-
throw new InvalidConfigurationException('Invalid PHP version set');
675-
}
676-
677-
$phpLevelSets = PhpLevelSetResolver::resolveFromPhpVersion($targetPhpVersion);
678-
$this->sets = array_merge($this->sets, $phpLevelSets);
679-
680-
return $this;
628+
return $this->addPhpLevelSets($pickedPhpVersions[0]);
681629
}
682630

683631
/**
@@ -686,83 +634,47 @@ public function withPhpSets(
686634
*/
687635
public function withPhp53Sets(): self
688636
{
689-
$this->isWithPhpSetsUsed = true;
690-
691-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_53));
692-
693-
return $this;
637+
return $this->addPhpLevelSets(PhpVersion::PHP_53);
694638
}
695639

696640
public function withPhp54Sets(): self
697641
{
698-
$this->isWithPhpSetsUsed = true;
699-
700-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_54));
701-
702-
return $this;
642+
return $this->addPhpLevelSets(PhpVersion::PHP_54);
703643
}
704644

705645
public function withPhp55Sets(): self
706646
{
707-
$this->isWithPhpSetsUsed = true;
708-
709-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_55));
710-
711-
return $this;
647+
return $this->addPhpLevelSets(PhpVersion::PHP_55);
712648
}
713649

714650
public function withPhp56Sets(): self
715651
{
716-
$this->isWithPhpSetsUsed = true;
717-
718-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_56));
719-
720-
return $this;
652+
return $this->addPhpLevelSets(PhpVersion::PHP_56);
721653
}
722654

723655
public function withPhp70Sets(): self
724656
{
725-
$this->isWithPhpSetsUsed = true;
726-
727-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_70));
728-
729-
return $this;
657+
return $this->addPhpLevelSets(PhpVersion::PHP_70);
730658
}
731659

732660
public function withPhp71Sets(): self
733661
{
734-
$this->isWithPhpSetsUsed = true;
735-
736-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_71));
737-
738-
return $this;
662+
return $this->addPhpLevelSets(PhpVersion::PHP_71);
739663
}
740664

741665
public function withPhp72Sets(): self
742666
{
743-
$this->isWithPhpSetsUsed = true;
744-
745-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_72));
746-
747-
return $this;
667+
return $this->addPhpLevelSets(PhpVersion::PHP_72);
748668
}
749669

750670
public function withPhp73Sets(): self
751671
{
752-
$this->isWithPhpSetsUsed = true;
753-
754-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_73));
755-
756-
return $this;
672+
return $this->addPhpLevelSets(PhpVersion::PHP_73);
757673
}
758674

759675
public function withPhp74Sets(): self
760676
{
761-
$this->isWithPhpSetsUsed = true;
762-
763-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_74));
764-
765-
return $this;
677+
return $this->addPhpLevelSets(PhpVersion::PHP_74);
766678
}
767679

768680
// there is no withPhp80Sets() and above,
@@ -1214,32 +1126,25 @@ public function withDowngradeSets(
12141126
bool $php72 = false,
12151127
bool $php71 = false,
12161128
): self {
1217-
$pickedArguments = array_filter(func_get_args());
1218-
if (count($pickedArguments) !== 1) {
1129+
$pickedDowngradeSets = array_keys(array_filter([
1130+
DowngradeLevelSetList::DOWN_TO_PHP_84 => $php84,
1131+
DowngradeLevelSetList::DOWN_TO_PHP_83 => $php83,
1132+
DowngradeLevelSetList::DOWN_TO_PHP_82 => $php82,
1133+
DowngradeLevelSetList::DOWN_TO_PHP_81 => $php81,
1134+
DowngradeLevelSetList::DOWN_TO_PHP_80 => $php80,
1135+
DowngradeLevelSetList::DOWN_TO_PHP_74 => $php74,
1136+
DowngradeLevelSetList::DOWN_TO_PHP_73 => $php73,
1137+
DowngradeLevelSetList::DOWN_TO_PHP_72 => $php72,
1138+
DowngradeLevelSetList::DOWN_TO_PHP_71 => $php71,
1139+
]));
1140+
1141+
if (count($pickedDowngradeSets) !== 1) {
12191142
throw new InvalidConfigurationException(
12201143
'Pick only one PHP version target in "withDowngradeSets()". All rules down to this version will be used.',
12211144
);
12221145
}
12231146

1224-
if ($php84) {
1225-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_84;
1226-
} elseif ($php83) {
1227-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_83;
1228-
} elseif ($php82) {
1229-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_82;
1230-
} elseif ($php81) {
1231-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_81;
1232-
} elseif ($php80) {
1233-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_80;
1234-
} elseif ($php74) {
1235-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_74;
1236-
} elseif ($php73) {
1237-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_73;
1238-
} elseif ($php72) {
1239-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_72;
1240-
} elseif ($php71) {
1241-
$this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_71;
1242-
}
1147+
$this->sets[] = $pickedDowngradeSets[0];
12431148

12441149
return $this;
12451150
}
@@ -1293,6 +1198,18 @@ public function withSetProviders(string ...$setProviders): self
12931198
return $this;
12941199
}
12951200

1201+
/**
1202+
* @param PhpVersion::* $phpVersion
1203+
*/
1204+
private function addPhpLevelSets(int $phpVersion): self
1205+
{
1206+
$this->isWithPhpSetsUsed = true;
1207+
1208+
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion($phpVersion));
1209+
1210+
return $this;
1211+
}
1212+
12961213
/**
12971214
* @param array<class-string<RectorInterface>> $availableRules
12981215
*/

0 commit comments

Comments
 (0)