Skip to content

False negative accessing array gap index guarded by count #13111

@jtojnar

Description

@jtojnar

Bug report

array_filter can create array with gaps so getting the first element using $arr[0] might not work. But guarding the array access by count($array) === 1 will make PHPStan happy:

https://phpstan.org/r/d924c125-9c42-41a3-a90d-04381950227b

Actually, array_filter is not even necessary:

Code snippet that reproduces the problem

https://phpstan.org/r/1e55cca6-5846-4d63-8c25-7539e069aba4

Expected output

PHPStan should print the following error (like if count is removed):

Offset 0 might not exist on array{0?: string, 1?: int}.

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

Hoping PHPStan could eventually detect j0k3r/php-readability#96, though we are not up to level four yet.

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