Skip to content

Fix RemoveDeadIfBlockRector dropping else block when merging empty if with elseif - #8022

Merged
TomasVotruba merged 1 commit into
mainfrom
fix/9776-remove-dead-if-block-drops-else
Jun 2, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix/9776-remove-dead-if-block-drops-else

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9776

Problem

When an if block has an empty body but is followed by an elseif (with statements) and an else block, RemoveDeadIfBlockRector merges the negated if condition into the first elseif, but silently drops the else block.

Input

if ($qty < 1) {
} elseif (!$cond) {
    $total = $qty * $price1;
} else {
    $total = $qty * $price2;
}

Actual (buggy)

if ($qty >= 1 && !$cond) {
    $total = $qty * $price1;
}

Expected

if ($qty >= 1 && !$cond) {
    $total = $qty * $price1;
} else {
    $total = $qty * $price2;
}

Fix

When constructing the new merged If_ node, carry over the original else block (alongside the already-handled remaining elseifs).

A fixture covering the issue's reproduction has been added.

@TomasVotruba
TomasVotruba merged commit 56fa09d into main Jun 2, 2026
62 checks passed
@TomasVotruba

Copy link
Copy Markdown
Member Author

LGTM 👍

@TomasVotruba
TomasVotruba deleted the fix/9776-remove-dead-if-block-drops-else branch June 2, 2026 19:16
TomasVotruba added a commit that referenced this pull request Jun 16, 2026
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Nov 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

RemoveDeadIfBlockRector removes else condition

1 participant