diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 514dfbe7a..e1143c521 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -439,7 +439,7 @@ parameters: path: tests/Unit/Message/Translator/ReplaceEventTranslatorTest.php - - message: '#^Method class@anonymous/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest\.php\:212\:\:profileVisited\(\) has parameter \$message with no type specified\.$#' + message: '#^Method class@anonymous/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest\.php\:211\:\:profileVisited\(\) has parameter \$message with no type specified\.$#' identifier: missingType.parameter count: 1 path: tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php diff --git a/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php b/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php index 649acaf3d..324fb0686 100644 --- a/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php +++ b/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php @@ -58,12 +58,12 @@ public function metadata(string $subscriber): SubscriberMetadata throw DuplicateSubscribeMethod::duplicateEvent( $subscriber, $eventClass, - $subscribeMethods[$eventClass][0]->name, + $subscribeMethods[$eventClass]->name, $method->getName(), ); } - $subscribeMethods[$eventClass][] = $this->subscribeMethod($method); + $subscribeMethods[$eventClass] = $this->subscribeMethod($method); } if ($method->getAttributes(OnFailed::class)) { diff --git a/src/Metadata/Subscriber/SubscriberMetadata.php b/src/Metadata/Subscriber/SubscriberMetadata.php index 87b13ab39..bad075ab0 100644 --- a/src/Metadata/Subscriber/SubscriberMetadata.php +++ b/src/Metadata/Subscriber/SubscriberMetadata.php @@ -13,7 +13,7 @@ public function __construct( public readonly string $id, public readonly string $group = Subscription::DEFAULT_GROUP, public readonly RunMode $runMode = RunMode::FromBeginning, - /** @var array> */ + /** @var array */ public readonly array $subscribeMethods = [], public readonly string|null $setupMethod = null, public readonly string|null $teardownMethod = null, diff --git a/src/Subscription/Subscriber/MetadataSubscriberAccessor.php b/src/Subscription/Subscriber/MetadataSubscriberAccessor.php index 4435a3699..a706adc7a 100644 --- a/src/Subscription/Subscriber/MetadataSubscriberAccessor.php +++ b/src/Subscription/Subscriber/MetadataSubscriberAccessor.php @@ -16,7 +16,6 @@ use function array_key_exists; use function array_keys; use function array_map; -use function array_merge; /** @template T of object */ final class MetadataSubscriberAccessor implements SubscriberAccessor, RealSubscriberAccessor @@ -115,10 +114,15 @@ public function subscribeMethods(string $eventClass): array return $this->subscribeCache[$eventClass]; } - $methods = array_merge( - $this->metadata->subscribeMethods[$eventClass] ?? [], - $this->metadata->subscribeMethods[Subscribe::ALL] ?? [], - ); + $methods = []; + + if (array_key_exists($eventClass, $this->metadata->subscribeMethods)) { + $methods[] = $this->metadata->subscribeMethods[$eventClass]; + } + + if (array_key_exists(Subscribe::ALL, $this->metadata->subscribeMethods)) { + $methods[] = $this->metadata->subscribeMethods[Subscribe::ALL]; + } $this->subscribeCache[$eventClass] = array_map( fn (SubscribeMethodMetadata $method): Closure => $this->createClosure($eventClass, $method), diff --git a/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php b/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php index cf33e29bb..cb934fadb 100644 --- a/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php +++ b/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php @@ -114,9 +114,7 @@ public function drop(): void self::assertEquals( [ - ProfileVisited::class => [ - new SubscribeMethodMetadata('handle', []), - ], + ProfileVisited::class => new SubscribeMethodMetadata('handle', []), ], $metadata->subscribeMethods, ); @@ -141,8 +139,8 @@ public function handle(): void self::assertEquals( [ - ProfileVisited::class => [new SubscribeMethodMetadata('handle', [])], - ProfileCreated::class => [new SubscribeMethodMetadata('handle', [])], + ProfileVisited::class => new SubscribeMethodMetadata('handle', []), + ProfileCreated::class => new SubscribeMethodMetadata('handle', []), ], $metadata->subscribeMethods, ); @@ -163,7 +161,7 @@ public function handle(): void self::assertEquals( [ - '*' => [new SubscribeMethodMetadata('handle', [])], + '*' => new SubscribeMethodMetadata('handle', []), ], $metadata->subscribeMethods, ); @@ -189,17 +187,18 @@ public function profileCreated(ProfileCreated $profileCreated, string $aggregate self::assertEquals( [ - ProfileVisited::class => [ - new SubscribeMethodMetadata('profileVisited', [ - new ArgumentMetadata('message', Message::class), - ]), - ], - ProfileCreated::class => [ - new SubscribeMethodMetadata('profileCreated', [ + ProfileVisited::class => new SubscribeMethodMetadata( + 'profileVisited', + [new ArgumentMetadata('message', Message::class)], + ), + + ProfileCreated::class => new SubscribeMethodMetadata( + 'profileCreated', + [ new ArgumentMetadata('profileCreated', ProfileCreated::class), new ArgumentMetadata('aggregateId', 'string'), - ]), - ], + ], + ), ], $metadata->subscribeMethods, );