-
Notifications
You must be signed in to change notification settings - Fork 7.8k
"iterable" alias "array|Traversable" breaks PHP 8.1 code #9556
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
Comments
Huh, I will see if I can do an exception for the compile time redundancy check but not sure that this is possible. |
So I've had a look, and the problem is that the redundancy check is one of the last ones and doesn't have the knowledge of what the class name is, just that there is at least one class name within the type union. In the current state ttrying to allow I'll think a bit more, but by of the looks of it, it seems unlikely I can make this case an exception. |
I did some tests and it seems to be safe to replace So it seems to be easy to fix when mentioned in the "Backward Incompatible Changes". |
FYI |
Yes, that's what I expected, but I wanted to test it to be sure. :) |
To be "sure", you should test not only return types (covariant) but also parameter types (contravariant): https://3v4l.org/NQLV5 confirms the equivalence between |
Contravariance is done by doing a covariant check just with the types reversed, so testing just one is fine, added an invariant test in #9558 |
Description
I just recognized a problem in a project when testing it with PHP 8.2RC2 (API Platform 3.0).
There is an interface that contains a method with the return type definition "object|iterable|null". In PHP 8.2 "iterable" becomes "array|Traversable" and so the return type definition results in "object|array|Traversable|null", which now contains the redundant "Traversable" type (although "object" is already contained) and leads to a fatal error.
Eample...
The following code:
Resulted in this output:
But I expected this output instead:
As a workaround one could use "object|array" instead of "object|iterable", but best would be if the change in PHP 8.2 handles this special case, to avoid this break... Otherwise this should be listed in "Backward Incompatible Changes" instead of "Other Changes".
PHP Version
PHP 8.2.0RC2
Operating System
No response
The text was updated successfully, but these errors were encountered: