Search code examples
phpstormphpstorm-2016.1

PhpStorm type recognition/suggestions


Is there anything I can do to get PhpStorm (2016.1) to recognize types outside the "new X()" scope?

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 - no suggestions:

class UserClass
{
  public function DoMagic()
  {

    $dummy2 = new DummyClass();

    $params = $dummy2->GetParamsList(); 

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

}
?>

I found adding this hack works, but it's getting frustrating to employ it:

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

So the full working example would be:

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

    $params = $dummy->GetParamsList(); 

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

      $param-> * suggestions pop up * 
    }
  }

}

Solution

  • You should use doc-type comments before your function:

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