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

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

Open
devnix opened this issue Oct 13, 2024 · 2 comments

Comments

@devnix
Copy link

devnix commented Oct 13, 2024

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

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

Copy link

I found these snippets:

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

/**
 * @template T
 */
interface Query
{
}

/**
 * @template TQuery of Query
 */
interface QueryHandler
{
    /**
     * @template TReturn
     *
     * @param TQuery<TReturn> $query
     *
     * @return TReturn
     */
    public function __invoke($query);
}

/**
 * @implements Query<int>
 */
final readonly class Get implements Query
{
    public function __construct(public string $id)
    {
    }
}

/**
 * @implements QueryHandler<Get>
 */
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
Copy link
Author

devnix commented Oct 13, 2024

I posted a different report with the same example in PHPStan: phpstan/phpstan#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?

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