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
Allow narrowing of type in PHPDoc #482
Comments
@Jean85 I have trouble understanding your problem. Can you reproduce it on https://phpstan.org/? |
Sorry, I had wrongly narrowed down my problem. I've edited my starting example with something that now gets you the error that I wanted to report. |
The problem is here: return array_filter($this->getAnimals(), function ($val) {
return $val instanceof Bird;
}); Because |
And about the original form of your question (which I retrieved from my email inbox): interface AnimalCage
{
public function getAnimal(): Animal;
}
class BirdCage implements AnimalCage
{
/**
* @return Bird
*/
public function getAnimal(): Animal
{
// ...
}
} This is already supported and in case of Calling BirdCage::getAnimal(), PHPStan will assume that the return type is |
@Jean85 Or use foreach with if. It's a lot faster and a lot easier to statically analyse https://phpstan.org/r/6ef813feadeb02536965559f5d1e59d7 |
Yeah great! Thanks @JanTvrdik @ondrejmirtes I imagined that the issue whas somethat there while re-writing the example, thanks. |
@JanTvrdik #476 got merged, can you send your PR for |
Done, PR merged. Thanks to all! |
Take this code as an example
The PHPDoc over
getBirds()
is marked as an error by PHPStan, because it's somehow expecting@return Animal
:I would like to restrict the type and not be forced to use the
@return Animal[] | Bird[]
workaround to avoid errors with PHPStan.The text was updated successfully, but these errors were encountered: