Skip to content

Unresolvable loop causes incorrect behavior of AddDefaultValueForUndefinedVariableRector #7777

@jackbentley

Description

@jackbentley

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/e1b4cf4e-c34f-466d-ac1a-4ecdbffca61c

<?php

final class DemoFile
{   
    public function run(Item $yes)
    {
        while (true !== false) {
        }
        
        $a = [];
        
        if ($b = $yes) {
            $a[] = 'test';
        }
        
        return $a;
    }
}

More real-world example:

See https://getrector.com/demo/c74cd93b-9c55-41d6-b831-24f952405737

<?php

final class Item
{
    private $parent;
    
    /**
     * @return Item
     */
    public function getParent()
    {
        return $this->parent;
    }    
}

final class DemoFile
{
    private function create()
    {
        return [];
    }
    
    public function run(Item $yes)
    {
        while (($yes = $yes->getParent()) !== null) {
        }
        
        $a = $this->create();
        
        if ($b = $yes) {
            $a[] = 'test';
        }
        
        return $a;
    }
}

Responsible rules

  • AddDefaultValueForUndefinedVariableRector

Expected Behavior

Rector shouldn't "fix" this. The variable is always set. Removing the while makes Rector think the code is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions