Skip to content

Implemented method with the same conditional return type as the interface reports ImplementedReturnTypeMismatch #12988

Done

Description

@alies-dev

Reproducer: https://psalm.dev/r/2a1f7c61b7

<?php
/**
 * @template T = mixed
 */
interface Box
{
    /**
     * @template U = never
     * @param (callable(T): U)|null $f
     * @return Box<($f is null ? T : U)>
     */
    public function map(?callable $f = null): Box;
}

/**
 * @template T = mixed
 * @implements Box<T>
 */
final class Just implements Box
{
    /** @param T $value */
    public function __construct(private mixed $value) {}

    /**
     * @template U = never
     * @param (callable(T): U)|null $f
     * @return Box<($f is null ? T : U)>
     */
    public function map(?callable $f = null): Box
    {
        return $f === null ? $this : new Just($f($this->value));
    }
}
ImplementedReturnTypeMismatch (line 27): The inherited return type 'Box<(T:Just as mixed)|(U:fn-box::map as mixed)>' for Box::map is different to the implemented return type for Just::map 'Box<(TGeneratedFromParam0 is null ? T:Just as mixed : U:fn-just::map as mixed)>'

The docblocks are identical. The inherited type has its conditional collapsed to T|U, while the implemented one keeps it, so the comparison fails. Expected: no issue.

Seen on guzzle/promises 3.1.0 (Promise::then, Coroutine::then), now in the perf corpus (#12984).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions