PHPStorm Code Hinting for array of object arrays

倾然丶 夕夏残阳落幕 提交于 2019-12-22 04:16:33

问题


In PHPStorm, the Code Hinting for an object array is simple and awesome;

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

So if I do...

$fooList = new FooList();

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

PHPStorm understands that $fooList is an array of Foos, and therefore knows that $foo's type is Foo.

The problem is that I want an array of 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 :(
    }
}

I know that you can code hint manually inside the foreach, like...

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

Or ...

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

But I think that is ugly, as $listOfLists is build of Foo arrays, it should know what I am talking about without reminding it every time I implement a listOfLists.

Is there a way to implement this?


回答1:


Per the bug report linked in the comments by @LazyOne, as of PhpStorm EAP 138.256 (thus in PHPStorm 8) uniform multi-level array doc parsing is now supported.

This means you can now do this:

/**
 * @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();
    }
}

and get the expected:



来源:https://stackoverflow.com/questions/20643245/phpstorm-code-hinting-for-array-of-object-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!