PHPStorm代码提示对象数组的数组

时间:2013-12-17 19:43:34

标签: php phpstorm code-hinting

在PHPStorm中,对象数组的代码提示简单而且棒极了;

class FooList {
    public function __construct(){
        $this->_fooList[] = new Foo(1);
        $this->_fooList[] = new Foo(2);
        $this->_fooList[] = new Foo(3);
        $this->_fooList[] = new Foo(4);
    }

    /**
     * @return Foo[]
     */
    getFoos() {
        return $this->_fooList;
    }
}

所以,如果我这样做......

$fooList = new FooList();

foreach($fooList as $foo)
{
    // Nice hinting.
    $foo->FooMethod...
}

PHPStorm了解$ fooList是一个Foos数组,因此知道$ foo的类型是Foo。

问题是我需要一个FooList数组。

$listOfLists[] = new FooList();
$listOfLists[] = new FooList();
$listOfLists[] = new FooList();
$listOfLists[] = new FooList();

foreach ($listOfLists as $fooList)
{
    foreach($fooList as $foo)
    {
        // No code hinting for $foo :(
    }
}

我知道您可以在foreach中手动编写提示,例如......

foreach ($listOfLists as $fooList)
{
    foreach($fooList as $foo)
    {
        /** $var $foo Foo */
        // Code hinting, yay!!
    }
}

或......

foreach ($listOfLists as $fooList)
{
    /** $var $fooList Foo[] */
    foreach($fooList as $foo)
    {
        // Code hinting, yay!!
    }
}

但我认为这是丑陋,因为$ listOfLists是Foo数组的构建,它应该知道我在说什么,而不是在每次实现listOfLists时都提醒它。

有没有办法实现这个?

1 个答案:

答案 0 :(得分:8)

根据bug report中链接的comments by @LazyOne,从PhpStorm EAP 138.256开始(因此在PHPStorm 8中)现在支持统一的多级数组文档解析。

这意味着您现在可以执行此操作:

/**
 * @var $listOfLists Foo[][]
 */
$listOfLists[] = (new FooList())->getFoos();
$listOfLists[] = (new FooList())->getFoos();
$listOfLists[] = (new FooList())->getFoos();
$listOfLists[] = (new FooList())->getFoos();

foreach ($listOfLists as $fooList)
{
    foreach($fooList as $foo)
    {
        // Code hinting, yay!!
        $foo->fooMethod();
    }
}

并获得预期:

Screenshot

相关问题