Skip to content

Commit 40a7163

Browse files
Bartłomiej Nowakbnowak
authored andcommitted
implementation for interfaces
1 parent 3a6ce55 commit 40a7163

3 files changed

Lines changed: 42 additions & 10 deletions

File tree

extension.neon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ parameters:
1010
consoleApplicationLoader: null
1111
messenger:
1212
handleTraitWrappers:
13-
# move that params to tests only
13+
# todo move that params to tests only
1414
- MessengerHandleTrait\QueryBus::dispatch
15-
- MessengerHandleTrait\QueryBus2::dispatch
15+
- MessengerHandleTrait\QueryBus::dispatch2
16+
- MessengerHandleTrait\QueryBusInterface::dispatch
1617
stubFiles:
1718
- stubs/Psr/Cache/CacheException.stub
1819
- stubs/Psr/Cache/CacheItemInterface.stub

src/Type/Symfony/MessengerHandleTraitWrapperReturnTypeExtension.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\MethodCall;
77
use PhpParser\Node\Identifier;
88
use PHPStan\Analyser\Scope;
9+
use PHPStan\Reflection\ReflectionProvider;
910
use PHPStan\Symfony\MessageMap;
1011
use PHPStan\Symfony\MessageMapFactory;
1112
use PHPStan\Type\ExpressionTypeResolverExtension;
@@ -18,7 +19,7 @@
1819
* Configurable extension for resolving return types of methods that internally use HandleTrait.
1920
*
2021
* Configured via PHPStan parameters under symfony.messenger.handleTraitWrappers with
21-
* "Class::method" patterns, e.g.:
22+
* "class::method" patterns, e.g.:
2223
* - App\Bus\QueryBus::dispatch
2324
* - App\Bus\QueryBus::query
2425
* - App\Bus\CommandBus::execute
@@ -34,11 +35,14 @@ final class MessengerHandleTraitWrapperReturnTypeExtension implements Expression
3435
/** @var array<string> */
3536
private array $wrappers;
3637

38+
private ReflectionProvider $reflectionProvider;
39+
3740
/** @param array{handleTraitWrappers: array<string>}|null $messenger */
38-
public function __construct(MessageMapFactory $messageMapFactory, ?array $messenger)
41+
public function __construct(MessageMapFactory $messageMapFactory, ?array $messenger, ReflectionProvider $reflectionProvider)
3942
{
4043
$this->messageMapFactory = $messageMapFactory;
4144
$this->wrappers = $messenger['handleTraitWrappers'] ?? [];
45+
$this->reflectionProvider = $reflectionProvider;
4246
}
4347

4448
public function getType(Expr $expr, Scope $scope): ?Type
@@ -91,8 +95,23 @@ private function isSupported(Expr $expr, Scope $scope): bool
9195
$className = $classNames[0];
9296
$classMethodCombination = $className . '::' . $methodName;
9397

94-
// Check if this class::method combination is configured
95-
return in_array($classMethodCombination, $this->wrappers, true);
98+
// Check if this exact class::method combination is configured
99+
if (in_array($classMethodCombination, $this->wrappers, true)) {
100+
return true;
101+
}
102+
103+
// Check if any interface implemented by this class::method is configured
104+
if ($this->reflectionProvider->hasClass($className)) {
105+
$classReflection = $this->reflectionProvider->getClass($className);
106+
foreach ($classReflection->getInterfaces() as $interface) {
107+
$interfaceMethodCombination = $interface->getName() . '::' . $methodName;
108+
if (in_array($interfaceMethodCombination, $this->wrappers, true)) {
109+
return true;
110+
}
111+
}
112+
}
113+
114+
return false;
96115
}
97116

98117
private function getMessageMap(): MessageMap

tests/Type/Symfony/data/messenger_handle_trait.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,18 @@ public function dispatch(object $query): mixed
6262
{
6363
return $this->handle($query);
6464
}
65+
66+
public function dispatch2(object $query): mixed
67+
{
68+
return $this->handle($query);
69+
}
70+
}
71+
72+
interface QueryBusInterface {
73+
public function dispatch(object $query): mixed;
6574
}
6675

67-
class QueryBus2 {
76+
class QueryBusWithInterface implements QueryBusInterface {
6877
use HandleTrait;
6978

7079
public function dispatch(object $query): mixed
@@ -87,11 +96,14 @@ public function action()
8796

8897
assertType(TaggedResult::class, $queryBus->dispatch(new TaggedQuery()));
8998

99+
assertType(RegularQueryResult::class, $queryBus->dispatch2(new RegularQuery()));
100+
101+
$queryBusWithInterface = new QueryBusWithInterface();
102+
103+
assertType(RegularQueryResult::class, $queryBusWithInterface->dispatch(new RegularQuery()));
104+
90105
// HandleTrait will throw exception in fact due to multiple handle methods/handlers per single query
91106
assertType('mixed', $queryBus->dispatch(new MultiHandlesForInTheSameHandlerQuery()));
92107
assertType('mixed', $queryBus->dispatch(new MultiHandlersForTheSameMessageQuery()));
93-
94-
$queryBus2 = new QueryBus2();
95-
assertType(TaggedResult::class, $queryBus2->dispatch(new TaggedQuery()));
96108
}
97109
}

0 commit comments

Comments
 (0)