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 * 
    }
  }

}
4

1 回答 1

4

您应该在函数之前使用 doc 类型的注释:

/**
 * @return \MyObject
 */
public function GetMyObject()
{
    return new MyObject();
}
于 2017-01-04T15:38:06.927 回答