PhpStorm类型识别/建议

时间:2017-01-04 15:31:23

标签: phpstorm phpstorm-2016.1

我能做些什么来让PhpStorm(2016.1)识别"new X()"范围之外的类型吗?

SomeClass.php:

class SomeClass
{    
  public function DoMagic()
  {
    echo "doing magic";
  }

}

DummyClass.php:

class DummyClass
{
  protected $mParamsList;

  function __construct()
  {
    $this->mParamsList = array();
  }

  public function InitParamsList()
  {
    $this->mParamsList[] = new SomeClass();
  }

  public function GetParamsList()
  {
    return $this->mParamsList;
  }

}

UserClass.php - 没有建议:

class UserClass
{
  public function DoMagic()
  {

    $dummy2 = new DummyClass();

    $params = $dummy2->GetParamsList(); 

    foreach ($params as $param)
    {
      $param-> * nothing happens *
    }
  }

}
?>

我发现添加此hack有效,但使用它会让人感到沮丧:

if(false)   {     $ param = new SomeClass();   }

所以完整的工作示例是:

class UserClass
{
  public function DoMagic()
  {
    $dummy = new DummyClass();

    $params = $dummy->GetParamsList(); 

    foreach ($params as $param)
    {
      if (false)
      {
        $param = new SomeClass();
      }

      $param-> * suggestions pop up * 
    }
  }

}

1 个答案:

答案 0 :(得分:4)

您应该在功能之前使用doc-type注释:

/**
 * @return \MyObject
 */
public function GetMyObject()
{
    return new MyObject();
}