douyun1860 2019-03-14 09:22
浏览 158
已采纳

为什么PhpStorm允许使用yield函数的null返回类型?

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.

  • 写回答

2条回答 默认 最新

  • dongliping003116 2019-03-14 11:12
    关注

    That inspection and intended fix (the code added) is provided by a Php Inspections (EA Extended) plugin.

    Please report to a plugin author: https://github.com/kalessil/phpinspectionsea/issues

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?