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
PHPStan thinks loop element can be false when looping custom Iterator implementation #5817
Comments
Well, as you document |
Following your logic, this simple ArrayIterator on int[] should actually be Iterator<int, int|null> Maybe someone can enlighten me regarding:
I'm not trying to blame anyone here; it just seems very unclear to me. |
@InvisibleSmiley Adding |
Umm, I have exactly that in line 20 of my initial example ;) Or am I getting you wrong? |
It seems to me, that we either need to have a way to hint the "empty" type of Anyway, I think there is no way to implement a default behavior for this, because of the inability to determine what the "empty" return type of |
Related #3763 |
Is there something I could help with here? Would a failing test help (I don't think so as there is already a replicable case in phpstan.org)? In my case I use both current() as well as iterating, so at least one of them will be incorrect with the current implementation. Either the current will be missing the potential null or the iteration will need to have additional not null assertion somewhere in the code. I understand the logic @hrach is articulating in #3763 (comment), but unfortunately it's not a matter of principle, but rather a matter of being able to describe a code that actually exists and cannot be easily changed. |
The problem with "nullable"/"falsable" return type of But I realized it's unambiguous when |
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. |
Bug report
I have a problem with a custom class implementing Iterator, specifically Iterator::current using PHP built-in function current() on an internal array. The current() function may return an array item or false:
PHPStan thinks foreach loop items may be false when in reality that cannot happen.
Not sure whether this stems from the Iterator::current method implementation though, because I also have "false" in annotations of methods next() (using function next()) and rewind() (using function reset()).
Code snippet that reproduces the problem
https://phpstan.org/r/2fefbc8d-c1f2-4ee8-bc04-59a3ef0be3cd
Note that two more errors are displayed in strict mode but I think these are wrong, too.
The text was updated successfully, but these errors were encountered: