How to fix code completion using DocBlock type hints with Intelephense?

327 Views Asked by At

I'm trying to get better completion with the VSCode extension Intelephense and some things aren't making sense. To start here is some example code:

<?php
declare(strict_types=1);

// "square/square": "30.0.0.20230816"
use Square\SquareClient;
use Square\Models\CatalogObject;
use Square\Models\CatalogItem;

require 'vendor/autoload.php';

$client = new SquareClient([
    'accessToken' => $_ENV['SQUARE_ACCESS_TOKEN'],
    'environment' => 'sandbox'
]);

$res = $client->getCatalogApi()->listCatalog();

/* @var CatalogObject[] $catalog */
$catalog = $res->getResult()->getObjects();

if ($catalog) {

    /* @var CatalogObject $item */
    foreach ($catalog as $item) {

        /* @var CatalogItem $data */
        $data = $item->getItemData();

    }
}

I get completion on $client->getCatalogApi() after scrolling past all global symbols & methods. I'd love to remove them because $client->$_COOKIE just doesn't seem helpful.

Second and more importantly I get why the type can't be determined for $catalog, $item and $data but it appears like hinting as I've done with @var <Type> $variable is supported by Intelephense but it's having no affect with Intelephense 1.9.5 (I've re-indexed). What I'm expecting is if I type for example $data->getItem one of the choices will be $data->getItemData(). Thank you!

VSCode 1.81.1.
Intelephense 1.9.5 (paid).
PHP 8.2.

1

There are 1 best solutions below

0
JSP On

So the answer to this question for me was a result of two issues. The first was code completion being "polluted" with a ton of items from the global namespace so even in situations where the completion had the right answer I had to scroll past all the globals to get to it. Here's an example:

example of code completion returning global items

To get rid of the globals you just have to disable VSCodes built-in PHP support:

To disable the built-in PHP smart completions in favor of suggestions from an installed PHP extension, uncheck PHP > Suggest: Basic, which sets php.suggest.basic to false in your settings.json file.

The other issue I was bumping into is PHP Intelephense does not appear to support PHPDoc type hints (v1.9.5). To fix this I just installed the PHPActor LSP along with the VSCode extension which does understand the type hints.