Skip to content

Failed to infer iterable type when iterating on \Generator<someType>|null #5998

@dktapps

Description

@dktapps

Bug report

I know that iterating on Generator|null is a bug, but it's not reported until level 7.

When up to level 6, PHPStan does not report a problem and should be able to understand what the iterable type will be in the case where it's not null.

Code snippet that reproduces the problem

https://phpstan.org/r/c9fc2ca5-501f-4fbe-bdcc-cf14a9f12ea9

Expected output

The dumped type should be a union of Block and *ERROR*. It may be an stdClass or it may not be iterable at all.

In addition, an error should be reported on line 18, because neither *ERROR* nor Block has a method called getPos() (definite crash).

Additional notes

I consider this a serious problem because it's an obvious bug that otherwise won't get detected until reaching level 8. This means that reaching level 6 provides a false sense of security while missing problems like this.

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

It's greatly assisted me while updating an old project to a newer API.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions