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

Template variables for iterable parameters not being inferred from Iterator or IteratorAggregate arguments

Open NotLoose opened this issue 5 months ago • 0 comments

Describe the bug

Template variables are not being correctly inferred when passing an Iterator<T> or IteratorAggregate<T> into an iterable<T> parameter.

To Reproduce

Create the following function:

/**
 * @template T
 *
 * @param iterable<T> $testInput
 *
 * @return T
 */
function templateTest(iterable $testInput): mixed
{
    return 'some value (pretend this is the correct type)';
}

And call this function with a variable of type Iterator or IteratorAggregate (ignore the actual value of the variable, it doesn't matter):

/** @var Iterator<string> */
$testInput = [];
$testOutput = templateTest($testInput);
/** @var IteratorAggregate<string> */
$testInput = [];
$testOutput = templateTest($testInput);

When you inspect the type of $testOutput it will be mixed.

Expected behavior

I expect the type of $testOutput to be correctly inferred from the input type (in the above examples it should be string).

This already works when passing an input value of type iterable or array:

/** @var iterable<string> */
$testInput = [];
$testOutput = templateTest($testInput);
/** @var array<string> */
$testInput = [];
$testOutput = templateTest($testInput);

In the above cases, the type of $testOutput is correctly inferred as string.

Screenshots

Screenshot 2024-09-16 at 1 31 10 PM

Screenshot 2024-09-16 at 1 32 21 PM

Platform and version

M2 Pro - macOS Sonoma 14.6 PHP Intelephense v1.12.6

NotLoose avatar Sep 16 '24 01:09 NotLoose