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

Foreaching an Iterable<Key, NonNullType> never results in null #3763

Closed
dktapps opened this issue Aug 22, 2020 · 7 comments
Closed

Foreaching an Iterable<Key, NonNullType> never results in null #3763

dktapps opened this issue Aug 22, 2020 · 7 comments
Labels

Comments

@dktapps
Copy link
Contributor

dktapps commented Aug 22, 2020

Bug report

Code snippet that reproduces the problem

https://phpstan.org/r/807ccb92-293e-4443-9a37-90d210e75a89 (execution: https://3v4l.org/SWnXd)

Expected output

Zero errors.

@dktapps dktapps changed the title Foreaching an Iterable never results in null Foreaching an Iterable<Key, NonNullType> never results in null Aug 22, 2020
@dktapps
Copy link
Contributor Author

dktapps commented Aug 22, 2020

It seems like the foreach type is being inferred from the return type of current() instead of the generic TValue. Since PHPStan doesn't disallow having a non-covariant NamedTag|null return type on an Iterator<int, NamedTag>, I'm not entirely sure where the bug lies -- either null should be allowed and PHPStan should ignore it, or null should be disallowed by covariance rules.

@dktapps
Copy link
Contributor Author

dktapps commented Aug 22, 2020

In line with PHP's own iterators, it's my belief that Iterator->current() should be allowed to return NULL without affecting the iterator's overall type, as per this example: https://3v4l.org/3Flfs

@hrach
Copy link
Contributor

hrach commented Oct 10, 2020

This is IMO not a bug, this is something PHPStan should check. There is no official documentation how current should behave and the internal behaves the way it returns a null in invalid state, but the general logic approach here is to throw (InvalidStateException) in invalid state. Ie. current should return only valid values, otherwise a check via valid() call is needed.

@phpstan-bot
Copy link
Contributor

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

@@ @@
+17: Method Iterable1::current() should return NamedTag|null but returns NamedTag|false.
 49: Cannot call method getValue() on NamedTag|null.
Full report
Line Error
17 Method Iterable1::current() should return NamedTag
49 Cannot call method getValue() on NamedTag

@phpstan-bot
Copy link
Contributor

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

@@ @@
-49: Cannot call method getValue() on NamedTag|null.
+17: Method Iterable1::current() should return NamedTag|null but returns NamedTag|false.
Full report
Line Error
17 `Method Iterable1::current() should return NamedTag

@ondrejmirtes
Copy link
Member

I think this works as expected now.

@github-actions
Copy link

github-actions bot commented Mar 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 Mar 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

4 participants