Skip to content

ArrayDimFetch probably null on Iterator fetch detected as non null #9784

@samsonasik

Description

@samsonasik

Bug report

For example, I have the following code:

final class DemoFile
{
    private function complementArticleData(Iterator $articles): void
    {
        $result = [
            'artikel' => $articles,
            'farben' => null,
            'artikel_ids' => [],
        ];

        // collect article ids
        foreach ($result['artikel'] as $article) {
            $result['artikel_ids'][] = 1;
        }

        if ($result['artikel_ids'] !== []) {
            $result['farben'] = new stdClass();
        }

        // $result['farben'] might be also null
		\PHPStan\dumpType($result['farben']);
        if ($result['farben'] instanceof stdClass) {
            echo '123';
        }
    }
}

Above, \PHPStan\dumpType($result['farben']); should be null|stdClass, but detected only as stdClass.

Code snippet that reproduces the problem

https://phpstan.org/r/05fd1758-7df0-4307-b419-dc29bd867ae4

Expected output

It should output null|stdClass as type.

Array vs Iterator result:

Array: working OK https://phpstan.org/r/dcded1e7-0941-4d6b-9451-c33face0ff89
Iterator: NOT working https://phpstan.org/r/05fd1758-7df0-4307-b419-dc29bd867ae4

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