Skip to content

[EarlyReturn] Skip several ifs assigning the returned variable in PreparedValueToEarlyReturnRector - #9033

Merged
TomasVotruba merged 1 commit into
mainfrom
prepared-value-early-return-multi-if
Nov 23, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
prepared-value-early-return-multi-if

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

PreparedValueToEarlyReturnRector changed behavior when several ifs assign the returned variable:

$var = null;
if (rand(0, 1)) { $var = 1; }
if (rand(0, 2)) { $var = 2; }
return $var;

became

if (rand(0, 1)) { return 1; }
if (rand(0, 2)) { return 2; }
return null;

When both conditions are true, the original returns 2 (last assignment wins), the new code returns 1 and never evaluates the second condition.

Fix - skip when more than one if assigns the variable before the return. A single if still converts. The 3 fixtures that encoded the bug are now skip fixtures, a single_if fixture is added and the rule doc sample is reduced to a single if.

@TomasVotruba
TomasVotruba merged commit 9c8e623 into main Nov 23, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the prepared-value-early-return-multi-if branch November 23, 2026 22:25
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