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

Type inference after using array_pop could be improved. #7804

Open
mad-briller opened this issue Aug 16, 2022 · 4 comments
Open

Type inference after using array_pop could be improved. #7804

mad-briller opened this issue Aug 16, 2022 · 4 comments
Labels

Comments

@mad-briller
Copy link
Contributor

Feature request

In the below case, using array_pop makes phpstan believe the $headers value could be an empty array, when prior it knew that there were atleast 4 values.
array_pop always removes one element from the array and as such the second count() value could return int<3, max> in this case, and also persist the non-empty-array type.

https://phpstan.org/r/5003d274-3690-464e-a5e1-6e898cd00411

@herndlm
Copy link
Contributor

herndlm commented Aug 16, 2022

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Aug 16, 2022
@thg2k
Copy link

thg2k commented Aug 16, 2022

Interestingly, if you dump the type of the array before dumping the type of the count, it changes to int<1, max>.

https://phpstan.org/r/892c0769-7920-42cf-9354-69c91b915ef1

Could this be a signal for some other more serious bug? @ondrejmirtes

@mad-briller
Copy link
Contributor Author

@thg2k i also stumbled across that while debugging and made a separate issue #7803

@phpstan-bot
Copy link
Contributor

@thg2k After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:

@@ @@
  7: Dumped type: int<4, max>
  8: Dumped type: non-empty-array<int, string>
- 9: Dumped type: int<1, max>
+ 9: Dumped type: int<4, max>
 11: Dumped type: int<0, max>
Full report
Line Error
7 Dumped type: int<4, max>
8 Dumped type: non-empty-array<int, string>
9 Dumped type: int<4, max>
11 Dumped type: int<0, max>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants