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
Incorrect assumption with static and inheritance #6621
Comments
Hi, you have multiple The right solution here is to make your parent classes generic: https://phpstan.org/r/ddf12918-9b8a-45d2-b825-9fe128085580 |
Your solution leads to two new errors though:
|
Yes, because I don't know how your implementation actually looks like and how to fix it. With this code these iterators are basically infinite. |
My code does look about like that, just with more methods and functionality - both the base classes and the specific classes are used. Why would they be infinite? Either the only guarantee is that you get an object (BaseEntries, BaseIterator), or otherwise you get a specific type (Entries, SpecificIterator). |
You'd be able to get rid of the errors by having a constructor in the class which isn't unrealistic. PHPStan needs some way to know that you'll really provide Generics want you to provide as specific type as possible. More info here: https://phpstan.org/blog/generics-in-php-using-phpdocs |
I guess I will try it, thanks for the feedback. A pity though that you do not recognize multiple |
You can: https://phpstan.org/user-guide/stub-files
This is probably what changed:
So the generics were always read in a different order, but your problem was hidden thanks to the typehints in TBH I consider having |
Why undefined behavior though? To me this is like any covariance type according to LSP, in that narrowing the type in a subclass from |
Yeah but when |
With |
But it's not always covariance. You can also have |
But not with just |
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 think PHPStan does not correctly process the return type "static" in some circumstances. This bug appeared in v1.4.3 and still occurs with the latest version, v1.4.6:
Code snippet that reproduces the problem
The problem only occurs when using inheritance and if a base class returns "static" (as far as I can tell), this is an example (as simple as I managed to get it to, similar to how it was generated in my code):
https://phpstan.org/r/ce9ee7ac-0301-49fa-9c33-80f68e9d8a64
Expected output
There should be no errors, as PHPStan knows
$entry
has the typeAccount
, notobject
. With PHPStan v1.4.2 and before it worked as expected.Did PHPStan help you today? Did it make you happy in any way?
Thanks for your work, I do appreciate PHPStan and how it improves code quality!
The text was updated successfully, but these errors were encountered: