From c2f249b806d751025e2b551ff5d85be943e00bf0 Mon Sep 17 00:00:00 2001 From: Fritz Gerneth Date: Tue, 10 Feb 2026 07:45:17 +0000 Subject: [PATCH] Add nullable information to Subscriber ArgumentMetadata --- phpstan-baseline.neon | 2 +- src/Metadata/Subscriber/ArgumentMetadata.php | 1 + .../AttributeSubscriberMetadataFactory.php | 1 + ...AttributeSubscriberMetadataFactoryTest.php | 31 +++++++++++++++++-- .../AggregateIdArgumentResolverTest.php | 8 ++--- .../EventArgumentResolverTest.php | 6 ++-- .../ArgumentResolver/LookupResolverTest.php | 6 ++-- .../MessageArgumentResolverTest.php | 6 ++-- .../RecordedOnArgumentResolverTest.php | 8 ++--- 9 files changed, 48 insertions(+), 21 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2ee97190e..87f5189e0 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -463,7 +463,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\:237\:\: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/ArgumentMetadata.php b/src/Metadata/Subscriber/ArgumentMetadata.php index 56576fdba..ed7ef590c 100644 --- a/src/Metadata/Subscriber/ArgumentMetadata.php +++ b/src/Metadata/Subscriber/ArgumentMetadata.php @@ -9,6 +9,7 @@ final class ArgumentMetadata public function __construct( public readonly string $name, public readonly string $type, + public readonly bool $allowsNull = false, ) { } } diff --git a/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php b/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php index 649acaf3d..3119e9ff8 100644 --- a/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php +++ b/src/Metadata/Subscriber/AttributeSubscriberMetadataFactory.php @@ -151,6 +151,7 @@ private function subscribeMethod(ReflectionMethod $method): SubscribeMethodMetad $arguments[] = new ArgumentMetadata( $parameter->getName(), $type->getName(), + $parameter->allowsNull(), ); } diff --git a/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php b/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php index cf33e29bb..2edbb3158 100644 --- a/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php +++ b/tests/Unit/Metadata/Subscriber/AttributeSubscriberMetadataFactoryTest.php @@ -191,13 +191,38 @@ public function profileCreated(ProfileCreated $profileCreated, string $aggregate [ ProfileVisited::class => [ new SubscribeMethodMetadata('profileVisited', [ - new ArgumentMetadata('message', Message::class), + new ArgumentMetadata('message', Message::class, false), ]), ], ProfileCreated::class => [ new SubscribeMethodMetadata('profileCreated', [ - new ArgumentMetadata('profileCreated', ProfileCreated::class), - new ArgumentMetadata('aggregateId', 'string'), + new ArgumentMetadata('profileCreated', ProfileCreated::class, false), + new ArgumentMetadata('aggregateId', 'string', false), + ]), + ], + ], + $metadata->subscribeMethods, + ); + } + + public function testSubscribeNullableAttribute(): void + { + $subscriber = new #[Subscriber('foo', RunMode::FromBeginning)] + class { + #[Subscribe(ProfileVisited::class)] + public function profileVisited(ProfileVisited|null $message): void + { + } + }; + + $metadataFactory = new AttributeSubscriberMetadataFactory(); + $metadata = $metadataFactory->metadata($subscriber::class); + + self::assertEquals( + [ + ProfileVisited::class => [ + new SubscribeMethodMetadata('profileVisited', [ + new ArgumentMetadata('message', ProfileVisited::class, true), ]), ], ], diff --git a/tests/Unit/Subscription/Subscriber/ArgumentResolver/AggregateIdArgumentResolverTest.php b/tests/Unit/Subscription/Subscriber/ArgumentResolver/AggregateIdArgumentResolverTest.php index 4524e3a9a..8d0b1a295 100644 --- a/tests/Unit/Subscription/Subscriber/ArgumentResolver/AggregateIdArgumentResolverTest.php +++ b/tests/Unit/Subscription/Subscriber/ArgumentResolver/AggregateIdArgumentResolverTest.php @@ -26,21 +26,21 @@ public function testSupport(): void self::assertTrue( $resolver->support( - new ArgumentMetadata('aggregateId', Uuid::class), + new ArgumentMetadata('aggregateId', Uuid::class, false), ProfileCreated::class, ), ); self::assertTrue( $resolver->support( - new ArgumentMetadata('aggregateRootId', ProfileId::class), + new ArgumentMetadata('aggregateRootId', ProfileId::class, false), ProfileCreated::class, ), ); self::assertFalse( $resolver->support( - new ArgumentMetadata('foo', ProfileCreated::class), + new ArgumentMetadata('foo', ProfileCreated::class, false), ProfileCreated::class, ), ); @@ -58,7 +58,7 @@ public function testResolve(): void self::assertEquals( new CustomId('bar'), $resolver->resolve( - new ArgumentMetadata('foo', CustomId::class), + new ArgumentMetadata('foo', CustomId::class, false), $message, ), ); diff --git a/tests/Unit/Subscription/Subscriber/ArgumentResolver/EventArgumentResolverTest.php b/tests/Unit/Subscription/Subscriber/ArgumentResolver/EventArgumentResolverTest.php index 1317b9302..718ba6547 100644 --- a/tests/Unit/Subscription/Subscriber/ArgumentResolver/EventArgumentResolverTest.php +++ b/tests/Unit/Subscription/Subscriber/ArgumentResolver/EventArgumentResolverTest.php @@ -22,14 +22,14 @@ public function testSupport(): void self::assertTrue( $resolver->support( - new ArgumentMetadata('foo', ProfileCreated::class), + new ArgumentMetadata('foo', ProfileCreated::class, false), ProfileCreated::class, ), ); self::assertFalse( $resolver->support( - new ArgumentMetadata('foo', ProfileVisited::class), + new ArgumentMetadata('foo', ProfileVisited::class, false), ProfileCreated::class, ), ); @@ -45,7 +45,7 @@ public function testResolve(): void self::assertSame( $event, $resolver->resolve( - new ArgumentMetadata('foo', ProfileVisited::class), + new ArgumentMetadata('foo', ProfileVisited::class, false), $message, ), ); diff --git a/tests/Unit/Subscription/Subscriber/ArgumentResolver/LookupResolverTest.php b/tests/Unit/Subscription/Subscriber/ArgumentResolver/LookupResolverTest.php index 6a46c45f4..527f4d15a 100644 --- a/tests/Unit/Subscription/Subscriber/ArgumentResolver/LookupResolverTest.php +++ b/tests/Unit/Subscription/Subscriber/ArgumentResolver/LookupResolverTest.php @@ -32,14 +32,14 @@ public function testSupport(): void self::assertTrue( $resolver->support( - new ArgumentMetadata('lookup', Lookup::class), + new ArgumentMetadata('lookup', Lookup::class, false), ProfileCreated::class, ), ); self::assertFalse( $resolver->support( - new ArgumentMetadata('foo', ProfileCreated::class), + new ArgumentMetadata('foo', ProfileCreated::class, false), ProfileCreated::class, ), ); @@ -61,7 +61,7 @@ public function testResolve(): void ); $lookup = $resolver->resolve( - new ArgumentMetadata('foo', Lookup::class), + new ArgumentMetadata('foo', Lookup::class, false), $message, ); diff --git a/tests/Unit/Subscription/Subscriber/ArgumentResolver/MessageArgumentResolverTest.php b/tests/Unit/Subscription/Subscriber/ArgumentResolver/MessageArgumentResolverTest.php index eee886166..a78e58955 100644 --- a/tests/Unit/Subscription/Subscriber/ArgumentResolver/MessageArgumentResolverTest.php +++ b/tests/Unit/Subscription/Subscriber/ArgumentResolver/MessageArgumentResolverTest.php @@ -20,14 +20,14 @@ public function testSupport(): void self::assertTrue( $resolver->support( - new ArgumentMetadata('foo', Message::class), + new ArgumentMetadata('foo', Message::class, false), 'qux', ), ); self::assertFalse( $resolver->support( - new ArgumentMetadata('foo', 'bar'), + new ArgumentMetadata('foo', 'bar', false), 'qux', ), ); @@ -41,7 +41,7 @@ public function testResolve(): void self::assertSame( $message, $resolver->resolve( - new ArgumentMetadata('foo', Message::class), + new ArgumentMetadata('foo', Message::class, false), $message, ), ); diff --git a/tests/Unit/Subscription/Subscriber/ArgumentResolver/RecordedOnArgumentResolverTest.php b/tests/Unit/Subscription/Subscriber/ArgumentResolver/RecordedOnArgumentResolverTest.php index 3ef1a8e8c..73503a336 100644 --- a/tests/Unit/Subscription/Subscriber/ArgumentResolver/RecordedOnArgumentResolverTest.php +++ b/tests/Unit/Subscription/Subscriber/ArgumentResolver/RecordedOnArgumentResolverTest.php @@ -23,14 +23,14 @@ public function testSupport(): void self::assertTrue( $resolver->support( - new ArgumentMetadata('foo', DateTimeImmutable::class), + new ArgumentMetadata('foo', DateTimeImmutable::class, false), 'qux', ), ); self::assertFalse( $resolver->support( - new ArgumentMetadata('foo', 'bar'), + new ArgumentMetadata('foo', 'bar', false), 'qux', ), ); @@ -53,7 +53,7 @@ public function testResolveFromAggregateHeader(): void self::assertSame( $date, $resolver->resolve( - new ArgumentMetadata('foo', DateTimeImmutable::class), + new ArgumentMetadata('foo', DateTimeImmutable::class, false), $message, ), ); @@ -69,7 +69,7 @@ public function testResolveFromRecordedOnHeader(): void self::assertSame( $date, $resolver->resolve( - new ArgumentMetadata('foo', DateTimeImmutable::class), + new ArgumentMetadata('foo', DateTimeImmutable::class, false), $message, ), );