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

Return type detection using static issue "...should return static(Foo) but returns Foo" #10286

Closed
eclipxe13 opened this issue Dec 12, 2023 · 2 comments

Comments

@eclipxe13
Copy link

Bug report

Accordint to PHPStan doc https://phpstan.org/writing-php-code/phpdocs-basics Using @return static along with the self native typehint means that the method returns a child class

As of 1.10.48 (not present on 1.10.47) return types at level 3 report an error like:
Method Foo::create() should return static(Foo) but returns Foo. using this example:

interface IFoo
{
	/** @return static */
	public static function create();
}

class Foo implements IFoo
{
	public static function create(): self
	{
		return new self();
	}
}

I don't think this is an error.
If it is an error in the code, how it could be solved?
If it is not an error, then it is a bug?

Thanks for PHPStan, it is geat!

Code snippet that reproduces the problem

https://phpstan.org/r/2eb3158b-0c10-4138-8e91-0daf17390e77

Expected output

The issue is reported incorrectly.

Did PHPStan help you today? Did it make you happy in any way?

PHPStan is a great tool, you are helping us (PhpCfdi) to maintain libre high-quality tools for Mexican PHP developers. Thanks a lot!

@eclipxe13 eclipxe13 changed the title Return type Return type detection using static issue "...should return static(Foo) but returns Foo" Dec 12, 2023
@ondrejmirtes
Copy link
Member

@return static is the same as native static return type: https://wiki.php.net/rfc/static_return_type

Your code fails in this scenario: https://3v4l.org/n36OO

PHPStan 1.10.48 contains a bugfix that makes the @return static PHPDoc to be implicitly inherited in your code: phpstan/phpstan-src#2803

Some solutions:

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 Jan 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants