-
Notifications
You must be signed in to change notification settings - Fork 99
using messenger HandleTrait as QueryBus with appropriate result typing #423
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
Merged
ondrejmirtes
merged 11 commits into
phpstan:2.0.x
from
bnowak:messenger-handle-trait-generic-typing-support
Feb 26, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5d0d321
test-cases for using messenger HandleTrait as QueryBus
8a0a58d
implementation
05323a1
implementation for interfaces
452e3da
move test parameters to test extension-test-neon file
86e4507
updated README.md
0ff50fc
updated README.md
45b58a6
test class name fix
618ff42
added union test-cases
bnowak baec2a9
support for handling union types
bnowak 76a79c1
messenger params injected as service argument
bnowak c5829f9
fixed return mixed type wrong inference
bnowak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
src/Type/Symfony/MessengerHandleTraitWrapperReturnTypeExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Type\Symfony; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Identifier; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Symfony\MessageMap; | ||
| use PHPStan\Symfony\MessageMapFactory; | ||
| use PHPStan\Type\ExpressionTypeResolverExtension; | ||
| use PHPStan\Type\Type; | ||
| use PHPStan\Type\TypeCombinator; | ||
| use function count; | ||
| use function in_array; | ||
| use function is_null; | ||
|
|
||
| /** | ||
| * Configurable extension for resolving return types of methods that internally use HandleTrait. | ||
| * | ||
| * Configured via PHPStan parameters under symfony.messenger.handleTraitWrappers with | ||
| * "class::method" patterns, e.g.: | ||
| * - App\Bus\QueryBus::dispatch | ||
| * - App\Bus\QueryBus::query | ||
| * - App\Bus\CommandBus::execute | ||
| * - App\Bus\CommandBus::handle | ||
| */ | ||
| final class MessengerHandleTraitWrapperReturnTypeExtension implements ExpressionTypeResolverExtension | ||
| { | ||
|
|
||
| private MessageMapFactory $messageMapFactory; | ||
|
|
||
| private ?MessageMap $messageMap = null; | ||
|
|
||
| /** @var array<string> */ | ||
| private array $wrappers; | ||
|
|
||
| private ReflectionProvider $reflectionProvider; | ||
|
|
||
| /** @param array{handleTraitWrappers: array<string>}|null $messenger */ | ||
| public function __construct(MessageMapFactory $messageMapFactory, ?array $messenger, ReflectionProvider $reflectionProvider) | ||
| { | ||
| $this->messageMapFactory = $messageMapFactory; | ||
| $this->wrappers = $messenger['handleTraitWrappers'] ?? []; | ||
| $this->reflectionProvider = $reflectionProvider; | ||
| } | ||
|
|
||
| public function getType(Expr $expr, Scope $scope): ?Type | ||
| { | ||
| if (!$this->isSupported($expr, $scope)) { | ||
| return null; | ||
| } | ||
|
|
||
| $args = $expr->getArgs(); | ||
| if (count($args) !== 1) { | ||
| return null; | ||
| } | ||
|
|
||
| $arg = $args[0]->value; | ||
| $argClassNames = $scope->getType($arg)->getObjectClassNames(); | ||
|
|
||
| if (count($argClassNames) === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| $returnTypes = []; | ||
| foreach ($argClassNames as $argClassName) { | ||
| $messageMap = $this->getMessageMap(); | ||
| $returnType = $messageMap->getTypeForClass($argClassName); | ||
|
|
||
| if (is_null($returnType)) { | ||
| return null; | ||
| } | ||
|
|
||
| $returnTypes[] = $returnType; | ||
| } | ||
|
|
||
| return TypeCombinator::union(...$returnTypes); | ||
| } | ||
|
|
||
| /** | ||
| * @phpstan-assert-if-true =MethodCall $expr | ||
| */ | ||
| private function isSupported(Expr $expr, Scope $scope): bool | ||
| { | ||
| if ($this->wrappers === []) { | ||
| return false; | ||
| } | ||
|
|
||
| if (!($expr instanceof MethodCall) || !($expr->name instanceof Identifier)) { | ||
| return false; | ||
| } | ||
|
|
||
| $methodName = $expr->name->name; | ||
| $varType = $scope->getType($expr->var); | ||
| $classNames = $varType->getObjectClassNames(); | ||
|
|
||
| if (count($classNames) === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach ($classNames as $className) { | ||
| if (!$this->isClassMethodSupported($className, $methodName)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| private function isClassMethodSupported(string $className, string $methodName): bool | ||
| { | ||
| $classMethodCombination = $className . '::' . $methodName; | ||
|
|
||
| // Check if this exact class::method combination is configured | ||
| if (in_array($classMethodCombination, $this->wrappers, true)) { | ||
| return true; | ||
| } | ||
|
|
||
| // Check if any interface implemented by this class::method is configured | ||
| if ($this->reflectionProvider->hasClass($className)) { | ||
| $classReflection = $this->reflectionProvider->getClass($className); | ||
| foreach ($classReflection->getInterfaces() as $interface) { | ||
| $interfaceMethodCombination = $interface->getName() . '::' . $methodName; | ||
| if (in_array($interfaceMethodCombination, $this->wrappers, true)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private function getMessageMap(): MessageMap | ||
| { | ||
| if ($this->messageMap === null) { | ||
| $this->messageMap = $this->messageMapFactory->create(); | ||
| } | ||
|
|
||
| return $this->messageMap; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Producttype instead of genericmixed. 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.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes ping 🙏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes ping2 🙏
There was a problem hiding this comment.
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