Skip to content

Deduplicate PHP version branching in RectorConfigBuilder - #8227

Merged
TomasVotruba merged 1 commit into
mainfrom
dedup-php-sets
Jul 30, 2026
Merged

Deduplicate PHP version branching in RectorConfigBuilder#8227
TomasVotruba merged 1 commit into
mainfrom
dedup-php-sets

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Follow-up to #8225, same class. Two methods mapped a pile of bool arguments to a PHP version through long branch chains, and nine more methods repeated the same three statements.

withPhpSets()

Sixteen bool arguments were resolved by nine early-return blocks followed by a seven-branch elseif chain:

if ($php53) {
    $this->withPhp53Sets();
    return $this;
}

if ($php54) {
    $this->withPhp54Sets();
    return $this;
}

// ... seven more of these ...

if ($php80) {
    $targetPhpVersion = PhpVersion::PHP_80;
} elseif ($php81) {
    $targetPhpVersion = PhpVersion::PHP_81;
} elseif ($php82) {
// ... five more ...
} else {
    throw new InvalidConfigurationException('Invalid PHP version set');
}

Now one map:

$pickedPhpVersions = array_keys(array_filter([
    PhpVersion::PHP_53 => $php53,
    PhpVersion::PHP_54 => $php54,
    // ...
    PhpVersion::PHP_86 => $php86,
]));

This also replaces the func_get_args() call that the arity checks were built on, so the "pick only one version" and "resolve from composer.json" branches read off the same array. The unreachable 'Invalid PHP version set' branch goes away — a non-empty picked list always has exactly one entry by that point.

The argument order in the signature is unchanged, including $php84/$php85/$php86 staying last for positional-call BC.

withDowngradeSets()

Same shape, nine branches:

-if ($php84) {
-    $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_84;
-} elseif ($php83) {
-    $this->sets[] = DowngradeLevelSetList::DOWN_TO_PHP_83;
-} elseif ($php82) {
-// ... six more ...
-}
+$pickedDowngradeSets = array_keys(array_filter([
+    DowngradeLevelSetList::DOWN_TO_PHP_84 => $php84,
+    DowngradeLevelSetList::DOWN_TO_PHP_83 => $php83,
+    // ...
+]));

The nine withPhpNNSets() methods

Each was the same three statements with one constant swapped:

public function withPhp70Sets(): self
{
    $this->isWithPhpSetsUsed = true;

    $this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_70));

    return $this;
}

becomes

public function withPhp70Sets(): self
{
    return $this->addPhpLevelSets(PhpVersion::PHP_70);
}

withPhpSets() uses the same private helper for both its composer.json branch and its explicit-version branch.

Verification

Public API and argument order are unchanged. Since these methods have no direct test coverage, I diffed old against new behavior over 14 cases through reflection on the resulting $sets: no arguments (composer.json resolution), php53/php74 by flag vs. by dedicated method, php80, php86, an explicitly-passed false, both downgrade targets, and all four throw paths (two versions at once, withPhpSets() called twice, no downgrade target, two downgrade targets). Output is byte-identical on every one.

Net -83 lines.

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.
@TomasVotruba
TomasVotruba merged commit 7ef3211 into main Jul 30, 2026
65 checks passed
@TomasVotruba
TomasVotruba deleted the dedup-php-sets branch July 30, 2026 19:36
@TomasVotruba
TomasVotruba restored the dedup-php-sets branch July 30, 2026 20:00
@TomasVotruba
TomasVotruba deleted the dedup-php-sets branch July 30, 2026 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant