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
Function f() should return T of A but returns A #6609
Comments
this works though. |
I'm surprised it doesn't work because these methods are already marked as returning |
@javaDeveloperKid After the latest commit in 1.7.x, PHPStan now reports different result with your code snippet: @@ @@
-15: Function modify() should return T of DateTime|DateTimeImmutable but returns DateTime|DateTimeImmutable.
+12: Function modify() never returns DateTimeImmutable so it can be removed from the return type. Full report
|
FYI @VincentLanglet we broke something :) |
Yes, but I dunno why. I did
But IMHO this code is not recommended
let believe that it returns a new instance of DateTime which is not. this https://phpstan.org/r/5e05cad2-18d4-4b6d-8cdf-8f54c4ae05ed might be better. |
Fixed: phpstan/phpstan-src@b5da7ce |
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
PHP version: 7.2.
As I need to support both
\DateTime
and\DateTimeImmutable
, at first I used@template T of \DateTimeInterface
but methodmodify
does not exist on this interface so I moved to@template T of \DateTime|\DateTimeImmutable
.I'm not sure if
@template
supports union types. I couldn't determine this from docs but if it doesn't then maybe PHPStan should throw when such a notation is used?Code snippet that reproduces the problem
https://phpstan.org/r/5a4f44a1-4266-4c04-a0db-30674b25053a
Expected output
No error.
Did PHPStan help you today? Did it make you happy in any way?
Yes, it did.
The text was updated successfully, but these errors were encountered: