Skip to content

[DeadCode] Skip RemoveAlwaysTrueIfConditionRector on maybe undefined variable - #8501

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-9904-always-true-maybe-undefined-var
Sep 16, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-9904-always-true-maybe-undefined-var

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9904

A variable only assigned inside conditional branches is maybe-undefined. On code paths where it stays undefined, PHP treats it as null, so the if condition is not always true. The rule now skips removal when a condition variable is not certainly defined in the current scope.

Before:

if ($username !== INTERFACE_USERNAME) {
    $error = true;
}
if ($password !== INTERFACE_PASSWORD) {
    $error = true;
}
if ($error) { // wrongly removed, $error can be undefined
    $response->setAttribute('status', 'ERROR');
}

@TomasVotruba
TomasVotruba merged commit 4d5420d into main Sep 16, 2026
45 checks passed
@TomasVotruba
TomasVotruba deleted the fix-9904-always-true-maybe-undefined-var branch September 16, 2026 14:57
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.

RemoveAlwaysTrueIfConditionRector does not work with potentially undefined variable

1 participant