Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_array with array of literals is always true when it is possible to contain the checked value. #5668

Closed
BackEndTea opened this issue Sep 22, 2021 · 4 comments
Labels
Milestone

Comments

@BackEndTea
Copy link
Contributor

BackEndTea commented Sep 22, 2021

Bug report

When doing an in_array check on an array with string literals, it is always true if the checked string matches one of the literals.

In both has1, has2 and has3 the type is not correct, as the array may be empty, or contain another value.

Code snippet that reproduces the problem

https://phpstan.org/r/2d2fb268-4ecd-4255-b8a2-312a0c7cbfaf

Expected output

Dumped types in order:

  • bool
  • bool
  • bool
  • true (this one is correct already)
@phpstan-bot
Copy link
Contributor

@BackEndTea After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
- 8: Dumped type: true
-16: Dumped type: true
-24: Dumped type: true
-33: Dumped type: true
+No errors

@phpstan-bot
Copy link
Contributor

@BackEndTea After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
- 8: Dumped type: true
-16: Dumped type: true
-24: Dumped type: true
+ 8: Dumped type: bool
+16: Dumped type: bool
+24: Dumped type: bool
 33: Dumped type: true
Full report
Line Error
8 Dumped type: bool
16 Dumped type: bool
24 Dumped type: bool
33 Dumped type: true

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@4321374

@github-actions
Copy link

github-actions bot commented Apr 3, 2022

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants