Skip to content

using messenger HandleTrait as QueryBus with appropriate result typing#423

Open
bnowak wants to merge 11 commits intophpstan:2.0.xfrom
bnowak:messenger-handle-trait-generic-typing-support
Open

using messenger HandleTrait as QueryBus with appropriate result typing#423
bnowak wants to merge 11 commits intophpstan:2.0.xfrom
bnowak:messenger-handle-trait-generic-typing-support

Conversation

@bnowak
Copy link
Contributor

@bnowak bnowak commented Jan 17, 2025

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.

@michaljusiega
Copy link

@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 ?

@bnowak
Copy link
Contributor Author

bnowak commented Mar 14, 2025

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.

@ondrejmirtes
Copy link
Member

So this QueryBus class is something in your codebase and you want the same type inference that classes using HandleTrait and calling the handle method have?

You can simply write a dynamic return type extension for QueryBus::dispatch() that delegates the type resolution to new MethodCall($methodCall->var, 'handle', $methodCall->getArgs()).

@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from cc1869e to 9b62bd8 Compare July 25, 2025 09:20
@bnowak bnowak marked this pull request as ready for review July 28, 2025 07:18
@bnowak
Copy link
Contributor Author

bnowak commented Jul 28, 2025

@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 HandleTrait internally. I didn't want to hardcoded it, so made it parameterized. I've added also support for interfaces. Please review in you free time 🙏

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. 😉

@bnowak
Copy link
Contributor Author

bnowak commented Aug 29, 2025

@ondrejmirtes any chances to review & merge it soon, please? :)

{
public function __invoke(GetProductQuery $query): Product
{
return $this->productRepository->get($query->productId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes any chances to re-look on this please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes ping 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes ping2 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes please, could you take a look on this again? and sorry for bothering you so many times

@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from cdcaab5 to a9f07aa Compare September 8, 2025 05:29
@bnowak bnowak requested a review from ondrejmirtes September 10, 2025 07:59
@bnowak
Copy link
Contributor Author

bnowak commented Dec 16, 2025

@ondrejmirtes could you review it again please?

@bnowak
Copy link
Contributor Author

bnowak commented Feb 17, 2026

@VincentLanglet thank you for your reply and review. I've addressed your comments and also added support for union types in classes which use HandleTrait directly (not only on their wrappers/query buses).

Please take a look and re-review.

Will that change be automatically merged into 2.x branch as well when approved? 🤞

@VincentLanglet
Copy link
Contributor

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.

@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from 6c3da90 to 5e425db Compare February 18, 2026 14:41
@bnowak bnowak changed the base branch from 1.4.x to 2.0.x February 18, 2026 14:42
@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from 5e425db to 60d8836 Compare February 18, 2026 14:42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the failure on php 7.4 I think you have to replace : mixed by a phpdoc @return mixed (everywhere in this file)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I restarted the Ci it's ok

@bnowak
Copy link
Contributor Author

bnowak commented Feb 18, 2026

@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 🙏

@VincentLanglet
Copy link
Contributor

Since you initially requested changes @ondrejmirtes, your review is required here :)

@bnowak
Copy link
Contributor Author

bnowak commented Feb 19, 2026

Thank you @VincentLanglet!

@ondrejmirtes kindly request to take a look at that🙏

@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from 390c9d6 to 3dea9c3 Compare February 19, 2026 08:21
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

Successfully merging this pull request may close these issues.

4 participants

Comments