psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Psalm consider that B&A<Foo> is more general than A<Foo> but it should be more specific

Open VincentLanglet opened this issue 1 year ago • 1 comments

In https://psalm.dev/r/574fe7f3a5, B&A<Foo> is considered as more general than A<Foo>

Am I wrong or there is no error, because B&A<Foo> should be considered as more specific than A<Foo> ?

VincentLanglet avatar Aug 23 '22 07:08 VincentLanglet

I found these snippets:

https://psalm.dev/r/574fe7f3a5
<?php

/**
 * @phpstan-template T of object
 */
interface A
{
}

/**
 * @phpstan-template T of object
 * @phpstan-extends A<T>
 */
interface B extends A
{
}

/**
 * @phpstan-template T of object
 */
class Admin
{
    /**
     * @param A<T> $proxy
     * @return A<T>
     */
    public function configureQuery(A $proxy): A
    {
        return $proxy;
    }
}

class Foo {}

/**
 * @phpstan-extends Admin<Foo>
 */
class SuperAdmin extends Admin
{
    public function configureQuery(A $proxy): A
    {
        \assert($proxy instanceof B);
        
        return $proxy;
    }
}
Psalm output (using commit afe85fa):

INFO: LessSpecificReturnStatement - 44:16 - The type 'B&A<Foo>' is more general than the declared return type 'A<Foo>' for SuperAdmin::configureQuery

INFO: MoreSpecificReturnType - 40:47 - The declared return type 'A<Foo>' for SuperAdmin::configureQuery is more specific than the inferred return type 'B&A<Foo>'

psalm-github-bot[bot] avatar Aug 23 '22 07:08 psalm-github-bot[bot]