Skip to content

RemoveDeadIfBlockRector crashes on an if / elseif chain with empty branches #9718

Description

@seremenko

Rector version: 2.3.x
PHP version: 8.3.13

Problem

Rector crashes while processing a method that contains an if / elseif chain with empty branches.

Error:

PhpParser\Node\Expr\BinaryOp::__construct(): Argument #2 ($right) must be of type PhpParser\Node\Expr, null given

The stack trace points to:

vendor/rector/rector/rules/DeadCode/Rector/If_/RemoveDeadIfBlockRector.php:121

Expected behavior

Rector should safely skip the node or produce a valid transformation. It should not crash while constructing a BinaryOp.

Minimal reproduction

<?php

final class Demo
{
    public function run(): void
    {
        $value = 1;
        $flag = true;

        if ($flag) {
        } elseif ($value === 2) {
        } elseif ($value === 3 || $value === 4) {
            echo "matched";
        }
    }
}

Run Rector with dead code rules enabled:

vendor/bin/rector process demo.php --dry-run --debug

Notes

The crash happens during dead-code analysis of the if / elseif structure, specifically when Rector tries to remove dead branches.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions