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

Fix RemoveDeadIfBlockRector dropping else block when merging empty if with elseif#8022
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
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.

RemoveDeadIfBlockRector removes else condition

1 participant