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

Invalid return type error for interface and class methods with return $this #2278

Closed
ADmad opened this issue Jul 8, 2019 · 10 comments
Closed

Comments

@ADmad
Copy link
Contributor

ADmad commented Jul 8, 2019

Bug report

Since phpstan 0.11.10 the code example below generates error like:

Method X\C::setFoo() should return $this(X\I) but returns $this(X\C)

Code snippet that reproduces the problem

<?php declare(strict_types = 1);
namespace X;

interface I
{
	/**
	 * @param string $p
	 * @return $this
	 */
	public function setFoo(string $p);
}

class C implements I
{
	/**
	 * @inheritDoc
	 */
	public function setFoo(string $p)
	{
		return $this;
	}
}

Expected output

No errors.

I am unable to reproduce the issue on https://phpstan.org. Is it using the latest phpstan version?

@mergeable
Copy link

mergeable bot commented Jul 8, 2019

This bug report is missing a link to reproduction on phpstan.org.
It will most likely be closed after manual review.

@ADmad ADmad changed the title Invalid return type error for interface and class methods which return $this Invalid return type error for interface and class methods with return $this Jul 8, 2019
@ondrejmirtes
Copy link
Member

This is fixed on master, not in 0.11.x. That's why you can't reproduce it. 0.11.10 added support for @inheritDoc. Before it didn't see the typehint at all.

@ADmad
Copy link
Contributor Author

ADmad commented Jul 8, 2019

Okay thanks. Would be a good idea to show the phpstan version number on PHPStan playground.

@ADmad
Copy link
Contributor Author

ADmad commented Jul 8, 2019

Will the issue be fixed in next 0.11.x release?

@ondrejmirtes
Copy link
Member

No, I've taken advantage of some refactoring to do the fix, that's why it's not in 0.11.x. Sorry.

@ADmad
Copy link
Contributor Author

ADmad commented Jul 8, 2019

That's quite unfortunate. This bug shows 34 invalid errors in our CakePHP codebase and now someone has to spend time ignoring them through phpstan.neon.

@ondrejmirtes
Copy link
Member

@ADmad OK, I will backport the fix, it might not be that hard. Please stay tuned, I'll do it ASAP.

@ondrejmirtes
Copy link
Member

Please test 0.11.12@dev (or something like that) if it works. It's commit 56b3eb2.

@ondrejmirtes ondrejmirtes reopened this Jul 8, 2019
@ADmad
Copy link
Contributor Author

ADmad commented Jul 8, 2019

It works! Thank you very much.

@ondrejmirtes
Copy link
Member

I just tagged 0.11.12 :)

@lock lock bot locked as resolved and limited conversation to collaborators Dec 23, 2019
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