From d5da5ee5ec940c38bcda2b72d889728a9cff3c55 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 30 Jul 2026 20:21:13 +0200 Subject: [PATCH] refactor: deduplicate PHP version branching in RectorConfigBuilder withPhpSets() mapped 16 bool arguments to a target version with nine "if ($phpNN) { $this->withPhpNNSets(); return $this; }" blocks followed by a seven-branch elseif chain. withDowngradeSets() had the same shape with nine branches. Both become an array_filter() over a version-keyed map. The nine withPhpNNSets() methods repeated the same three statements each. Extract them into a private addPhpLevelSets() helper, which withPhpSets() now shares. --- src/Configuration/RectorConfigBuilder.php | 203 +++++++--------------- 1 file changed, 60 insertions(+), 143 deletions(-) diff --git a/src/Configuration/RectorConfigBuilder.php b/src/Configuration/RectorConfigBuilder.php index 1a5c385b744..c3d90f17917 100644 --- a/src/Configuration/RectorConfigBuilder.php +++ b/src/Configuration/RectorConfigBuilder.php @@ -588,12 +588,30 @@ public function withPhpSets( $this->isWithPhpSetsUsed = true; - $pickedArguments = array_filter(func_get_args()); - if ($pickedArguments !== []) { + $pickedPhpVersions = array_keys(array_filter([ + PhpVersion::PHP_53 => $php53, + PhpVersion::PHP_54 => $php54, + PhpVersion::PHP_55 => $php55, + PhpVersion::PHP_56 => $php56, + PhpVersion::PHP_70 => $php70, + PhpVersion::PHP_71 => $php71, + PhpVersion::PHP_72 => $php72, + PhpVersion::PHP_73 => $php73, + PhpVersion::PHP_74 => $php74, + PhpVersion::PHP_80 => $php80, + PhpVersion::PHP_81 => $php81, + PhpVersion::PHP_82 => $php82, + PhpVersion::PHP_83 => $php83, + PhpVersion::PHP_84 => $php84, + PhpVersion::PHP_85 => $php85, + PhpVersion::PHP_86 => $php86, + ])); + + if ($pickedPhpVersions !== []) { Notifier::errorWithPhpSetsNotSuitableForPHP74AndLower(); } - if (count($pickedArguments) > 1) { + if (count($pickedPhpVersions) > 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.', @@ -602,82 +620,12 @@ public function withPhpSets( ); } - 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; + // no version picked, resolve it from the project composer.json + if ($pickedPhpVersions === []) { + return $this->addPhpLevelSets(ComposerJsonPhpVersionResolver::resolveFromCwdOrFail()); } - 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; - } elseif ($php85) { - $targetPhpVersion = PhpVersion::PHP_85; - } elseif ($php86) { - $targetPhpVersion = PhpVersion::PHP_86; - } else { - throw new InvalidConfigurationException('Invalid PHP version set'); - } - - $phpLevelSets = PhpLevelSetResolver::resolveFromPhpVersion($targetPhpVersion); - $this->sets = array_merge($this->sets, $phpLevelSets); - - return $this; + return $this->addPhpLevelSets($pickedPhpVersions[0]); } /** @@ -686,83 +634,47 @@ public function withPhpSets( */ public function withPhp53Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_53)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_53); } public function withPhp54Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_54)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_54); } public function withPhp55Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_55)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_55); } public function withPhp56Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_56)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_56); } public function withPhp70Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_70)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_70); } public function withPhp71Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_71)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_71); } public function withPhp72Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_72)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_72); } public function withPhp73Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_73)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_73); } public function withPhp74Sets(): self { - $this->isWithPhpSetsUsed = true; - - $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_74)); - - return $this; + return $this->addPhpLevelSets(PhpVersion::PHP_74); } // there is no withPhp80Sets() and above, @@ -1214,32 +1126,25 @@ public function withDowngradeSets( bool $php72 = false, bool $php71 = false, ): self { - $pickedArguments = array_filter(func_get_args()); - if (count($pickedArguments) !== 1) { + $pickedDowngradeSets = array_keys(array_filter([ + DowngradeLevelSetList::DOWN_TO_PHP_84 => $php84, + DowngradeLevelSetList::DOWN_TO_PHP_83 => $php83, + DowngradeLevelSetList::DOWN_TO_PHP_82 => $php82, + DowngradeLevelSetList::DOWN_TO_PHP_81 => $php81, + DowngradeLevelSetList::DOWN_TO_PHP_80 => $php80, + DowngradeLevelSetList::DOWN_TO_PHP_74 => $php74, + DowngradeLevelSetList::DOWN_TO_PHP_73 => $php73, + DowngradeLevelSetList::DOWN_TO_PHP_72 => $php72, + DowngradeLevelSetList::DOWN_TO_PHP_71 => $php71, + ])); + + if (count($pickedDowngradeSets) !== 1) { throw new InvalidConfigurationException( 'Pick only one PHP version target in "withDowngradeSets()". All rules down to this version will be used.', ); } - if ($php84) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_84; - } elseif ($php83) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_83; - } elseif ($php82) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_82; - } elseif ($php81) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_81; - } elseif ($php80) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_80; - } elseif ($php74) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_74; - } elseif ($php73) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_73; - } elseif ($php72) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_72; - } elseif ($php71) { - $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_71; - } + $this->sets[] = $pickedDowngradeSets[0]; return $this; } @@ -1293,6 +1198,18 @@ public function withSetProviders(string ...$setProviders): self return $this; } + /** + * @param PhpVersion::* $phpVersion + */ + private function addPhpLevelSets(int $phpVersion): self + { + $this->isWithPhpSetsUsed = true; + + $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion($phpVersion)); + + return $this; + } + /** * @param array> $availableRules */