Description
Recently, my PR about support for Messenger HandleTrait return types was merged. I would like to use that work done to ability for QueryBus classes to "learn them" how to determine their results correctly, wherever they are called.
There's related PR to this missing functionality, which demonstrate what I want to achieve. The most reasonably (and the easiest way of doing it) from my understanding would be getting query-result mapping (which HandleTrait::handle
method is aware now), and reusing it as the same input-output values for QueryBus::dispatch
method (from the PR's example). Perhaps, using some annotations?
Maybe it is trivial to complete, but at the moment I do not know how to connect these dots 😅
I will try to push this topic forward, but any thoughts/ideas are welcome :)
Activity
bnowak commentedon Jan 17, 2025
@ondrejmirtes it's the following part of your request here 😉
bnowak commentedon Feb 17, 2025
anyone? any idea please? 😅
/cc @ondrejmirtes @shish @lookyman @VincentLanglet @ruudk @JanTvrdik @staabm
ruudk commentedon Feb 17, 2025
This is how I do it in our project, where we have our own CommandBus interface (that has a Symfony Messenger implementation).
You can use this as inspiration 😉
Our handlers implement a custom
#[AsCommandHandler]
that we can read in a Bundle. Something like this:Then we have a CompilerPass that writes the mapping to a parameter:
command_handlers
.We have the following files for PHPStan.
This reads the mapping from the container.
This is a return type extension that is able to figure out what
$bus->handle(new SomeCommand))
returns.bnowak commentedon Mar 14, 2025
Thank you @ruudk for your detailed response and sorry for my late reply.
In fact, your solution is pretty similar to what I already done here.
It also creates kind of (command/query => result) map which PHPStan extension uses to determine the result statically. The differences in both implementations are that you have some additional custom & static part of code coupled with SF and project's CommandBus class dependency in PHPStan extension. My solution uses container which is already configured in
phpstan-symfony
settings (independently from project code) and do similar work, however it works currently only on level for class which uses messengerHandleTrait
(only internally). That's the case of that issue.Ideally, I'd like to reuse somehow already created map (internally in that plugin) to "learn" any bus classes (which return some single result using
HandleTrait
). I'd love to this in most simple way without need to adding any custom code to project codebase (excluding some annotation which PHPStan could understand) or add some code into this plugin which would help in "learning" that (eg. some extension for buses as you did, however more dynamically to not depend on any specific or do this in configurable way) 😉