如何使PhpStorm中的继承方法自动完成工作?

时间:2018-06-20 18:29:21

标签: autocomplete ide phpstorm fluent-interface

有两个类,定义如下:

class Foo
{
    private $aaa;
    public function setAaa(Aaa $aaa): self
    {
        $this->aaa = $aaa;
        return $this;
    }
}

class Bar extends Foo
{
    private $bbb;
    public function setBbb(Bbb $bbb): self
    {
        $this->bbb = $bbb;
        return $this;
    }
}

因此,这里使用“流利”设置器。但是PhpStorm似乎忽略了这一点并显示警告:

$bar = (new Bar())
    ->setAaa(new Aaa())
    ->setAaa(new Bbb())
;
  

在... \ Foo中找不到方法'setBbb'

autocomplete-for-inheritance

在这种情况下,是否可以使自动完成工作按预期进行?

1 个答案:

答案 0 :(得分:0)

首先-修复代码示例-使其真实而不是一些看起来像PHP的文本。

  • class Bar extends-扩展了什么?
  • 什么是setAaa()方法?
  • 什么是setBbb()方法?您的代码示例没有它。

无论如何...关于实际问题,在进行所有更改之后,看起来像真正的PHP代码...

使用PHPDoc并确保其显示@return $this。现在,它会将self部分中的: self解释为特定类(即Foo)...并且setPropertyBbb()Foo类中显然不可用。通过指定@return $this,可以使它在IDE眼中流利。

<?php

class Foo
{
    private $aaa;

    /**
     * My super method
     *
     * @param Aaa $aaa
     * @return $this
     */
    public function setPropertyAaa(Aaa $aaa): self
    {
        $this->aaa = $aaa;
        return $this;
    }
}

class Bar extends Foo 
{
    private $bbb;
    public function setPropertyBbb(Bbb $bbb): self
    {
        $this->bbb = $bbb;
        return $this;
    }
}

$bar = (new Bar())
    ->setPropertyAaa(new Aaa())
    ->setPropertyBbb(new Bbb())
;

enter image description here