Skip to content

No templated type inferred when many options given #2576

@muglug

Description

@muglug

Code snippet that reproduces the problem

/**
 * @template TReturn
 * @param callable():(\Generator<mixed, mixed, mixed, TReturn>|TReturn) $gen
 * @return array<int, TReturn>
 */
function call(callable $gen) : array {
    $return = $gen();
    if ($return instanceof Generator) {
        return [$return->getReturn()];
    }
    /** @var array<int, TReturn> */
    $wrapped_gen = [$gen];
    return $wrapped_gen;
}

$arr = call(
    /**
     * @return Generator<mixed, mixed, mixed, string>
     */
    function() {
        yield 1;
        return "hello";
    }
);

foreach ($arr as $a) {
    $a->foo();
}

https://phpstan.org/r/4fe92bd5-514f-42ba-ade6-8cf392b61ad1

Expected output

Cannot call method on string variable $a

Actual output

No issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions