phpstan: False positive always-true with `in_array`

I expect no error from the code https://phpstan.org/r/179a9da2-e9e7-4d4b-b4af-b0e01c35a9db

In my project issue I get

Call to function in_array() with arguments 0, array<int, 0|1>&nonEmpty and true will always evaluate to true.

But even if the array is non-empty, the in_array check isn’t supposed to be always true.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (13 by maintainers)

Commits related to this issue

Most upvoted comments

The issue can be simplified to https://phpstan.org/r/02705e4a-3f03-4fa4-83c1-9e1ec86a76f5

leads to $a typed with array<int, 0|1> and thats obviously contains always 0.

array<int, 0|1> doesn’t always contain 0 to me.

[1, 1, 1, 1] or [] are both example which are compatible to the array<int, 0|1> definition to me.

in_array() is a problematic function. Here’s where it’s implemented:

Ideally all of this should be handled in InArrayFunctionTypeSpecifyingExtension. It would also fix https://github.com/phpstan/phpstan-webmozart-assert/issues/142.