Skip to content

PHPStan 2.1.9 crash Undefined variable: $field1 on dynamic creation #12767

@samsonasik

Description

@samsonasik

Bug report

Given the following code:

class SkipDynamicVariable
{
    public function bar()
    {
        $employee = (object) ['data' => ['dd1' => 1, 'dd2' => 2]];

        for ($i=1; $i <= 2; $i++) {
            ${'field'.$i} = $employee->data['dd'.$i];

			\PHPStan\dumpType(${'field'.$i});
        }
    }
}

It crash


Undefined variable: $field1

Ref https://phpstan.org/r/875104df-5fa4-4850-9be7-c1fbabdc99a5

Ref rectorphp/rector-src#6806

Code snippet that reproduces the problem

https://phpstan.org/r/875104df-5fa4-4850-9be7-c1fbabdc99a5

Expected output

It should no crash

Did PHPStan help you today? Did it make you happy in any way?

No response

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