Skip to content

Template type is not narrowed down in conditional return types #10622

@canvural

Description

@canvural

Bug report

Hello 👋🏽

I have the following snippet:

<?php declare(strict_types = 1);

class Model {}

/**
 * @template TKey of array-key
 * @template TModel
 *
 */
class SupportCollection {}

/**
 * @template TKey of array-key
 * @template TModel of Model
 *
 */
class Collection
{
    /**
     * Run a map over each of the items.
     *
     * @template TMapValue
     *
     * @param  callable(TModel, TKey): TMapValue  $callback
     * @return (TMapValue is Model ? static<TKey, TMapValue> : SupportCollection<TKey, TMapValue>)
     */
    public function map(callable $callback) {} // @phpstan-ignore return.missing
}

I was expecting the TMapValue to be narrowed down to Model in static<TKey, TMapValue> because we are in the truthy branch of the if condition. But as it can be seen in the playground, it doesn't work like that.

Am I wrong to expect that, or is it a bug?

Code snippet that reproduces the problem

https://phpstan.org/r/92226828-0e09-4210-9906-6b1df14395c0

Expected output

No errors.

Did PHPStan help you today? Did it make you happy in any way?

It helps me every day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions