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

False array type specification adding foreach #7144

Closed
rajyan opened this issue Apr 28, 2022 · 2 comments · Fixed by phpstan/phpstan-src#1308
Closed

False array type specification adding foreach #7144

rajyan opened this issue Apr 28, 2022 · 2 comments · Fixed by phpstan/phpstan-src#1308
Labels

Comments

@rajyan
Copy link
Contributor

rajyan commented Apr 28, 2022

Bug report

Another related issue for #7000
Adding foreach make a wrong assumption on array type.

Code snippet that reproduces the problem

https://phpstan.org/r/f2f27d58-21fa-480c-a284-d70777f19926

The problem is described in test3. The addition of key in line 30 (compared to test1) and addition of foreach in line 32 (compared to test2) makes the type of $arr as array<'bar'|'baz'|'foo', array<string>> which I think is not correct.

Expected output

Addition of foreach should not change the array type.
Also, it should report on line 32 for non existing offset access.

Did PHPStan help you today? Did it make you happy in any way?

Yes! 😄

@rajyan rajyan changed the title False positive array type specification using foreach False array type specification adding foreach Apr 28, 2022
@phpstan-bot
Copy link
Contributor

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

@@ @@
 10: Dumped type: array{foo?: array<string>, bar?: array<string>}
 11: Offset 'bar'|'foo' does not exist on array{foo?: array<string>, bar?: array<string>}.
 21: Dumped type: array{foo?: array<string>, bar?: array<string>}
-31: Dumped type: array<'bar'|'baz'|'foo', array<string>>
+31: Dumped type: array{foo?: array<string>, bar?: array<string>}
+32: Offset 'bar'|'baz'|'foo' does not exist on array{foo?: array<string>, bar?: array<string>}.
Full report
Line Error
10 Dumped type: array{foo?: array<string>, bar?: array<string>}
11 `Offset 'bar'
21 Dumped type: array{foo?: array<string>, bar?: array<string>}
31 Dumped type: array{foo?: array<string>, bar?: array<string>}
32 `Offset 'bar'

@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 Jun 14, 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