I used PhpStorm to auto add the return type in the function below:
/**
* @return \Generator|null
*/
function yieldTest(): ?\Generator
{
yield from [1, 2, 3];
}
My question: Why does it add the null
option alongside \Generator
?
I can't see a way for this to return null so I'm wondering if I'm overlooking something in the way yield
works or if this is a quirk from PhpStorm's side and can safely be ditched?
Update:
To clarify - I am asking why PHPStorm generated the return type as ?\Generator
.
I understand that it then added null
to the php doc @return
tag because on the ?
.
Update 2:
Here is the full code form a test file before generating extra bits:
class yieldTestClass
{
public function yieldTest()
{
yield from [1, 2, 3];
}
}
And here is the code after choosing "Declare the return type" from the context menu on the method name:
class yieldTestClass
{
public function yieldTest(): ?\Generator
{
yield from [1, 2, 3];
}
}
I am not sure if this is a native feature in the IDE or from a plugin but if it is a plugin I am guessing it would be this https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-
Same question either way though.