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

Inside foreach, $value is not the same as $array[$key] #6000

Closed
Seldaek opened this issue Nov 13, 2021 · 4 comments · Fixed by phpstan/phpstan-src#1504
Closed

Inside foreach, $value is not the same as $array[$key] #6000

Seldaek opened this issue Nov 13, 2021 · 4 comments · Fixed by phpstan/phpstan-src#1504

Comments

@Seldaek
Copy link
Contributor

Seldaek commented Nov 13, 2021

Bug report

Even though I narrow down which $key is currently being looped over, $value still has an aggregate type of all possible keys, and does not update to become the stricter type associated with just that exact key. Anyway the snippet probably explains this better :D

It may be a feature request more than a bug I suppose.

Code snippet that reproduces the problem

https://phpstan.org/r/456203ce-8c04-406f-b7fb-556ac8d711ca

Expected output

No error

@phpstan-bot
Copy link
Contributor

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

@@ @@
-8: Parameter #2 $array of function implode expects array<string>, array<int|string, array<string>|string> given.
+9: Offset 'classmap' does not exist on array{psr-4?: array<string, array<string>|string>, classmap?: array<int, string>}.
Full report
Line Error
9 `Offset 'classmap' does not exist on array{psr-4?: array<string, array

@Seldaek
Copy link
Contributor Author

Seldaek commented Jul 12, 2022

@herndlm - I am not sure this is correct - I mean it's an improvement in that $value is now correct based on the $key check - but $data[$key] should IMO also be marked as existing within a foreach creating $key https://phpstan.org/r/47a8b19c-e77b-44b4-9272-b1eedf7b046b

@herndlm
Copy link
Contributor

herndlm commented Jul 12, 2022

Oh, that's a bit of a weird one. Adding some internal info for @ondrejmirtes mostly and also fyi I guess :)

this raises 3 questions for me

  • should we open a new feature / issue for the foreach looping key narrow thing?
  • why are intersections ignored in TypeUtils::getOldConstantArrays, shouldn't we consider them there too? I guess there's a good reason though why they are not considered there and in many other of those util methods
  • is that accessory type for a constant array something that should be normalized away maybe? it feels weird at least, doesn't add more info IMO and seems to be actively confusing some checks UPDATE: it still does add value I guess though

@github-actions
Copy link

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 Aug 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants