Vscode-intelephense: Array type hint bug

Created on 19 Sep 2019  路  2Comments  路  Source: bmewburn/vscode-intelephense

I don't know why, my vscode only hint while foreach for the array hint
The array typehint defines:

    /**
     * Basic trustocean products
     *
     * @return \Illuminate\Database\Eloquent\Collection|\App\Models\Product[]
     */
    function basic_trustocean_products() {}

Tests
image
as the screenshot shows, it works while foreach.

foreach (basic_trustocean_products() as $product) {
    $product->name; // here it can hint
}

image

But does not work while array index.

$products = basic_trustocean_products();
$products[0]->name; // it can't hint
bug

Most helpful comment

Fixed in 1.3. Also, any class that is an instance of ArrayAccess will also be able to be typed as MyClass<TKey, TValue> in 1.3. So in the original example:

/**
     * Basic trustocean products
     *
     * @return \Illuminate\Database\Eloquent\Collection<int, \App\Models\Product>
     */
    function basic_trustocean_products() {}

All 2 comments

~I can't reproduce with the below snippet. I wonder if there is some other assignment to $products above line 110 that could be causing it to fail?~

Seems it is the collection class that causes problems. Works with just Product[].

<?php

class Collection implements ArrayAccess
{

}

class Product
{
    public $name;
}

/** @return Collection|Product[] */
function basic_trustocean_products()
{
    return [new Product];
}

$products = basic_trustocean_products();
$products[0]->

Fixed in 1.3. Also, any class that is an instance of ArrayAccess will also be able to be typed as MyClass<TKey, TValue> in 1.3. So in the original example:

/**
     * Basic trustocean products
     *
     * @return \Illuminate\Database\Eloquent\Collection<int, \App\Models\Product>
     */
    function basic_trustocean_products() {}
Was this page helpful?
0 / 5 - 0 ratings