Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NotLoose opened this issue Sep 16, 2024 · 0 comments
Labels

Comments

@NotLoose
Copy link

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

@bmewburn bmewburn added the bug label Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants