phpstan: Unable to resolve the template type for callable return type

In a library I am trying to give Psalm and PHPStan the needed type hints to know the return types of a transaction interface (for database transactions). It works with psalm, but PHPStan always gives the error Unable to resolve the template type TReturn in call to method TransactionInterface::run() each time TransactionInterface::run is used.

interface TransactionInterface
{
    /**
     * @param callable $func
     * @param mixed ...$arguments
     * @return mixed
     *
     * @template TReturn
     * @psalm-param callable: TReturn $func
     * @psalm-return TReturn
     */
    public function run(callable $func, ...$arguments);
}

The run functions basically executes $func and returns the same type as $func returns, so whatever the return type of $func is, is also the return type of the run function, and that is what the template syntax should convey. I have tried everything to make this work with PHPStan, but the same error remains. As far as I can tell PHPStan should support this syntax.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 24 (12 by maintainers)

Most upvoted comments

Fair points, maybe I err on the side of helping as fast as possible instead of thinking about it for a day 😃 I take all feedback on the account, I dismissed internally that the Writing PHPDocs menu should be always unwrapped even when the user is in other sections because there’s not much space in the sidebar to do that.

I understand that your experience might have been frustrating, but I really needed the example on phpstan.org in order to help you - the issue might have been something completely different (on the call site) and I’m not able to find it out just from a textual description.

It also doesn’t help that you ventured into an area that’s not well covered by PHPStan - not many people write variadic callables, and it’s clearly broken.

I’m really trying my best for users to have the best possible experience - when using PHPStan on their machine, when reading documentation, or filing an issue (and people are almost always grateful for fast responses), but it sometimes doesn’t work out well… Thanks for understanding.

I can’ think of a type-safe signature to achieve what you want, but myself I always do this instead: https://phpstan.org/r/5c8ce53f-de4f-404c-9faf-33d46cab2f89

I’ve tried to be helpful and was replying to you instantly, not sure what I did wrong. I can’t spend time writing 3 paragraphs every time someone asks a question. I’m considering all gathered feedback, but only in form of aggregated data, a single developer is rarely right or wrong.

Can’t please everyone I guess. Thank you for your sponsorship and I hope you reconsider in the future.

Also, there’s search… not sure what can I do more…

Screenshot 2020-04-25 at 14 50 16

I assumed this was about how to write PHP code for PHPStan

I think PHPDocs match this description exactly.