Skip to content

[DeadCode] Skip nested conditional assign in RemoveDefaultValueFromAssignedPropertyRector - #8915

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-9957-nested-conditional-assign
Nov 3, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
fix-9957-nested-conditional-assign

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9957

RemoveDefaultValueFromAssignedPropertyRector wrongly removed a property default when the only assign lived inside a nested if/else that is itself wrapped in an outer conditional:

final class DemoFile
{
    private string $a = '';

    public function __construct(bool $value)
    {
        if ($value) {
            if (mt_rand(0, 1)) {
                $this->a = 'a';
            } else {
                $this->a = 'b';
            }
        }

        echo $this->a;
    }
}

The outer if can skip the whole block, so the property is not guaranteed to be assigned. Removing the default produced:

Typed property DemoFile::$a must not be accessed before initialization

ConstructorAssignDetector::isIfElseAssign() treated any if/else where both branches assign as guaranteed, ignoring that the if was nested. The fix only counts the if/else when it is a first-level statement of the constructor, matching the existing guard used for direct assigns.

@TomasVotruba
TomasVotruba merged commit 16b4632 into main Nov 3, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the fix-9957-nested-conditional-assign branch November 3, 2026 18:31
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.

RemoveDefaultValueFromAssignedPropertyRector does not work with nested conditional assignments

1 participant