Skip to content

PHPStan (wrongly) thinks that assigning mixed to a typed property will change its type #12393

@dktapps

Description

@dktapps

Bug report

In this test case, we know that assigning anything that isn't a string to $this->name will throw a TypeError, or coerce it to the correct type.

Therefore, the type inference by PHPStan is behaving wrongly, as if the property didn't have a static type.

Code snippet that reproduces the problem

https://phpstan.org/r/8018a918-f830-4b9a-8b1e-315958276fef

Expected output

No errors should be reported. $this->name is guaranteed to be some subtype of string (or uninitialized) no matter what inputs are given to the function, since PHP will bail out if the type is wrong.

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

No response

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