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

Problem with @return static #3523

Closed
MidnightDesign opened this issue Jun 22, 2020 · 8 comments
Closed

Problem with @return static #3523

MidnightDesign opened this issue Jun 22, 2020 · 8 comments
Labels
Milestone

Comments

@MidnightDesign
Copy link
Contributor

See https://phpstan.org/r/98a3d03f-add8-407f-ac45-69ac58dbba7e

<?php declare(strict_types = 1);

interface FooInterface
{
    /**
     * @return static
     */
    public function deserialize();
}

final class Foo implements FooInterface
{
    public function deserialize(): self
    {
        return new self();
    }
}

Expected behavior:
No errors

Actual behavior:
I get the error

Return type (Foo) of method Foo::deserialize() should be covariant with return type (static(FooInterface)) of method FooInterface::deserialize()

@ondrejmirtes
Copy link
Member

For some reason the PHPDoc isn't inherited but should be: https://phpstan.org/r/d965bee2-214a-494f-b2ea-1fb3fe973443

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan-strict-rules Jun 22, 2020
@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jun 22, 2020
@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-18: Method Foo::deserialize() should return static(Foo) but returns Foo.
+16: Return type (Foo) of method Foo::deserialize() should be covariant with return type (static(FooInterface)) of method FooInterface::deserialize()
Full report
Line Error
16 Return type (Foo) of method Foo::deserialize() should be covariant with return type (static(FooInterface)) of method FooInterface::deserialize()

@MidnightDesign
Copy link
Contributor Author

@ondrejmirtes Are you sure this is fixed? I would expect my playground snippet to not have any errors, but it still does.

@ondrejmirtes
Copy link
Member

Your playground no longer has errors: https://phpstan.org/r/22531cbe-e58b-406d-b4d3-e04abcaeb545 (the bot does not comment on closed issues)

@MidnightDesign
Copy link
Contributor Author

The original one does: https://phpstan.org/r/98a3d03f-add8-407f-ac45-69ac58dbba7e (I would expect the @return static to be inherited from FooInterface::deserialize())

@ondrejmirtes
Copy link
Member

@MidnightDesign There's a big button that says "This result is out of date. Show the latest result".

@MidnightDesign
Copy link
Contributor Author

Wasn't big enough for my shitty eyes. I'm sorry and thanks for the quick response!

@github-actions
Copy link

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 Apr 29, 2021
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

3 participants