Skip to content

Property value not changing type after reassigning in an if that checks its type #12947

@edzus

Description

@edzus

Bug report

It looks like setting a new property value (after checking its value in an if) does not change the type that PHPStan expects it to be. On local variables works as expected.

if (! $this->code) {
    $this->code = stringOrNull();
    $otherVar = stringOrNull();

    \PHPStan\dumpType($this->code);
    // Dumped type: ''|'0'|null

    \PHPStan\dumpType($otherVar);
    // Dumped type: string|null
}

Problem occurs in PHPStan 2.1.13. No problem in 2.1.12.

Code snippet that reproduces the problem

https://phpstan.org/r/17149323-3d3f-49e8-af7c-8548cbe95d3e

Expected output

Both dumped types should be the same - string|null

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

PHPStan made our codebase a lot more pleasant to work with (and eliminated a lot of errors). It is a must have for all my projects. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions