Skip to content

Incorrect type refinement with in_array(..., strict: true) #5759

@Firehed

Description

@Firehed

Bug report

Somewhere between 0.12.94 and 0.12.99, a correctly-reachable bit of code following an in_array(const-enum, array-of-const-enums, true) check started leading to incorrectly marking the value as unreachable. Using \PHPStan\dumpType($array) shows the correct set of possible values entering the function and conditional.

I suspect this is in some way related to the recent improvements which removed the hard limit of 8 values in constant scalar arrays, but haven't done a deep dive. It does occur (as demonstrated below) in smaller value sets, so possibly it's been a latent issue that only just started happening on my larger enums.

Update: poking around a bit more, I observed that without in_array(..., strict: true), I do not receive the unreachable code error. However, the checked value is still not refined out of the variable.

Code snippet that reproduces the problem

https://phpstan.org/r/2af7eca1-73d9-4489-a107-897f77d04e44

https://phpstan.org/r/437803d5-e64c-4149-9715-8805052141eb

Note the code I discovered this with is more complex (more constants, other conditionals, etc), but this seems to be the simplest reproduce case. The updated snippet reveals the issue is specific to strict checks of array contents.

Expected output

No "unreachable statement" error; dumpTypes should have Dumped type: array<2|3>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions