using messenger HandleTrait as QueryBus with appropriate result typing#423
using messenger HandleTrait as QueryBus with appropriate result typing#423bnowak wants to merge 11 commits intophpstan:2.0.xfrom
Conversation
|
@ondrejmirtes Sorry to disturb you, but I think this is one of the pieces that is missing in this whole puzzle to support. Can we move forward ? |
|
Hi @michaljusiega, thanks for your reply. I think we can add it - no problem. However, from my perspective it's more important to address the root issue (missing feature) of the topic (described here). My reasoning is without having this result interpreted on bus class level - this is much less useful in general. |
|
So this QueryBus class is something in your codebase and you want the same type inference that classes using HandleTrait and calling the You can simply write a dynamic return type extension for |
cc1869e to
9b62bd8
Compare
|
@ondrejmirtes It's ready for review now. You were right about my intentions, however I wanted to make it working for any class that uses It's likely to be conflicted with my another PR about splitting tests, so once first of these PR will be merged I can resolve conflicts on another one. 😉 |
|
@ondrejmirtes any chances to review & merge it soon, please? :) |
| { | ||
| public function __invoke(GetProductQuery $query): Product | ||
| { | ||
| return $this->productRepository->get($query->productId); |
There was a problem hiding this comment.
Can you please add comments to the code in README about the inferred types with and without the extension? I'm still not sure what it does.
There was a problem hiding this comment.
Thanks for reply.
So it's about inferring types of query bus classes (from CQRS pattern) or any custom class which internally use messenger's HandleTrait to get and return result of any dispatched message. Without the extension phpstan is not able to recognize that and gives generic return type of bus class (which in fact is some more specified query result).
So the main difference is calling query bus classes, where based on passed/dispatched message we're getting its correct result type.
I've updated README a bit and the the direct outcome is shown in the end of it - it shows that we're getting Product type instead of generic mixed. If that's still not precised and unclear, please advice me what else I could add/adjust. Maybe my describing skills are not so good 😅
There was a problem hiding this comment.
@ondrejmirtes any chances to re-look on this please?
There was a problem hiding this comment.
@ondrejmirtes please, could you take a look on this again? and sorry for bothering you so many times
cdcaab5 to
a9f07aa
Compare
|
@ondrejmirtes could you review it again please? |
src/Type/Symfony/MessengerHandleTraitWrapperReturnTypeExtension.php
Outdated
Show resolved
Hide resolved
src/Type/Symfony/MessengerHandleTraitWrapperReturnTypeExtension.php
Outdated
Show resolved
Hide resolved
|
@VincentLanglet thank you for your reply and review. I've addressed your comments and also added support for union types in classes which use Please take a look and re-review. Will that change be automatically merged into 2.x branch as well when approved? 🤞 |
|
Oh, didn't see you were targeting the 1.4.x branch. You should target the 2.0.x instead ; there is no feature added to the version 1. |
6c3da90 to
5e425db
Compare
5e425db to
60d8836
Compare
There was a problem hiding this comment.
To avoid the failure on php 7.4 I think you have to replace : mixed by a phpdoc @return mixed (everywhere in this file)
There was a problem hiding this comment.
thanks for advice. I've tried that, but with just adding phpdoc it didn't change anything. It started working when I simply removed native return type (it's mixed by default).
Now it should work (I've tested locally on PHP 7.4 & 8.4), but now one CI job failed (on github.com authentication). I don't see button for retrying it (perhaps I don't have such permissions).
@VincentLanglet could you take a look and rerun CI jobs, please?
There was a problem hiding this comment.
I restarted the Ci it's ok
|
@VincentLanglet rebased to 2.0.x branch, but there's still issue with failed CI job (mentioned here). Could you take a look on that and re-review once again? I hope, I already covered everything in this PR 🙏 |
|
Since you initially requested changes @ondrejmirtes, your review is required here :) |
|
Thank you @VincentLanglet! @ondrejmirtes kindly request to take a look at that🙏 |
390c9d6 to
3dea9c3
Compare
This PR goal is to determine correct result typing for QueryBus kind of classes which uses SF messenger HandleTrait internally and simply return its results.