Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Filtered array of enum incorrectly narrowed to empty array #13421

@podoko

Description

@podoko

Bug report

Phpstan wrongly assumes an array of Enums to be empty when filtered against a non-empty-array of the same enum. (see submitted code snippet)

There doesn't seem to be an error when filtering against a possibly empty array : https://phpstan.org/r/baddcabd-e72b-42c8-a7b2-ab8eb4018483

There is no problem either when the filter function is saved in a temp variable : https://phpstan.org/r/8a1090e8-19e9-4b1f-9e63-cd97feb966ba

Though the dumped types for the filtered array are not the same which is odd :

  • with possibly empy array : Dumped type: array{0?: Bar::Bar1, 1?: Bar::Bar2, 2?: Bar::Bar3}
  • temporary $filter callback : Dumped type: array<0|1|2, Bar::Bar1|Bar::Bar2|Bar::Bar3>

Note : The same error occurs when filtering against an array with only one value of the enum (but the actual value is not specified) : https://phpstan.org/r/7437d4c7-0723-4292-89cf-62f4388c6ae1

Code snippet that reproduces the problem

https://phpstan.org/r/3b314e0f-eb01-4d5b-8ad3-b416d9a27c25

Expected output

I would expect no error to be reported as $filteredBars might not be empty.

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

Helps me everyday. Lastly it helped me make sure I didn't break anything during a refacto.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions