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

Interface method's docblock not inherited properly with generics #10921

Open
Shira-3749 opened this issue Apr 26, 2024 · 1 comment
Open

Interface method's docblock not inherited properly with generics #10921

Shira-3749 opened this issue Apr 26, 2024 · 1 comment

Comments

@Shira-3749
Copy link

Interface method's docblock is not inherited properly with generics in this case:
https://psalm.dev/r/dd85197efc

Copy-pasting the entire doc-comment onto Collection::toArray() fixes this, but that's not ideal:
https://psalm.dev/r/f7036c58c3

For comparison, it works as-is in PHPStan:
https://phpstan.org/r/2185731e-afd0-4e07-905a-33908ee99f6a

Copy link

I found these snippets:

https://psalm.dev/r/dd85197efc
<?php declare(strict_types=1);

/**
 * @template-covariant T
 */
interface ReadableList
{
    /**
     * @return list<T>
     */
    function toArray(): array;
}

/**
 * @template T
 * @implements ReadableList<T>
 * @psalm-consistent-constructor
 */
class Collection implements ReadableList
{
    /**
     * @param list<T> $values
     */
    function __construct(protected array $values = [])
    {}
    
    function toArray(): array
    {
        return $this->values;
    }
}

/**
 * @template T of scalar
 * @extends Collection<T>
 */
class ScalarList extends Collection
{
}

/**
 * @param ScalarList<string> $strings
 * @psalm-suppress UnusedVariable
 */
function example(ScalarList $strings): void
{
    /** @psalm-check-type $x = list<string> */
	$x = $strings->toArray();
}
Psalm output (using commit 08afc45):

ERROR: CheckType - 48:2 - Checked variable $x = list<string> does not match $x = list<T:ScalarList as scalar>
https://psalm.dev/r/f7036c58c3
<?php declare(strict_types=1);

/**
 * @template-covariant T
 */
interface ReadableList
{
    /**
     * @return list<T>
     */
    function toArray(): array;
}

/**
 * @template T
 * @implements ReadableList<T>
 * @psalm-consistent-constructor
 */
class Collection implements ReadableList
{
    /**
     * @param list<T> $values
     */
    function __construct(protected array $values = [])
    {}
    
    /**
     * (copy-pasted doc-comment)
     *
     * @return list<T>
     */
    function toArray(): array
    {
        return $this->values;
    }
}

/**
 * @template T of scalar
 * @extends Collection<T>
 */
class ScalarList extends Collection
{
}

/**
 * @param ScalarList<string> $strings
 * @psalm-suppress UnusedVariable
 */
function example(ScalarList $strings): void
{
    /** @psalm-check-type $x = list<string> */
	$x = $strings->toArray();
}
Psalm output (using commit 08afc45):

No issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant