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
Generics and static type #3118
Comments
Note this is only a false-positive if the class that makes Otherwise this pattern can lead to runtime errors: https://3v4l.org/E78KQ |
Yes, and that should be de case for all enums and enum sets :) |
Yeah, just making a note for whoever tries to fix this :) |
Another, simplified reproducer: https://phpstan.org/r/0a7c0617-a38b-42f2-b1b1-d059e0a80715 |
I think this is the same problem, or at least related to the problem, I am having: |
@ComiR You're breaking the interface contract. Your code wuold only be valid if Hello was a final class (but PHPStan currently complains which is a bug but a different one). If you create |
Thanks @ondrejmirtes, I forgot to make the class Hello final in this example. The error stays the same: https://phpstan.org/r/19d1dd4a-1535-4ca5-9cd9-779743b92d5c |
Yeah, that's a bug, but you can work around it: https://phpstan.org/r/7e2056c0-7b2e-4cfe-bf95-3d445e542406 |
@amorimjuliana PHPStan now reports different result with your code snippet: @@ @@
-42: Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()
+PHP 7.4 – 8.0 (1 error)
+==========
+
+42: Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()
+
+PHP 7.1 – 7.3 (1 error)
+==========
+
+42: Return type CustomEnumSet of method CustomEnum::all() is not compatible with return type EnumSet of method Enum::all(). Full reportPHP 7.4 – 8.0 (1 error)
PHP 7.1 – 7.3 (1 error)
|
@Majkl578 After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-13: Method Test::foo() should return static(Test) but returns Test.
+No errors |
@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-No errors
+19: Return type (Hello) of method Hello::create() should be covariant with return type (static(Greeter)) of method Greeter::create() Full report
|
@ComiR After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-19: Return type (Hello) of method Hello::create() should be covariant with return type (static(Greeter)) of method Greeter::create()
+No errors |
@amorimjuliana After the latest commit in dev-master, PHPStan now reports different result with your code snippet: @@ @@
-42: Return type (CustomEnumSet) of method CustomEnum::all() should be compatible with return type (EnumSet<static(Enum)>) of method Enum::all()
+PHP 7.4 – 8.0
+==========
+
+No errors
+
+PHP 7.1 – 7.3 (1 error)
+==========
+
+42: Return type CustomEnumSet of method CustomEnum::all() is not compatible with return type EnumSet of method Enum::all(). Full reportPHP 7.4 – 8.0No errors PHP 7.1 – 7.3 (1 error)
|
Fixed by: phpstan/phpstan-src@d225a68 |
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
PHPStan complains about returning a subclass of a generic class that is compatible with the return type.
Code snippet that reproduces the problem
https://phpstan.org/r/df0e168c-f207-4db8-9d24-e1324e287652
Expected output
No errors.
The text was updated successfully, but these errors were encountered: