Skip to content
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

Closed
javaDeveloperKid opened this issue Feb 8, 2022 · 7 comments
Closed

Function f() should return T of A but returns A #6609

javaDeveloperKid opened this issue Feb 8, 2022 · 7 comments
Labels
Milestone

Comments

@javaDeveloperKid
Copy link

javaDeveloperKid commented Feb 8, 2022

Bug report

PHP version: 7.2.

As I need to support both \DateTime and \DateTimeImmutable, at first I used @template T of \DateTimeInterface but method modify 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.

@Ahummeling
Copy link

this works though.
Phpstan doesn't know that DateTime::modify returns the an instance of itself. That's why you get this error message.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Feb 10, 2022
@ondrejmirtes
Copy link
Member

I'm surprised it doesn't work because these methods are already marked as returning static in functionMap. Needs more investigation.

@phpstan-bot
Copy link
Contributor

@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
Line Error
12 Function modify() never returns DateTimeImmutable so it can be removed from the return type.

@ondrejmirtes
Copy link
Member

FYI @VincentLanglet we broke something :)

@VincentLanglet
Copy link
Contributor

FYI @VincentLanglet we broke something :)

Yes, but I dunno why. I did

return new StaticType($methodReflection->getDeclaringClass());

But IMHO this code is not recommended

$date = $date->modify('+1 day');

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.
and it gives a different error message.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@b5da7ce

@github-actions
Copy link

github-actions bot commented Jun 5, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants