diff --git a/src/CommandBus.php b/src/CommandBus.php index 4425938..19bc059 100644 --- a/src/CommandBus.php +++ b/src/CommandBus.php @@ -19,6 +19,12 @@ public function __construct( ) { } + /** + * @throws CommandNotRegisteredException + * @throws \Throwable + * + * @psalm-suppress InvalidReturnType + */ public function dispatch(CommandInterface $command): mixed { try { diff --git a/src/Exception/CommandNotRegisteredException.php b/src/Exception/CommandNotRegisteredException.php index 2195dfa..b1dfac8 100644 --- a/src/Exception/CommandNotRegisteredException.php +++ b/src/Exception/CommandNotRegisteredException.php @@ -12,7 +12,7 @@ public function __construct(CommandInterface $command, \Throwable $previous) { parent::__construct( sprintf("The command <%s> hasn't a command handler associated", get_class($command)), - $previous->getCode(), + (int)$previous->getCode(), $previous ); } diff --git a/src/Exception/HandlerTypeIsNotSupported.php b/src/Exception/HandlerTypeIsNotSupported.php index 2d76929..069a090 100644 --- a/src/Exception/HandlerTypeIsNotSupported.php +++ b/src/Exception/HandlerTypeIsNotSupported.php @@ -8,7 +8,7 @@ class HandlerTypeIsNotSupported extends CqrsException { - public function __construct(Envelope $envelope, ?Throwable $previous = null) + public function __construct(Envelope $envelope, ?\Throwable $previous = null) { parent::__construct( \sprintf( diff --git a/src/Exception/QueryNotRegisteredException.php b/src/Exception/QueryNotRegisteredException.php index afe11e9..cc552d4 100644 --- a/src/Exception/QueryNotRegisteredException.php +++ b/src/Exception/QueryNotRegisteredException.php @@ -12,7 +12,7 @@ public function __construct(QueryInterface $query, \Throwable $previous = null) { parent::__construct( sprintf("The query <%s> hasn't a query handler associated", get_class($query)), - $previous->getCode(), + (int)$previous->getCode(), $previous ); } diff --git a/src/HandlersLocator.php b/src/HandlersLocator.php index d375bdf..1eb7268 100644 --- a/src/HandlersLocator.php +++ b/src/HandlersLocator.php @@ -4,6 +4,10 @@ namespace Spiral\Cqrs; +use Generator; +use ReflectionMethod; +use ReflectionNamedType; +use ReflectionType; use Spiral\Attributes\ReaderInterface; use Spiral\Core\Container; use Spiral\Cqrs\Attribute\CommandHandler; @@ -84,11 +88,12 @@ private function shouldHandle(Envelope $envelope, HandlerDescriptor $handlerDesc return true; } + /** @var ReceivedStamp $received */ return $received->getTransportName() === $expectedTransport; } /** - * @param array{0: class-string, 1: non-empty-string} $handler + * @param array{0: class-string, 1: non-empty-string} $handler */ private function buildHandlerDescriptor(array $handler): HandlerDescriptor { @@ -113,13 +118,13 @@ private function lookForHandlers() } } - private function processCommandHandler(\ReflectionMethod $method): void + private function processCommandHandler(ReflectionMethod $method): void { $this->assertHandlerMethodIsPublic($method); - foreach ($method->getParameters() as $parameter) { - if (is_a($parameter->getType()->getName(), CommandInterface::class, true)) { - $this->commandHandlers[$parameter->getType()->getName()][] = [ + foreach ($this->getMethodParameters($method) as $parameter) { + if (is_a($parameter->getName(), CommandInterface::class, true)) { + $this->commandHandlers[$parameter->getName()][] = [ $method->getDeclaringClass()->getName(), $method->getName(), ]; @@ -127,13 +132,13 @@ private function processCommandHandler(\ReflectionMethod $method): void } } - private function processQueryHandler(\ReflectionMethod $method) + private function processQueryHandler(ReflectionMethod $method) { $this->assertHandlerMethodIsPublic($method); - foreach ($method->getParameters() as $parameter) { - if (is_a($parameter->getType()->getName(), QueryInterface::class, true)) { - $this->queryHandlers[$parameter->getType()->getName()][] = [ + foreach ($this->getMethodParameters($method) as $parameter) { + if (is_a($parameter->getName(), QueryInterface::class, true)) { + $this->queryHandlers[$parameter->getName()][] = [ $method->getDeclaringClass()->getName(), $method->getName(), ]; @@ -144,7 +149,7 @@ private function processQueryHandler(\ReflectionMethod $method) /** * @throws InvalidHandlerException */ - private function assertHandlerMethodIsPublic(\ReflectionMethod $method): void + private function assertHandlerMethodIsPublic(ReflectionMethod $method): void { if (! $method->isPublic()) { throw new InvalidHandlerException( @@ -156,4 +161,21 @@ private function assertHandlerMethodIsPublic(\ReflectionMethod $method): void ); } } + + /** + * @param ReflectionMethod $method + * @return Generator + */ + private function getMethodParameters(ReflectionMethod $method): Generator + { + foreach ($method->getParameters() as $parameter) { + if ($parameter->getType() instanceof \ReflectionUnionType) { + foreach ($parameter->getType()->getTypes() as $type) { + yield $type; + } + } else { + yield $parameter->getType(); + } + } + } } diff --git a/src/MessageBusExceptionTrait.php b/src/MessageBusExceptionTrait.php index 82c0853..63df233 100644 --- a/src/MessageBusExceptionTrait.php +++ b/src/MessageBusExceptionTrait.php @@ -8,6 +8,9 @@ trait MessageBusExceptionTrait { + /** + * @throws \Throwable + */ public function throwException(HandlerFailedException $exception): void { while ($exception instanceof HandlerFailedException) { diff --git a/src/QueryBus.php b/src/QueryBus.php index 328db65..9ad9455 100644 --- a/src/QueryBus.php +++ b/src/QueryBus.php @@ -19,6 +19,12 @@ public function __construct( ) { } + /** + * @throws QueryNotRegisteredException + * @throws \Throwable + * + * @psalm-suppress InvalidReturnType + */ public function ask(QueryInterface $query): mixed { try { diff --git a/tests/app/Command/UpdateUser.php b/tests/app/Command/UpdateUser.php new file mode 100644 index 0000000..bb9b4ea --- /dev/null +++ b/tests/app/Command/UpdateUser.php @@ -0,0 +1,16 @@ +entityManager->store([ 'uuid' => $command->uuid,