vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.49k stars 655 forks source link

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

Open Shira-3749 opened 2 months ago

Shira-3749 commented 2 months ago

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

psalm-github-bot[bot] commented 2 months ago

I found these snippets:

https://psalm.dev/r/dd85197efc ```php */ function toArray(): array; } /** * @template T * @implements ReadableList * @psalm-consistent-constructor */ class Collection implements ReadableList { /** * @param list $values */ function __construct(protected array $values = []) {} function toArray(): array { return $this->values; } } /** * @template T of scalar * @extends Collection */ class ScalarList extends Collection { } /** * @param ScalarList $strings * @psalm-suppress UnusedVariable */ function example(ScalarList $strings): void { /** @psalm-check-type $x = list */ $x = $strings->toArray(); } ``` ``` Psalm output (using commit 08afc45): ERROR: CheckType - 48:2 - Checked variable $x = list does not match $x = list ```
https://psalm.dev/r/f7036c58c3 ```php */ function toArray(): array; } /** * @template T * @implements ReadableList * @psalm-consistent-constructor */ class Collection implements ReadableList { /** * @param list $values */ function __construct(protected array $values = []) {} /** * (copy-pasted doc-comment) * * @return list */ function toArray(): array { return $this->values; } } /** * @template T of scalar * @extends Collection */ class ScalarList extends Collection { } /** * @param ScalarList $strings * @psalm-suppress UnusedVariable */ function example(ScalarList $strings): void { /** @psalm-check-type $x = list */ $x = $strings->toArray(); } ``` ``` Psalm output (using commit 08afc45): No issues! ```