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

Conditional while loop causes wrong assumption about array's contents #3314

Closed
gmta opened this issue May 19, 2020 · 6 comments
Closed

Conditional while loop causes wrong assumption about array's contents #3314

gmta opened this issue May 19, 2020 · 6 comments
Labels
Milestone

Comments

@gmta
Copy link

gmta commented May 19, 2020

Code snippet that reproduces the problem

https://phpstan.org/r/49a3d04d-8e39-4420-b0cd-66a0db7fa541
Old example: https://phpstan.org/r/0eededac-4378-4914-8928-ce5cb34cba85

Problem

The fact that the contents of the while block is never executed is not taken into account, leading PHPStan to incorrectly conclude that the first entry of the array is always present and has a constant value.

Still an issue as of PHPStan version 0.12.28.

@gmta gmta changed the title Conditional while loop and array type result incorrectly assume values Conditional while loop causes wrong assumption about array's contents May 19, 2020
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Nov 22, 2020
@phpstan-bot
Copy link
Contributor

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

@@ @@
+4: While loop condition is always false.
 7: Call to function in_array() with arguments '1', array<int, '1'> and true will always evaluate to true.
Full report
Line Error
4 While loop condition is always false.
7 Call to function in_array() with arguments '1', array<int, '1'> and true will always evaluate to true.

@gmta
Copy link
Author

gmta commented Sep 21, 2021

Apparently PHPStan now understands that while (false) is never executed, but this still produces the same issue:
https://phpstan.org/r/49a3d04d-8e39-4420-b0cd-66a0db7fa541

@phpstan-bot
Copy link
Contributor

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

@@ @@
-7: Call to function in_array() with arguments '1', array<int, '1'> and true will always evaluate to true.
+No errors

@phpstan-bot
Copy link
Contributor

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

@@ @@
-7: Call to function in_array() with arguments '1', array<int, '1'> and true will always evaluate to true.
+4: While loop condition is always false.
Full report
Line Error
4 While loop condition is always false.

@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

No branches or pull requests

3 participants