vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Nested @template are not resolved

Open dtdesign opened this issue 1 year ago • 0 comments

Describe the bug Intelephense is unable to narrow down the type of a nested @template annotation. The example below illustrates how Intelephense reports the object in line 58 to be of type DataObject and thus complains about the function call in line 59 that expects ExampleDataObject.

PHPStan has been verified to narrow down the type correctly: https://phpstan.org/r/22cac7bf-a8b5-4689-9ebd-7d3e4837f0fb

To Reproduce

<?php

interface DataObject {}

/**
 * @template T of DataObject
 */
abstract class DataObjectDecorator
{
    public function __construct(
        /** @var T */
        public readonly DataObject $object,
    ) {}
}

/**
 * @template TDataObject of DataObject
 * @template TDataObjectDecorator of DataObjectDecorator<TDataObject>
 */
abstract class Action
{
    public function __construct(
        /** @var TDataObjectDecorator[] */
        protected readonly array $objects
    ) {}

    /**
     * @return TDataObjectDecorator[]
     */
    public function getObjects(): array
    {
        return $this->objects;
    }
}

class ExampleDataObject implements DataObject {}

/**
 * @extends DataObjectDecorator<ExampleDataObject>
 */
class ExampleDataObjectDecorator extends DataObjectDecorator {}

/**
 * @extends Action<ExampleDataObject, ExampleDataObjectDecorator> 
 */
class ExampleList extends Action {}

$decorator = new ExampleDataObjectDecorator(new ExampleDataObject);
\PHPStan\dumpType($decorator->object);

function expectsExample(ExampleDataObject $object): void
{
    // Do something fancy.
}

$objects = (new ExampleList([$decorator]))->getObjects();
foreach ($objects as $object) {
    \PHPStan\dumpType($object->object);
    expectsExample($object->object);
}

Expected behavior The type of $object->object (or $decorator->object) in the example above should be resolved to be of type ExampleDataObject.

Platform and version macOS 15.3.1 (aarch64) Intelephense 1.14.3 (pre-release)

dtdesign avatar Mar 08 '25 14:03 dtdesign