Skip to content

Return type inference sometimes wrong on nullable fields when methods are called #2850

@Firehed

Description

@Firehed

Bug report

When returning a non-nullable object from a nullable field and the code ensures that the value is non-null, method calls on the object inside the conditional can break the nullability erasure.

Code snippet that reproduces the problem

https://phpstan.org/r/d003958b-29d9-4957-a726-fdce34a19bf5

Expected output

No errors. When the method call is commented out, it works as expected:

https://phpstan.org/r/aadf2811-f2b6-4f8d-8116-77cb1fbcae14

Notes

I don't think there are any possible method calls that could set the parent object back to null, other than some very specific and bizarre actions on the Closure class. If the parent object was passed into the method then some really bad code possibly could, but not in the simple case in the example AFAIK.

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