vimeo / psalm

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

Cannot define a param of a class template, with a function template hydrated by the param? #11125

Open devnix opened 5 days ago

devnix commented 5 days ago

Yup, I don't know how to phrase the title better 😰

I'll let this snippet describe it better: https://psalm.dev/r/ffdbd898f0

psalm-github-bot[bot] commented 5 days ago

I found these snippets:

https://psalm.dev/r/ffdbd898f0 ```php $query * * @return TReturn */ public function __invoke($query); } /** * @implements Query */ final readonly class Get implements Query { public function __construct(public string $id) { } } /** * @implements QueryHandler */ final class GetHandler implements QueryHandler { public function __invoke($get) { return 42; } } ``` ``` Psalm output (using commit 03ee02c): ERROR: UndefinedDocblockClass - 18:15 - Docblock-defined class, interface or enum named TQuery does not exist ERROR: InvalidReturnType - 40:21 - The declared return type 'TReturn:fn-queryhandler::__invoke as mixed' for GetHandler::__invoke is incorrect, got 'int' ```
devnix commented 5 days ago

I posted a different report with the same example in PHPStan: https://github.com/phpstan/phpstan/issues/11839

Maybe I'm missing an already existing solution around "inner templates"? If not, would my example be an agreeable common syntax for this problem in both projects?