PhpStorm type hinting array of objects fails after testing with is

I am having trouble getting Phpstorm type hints working on arrays of objects when I test the array with is_array() before iterating over the array.

Can anyone shed some light on why is_array() removes the type hinting of the objects?

$test = new Test();
$testers = $test->getAll();

// Type hinting WORKS for $test
foreach($testers as $test) {
    $test->getId(); // Type hinting works
}

// Type hinting does NOT work for $test
if(is_array($testers)) {
    foreach($testers as $test) {
        $test->getId(); // Type hinting does NOT work
    }
}

I can get type hinting to work if I add type hint comment where the array is retrieved, but this is undesirable, as I would prefer Phpstorm to use the object method return type hint info instead of having to specify it all the time:

Adding this works even when is_array is present /** @var Tester[] $testers */ $testers = $test->getAll();

Test class code in case anyone wants to run the example:

class Tester {
    private $id;
    private $name;

    public function __construct($id, $name) {
        $this->id = $id;
        $this->name = $name;
    }

    /**
     * @return Integer
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @return String
     */
    public function getName() {
        return $this->name;
    }

}

class Test {
    /**
     * @return tester[]
     */
    public function getAll() {
        $testers = array(
            new Tester(1, 'Bob'),
            new Tester(2, 'Jane'),
            new Tester(3, 'Tim')
        );
        return $testers;
    }
}
链接地址: http://www.djcxy.com/p/58624.html

上一篇: 我怎样才能创建一个PHPDoc魔术属性的类定义之外?

下一篇: 使用is进行测试后,PhpStorm类型提示对象阵列失败