diff --git a/src/Symfony/MessageMapFactory.php b/src/Symfony/MessageMapFactory.php index 5c9ef152..d94f046f 100644 --- a/src/Symfony/MessageMapFactory.php +++ b/src/Symfony/MessageMapFactory.php @@ -7,6 +7,7 @@ use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; use function class_exists; use function count; +use function interface_exists; use function is_array; use function is_int; use function is_string; @@ -91,7 +92,7 @@ public function create(): MessageMap /** @return iterable> */ private function guessHandledMessages(ClassReflection $reflectionClass): iterable { - if ($reflectionClass->implementsInterface(MessageSubscriberInterface::class)) { + if (interface_exists(MessageSubscriberInterface::class) && $reflectionClass->implementsInterface(MessageSubscriberInterface::class)) { $className = $reflectionClass->getName(); foreach ($className::getHandledMessages() as $index => $value) { diff --git a/tests/Type/Symfony/ExtensionTest.php b/tests/Type/Symfony/ExtensionTest.php index 40420be0..0848715e 100644 --- a/tests/Type/Symfony/ExtensionTest.php +++ b/tests/Type/Symfony/ExtensionTest.php @@ -6,6 +6,7 @@ use ReflectionMethod; use Symfony\Component\HttpFoundation\Request; use function class_exists; +use function interface_exists; use function strpos; class ExtensionTest extends TypeInferenceTestCase @@ -15,6 +16,11 @@ class ExtensionTest extends TypeInferenceTestCase public function dataFileAsserts(): iterable { yield from $this->gatherAssertTypes(__DIR__ . '/data/messenger_handle_trait.php'); + + if (interface_exists('Symfony\Component\Messenger\Handler\MessageSubscriberInterface')) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/messenger_handle_trait_with_subscriber.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/envelope_all.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/header_bag_get.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/response_header_bag_get_cookies.php'); diff --git a/tests/Type/Symfony/data/messenger_handle_trait.php b/tests/Type/Symfony/data/messenger_handle_trait.php index 7a86d482..10f62a0c 100644 --- a/tests/Type/Symfony/data/messenger_handle_trait.php +++ b/tests/Type/Symfony/data/messenger_handle_trait.php @@ -2,7 +2,6 @@ namespace MessengerHandleTrait; -use Symfony\Component\Messenger\Handler\MessageSubscriberInterface; use Symfony\Component\Messenger\HandleTrait; use function PHPStan\Testing\assertType; @@ -16,41 +15,6 @@ public function __invoke(RegularQuery $query): RegularQueryResult } } -class BooleanQuery {} -class StringQuery {} -class IntQuery {} -class FloatQuery {} -class MultiQueryHandler implements MessageSubscriberInterface -{ - public static function getHandledMessages(): iterable - { - yield BooleanQuery::class; - yield IntQuery::class => ['method' => 'handleInt']; - yield FloatQuery::class => ['method' => 'handleFloat']; - yield StringQuery::class => ['method' => 'handleString']; - } - - public function __invoke(BooleanQuery $query): bool - { - return true; - } - - public function handleInt(IntQuery $query): int - { - return 0; - } - - public function handleFloat(FloatQuery $query): float - { - return 0.0; - } - - public function handleString(StringQuery $query): string - { - return 'string result'; - } -} - class TaggedQuery {} class TaggedResult {} class TaggedHandler @@ -61,21 +25,6 @@ public function handle(TaggedQuery $query): TaggedResult } } -class MultiHandlesForInTheSameHandlerQuery {} -class MultiHandlesForInTheSameHandler implements MessageSubscriberInterface -{ - public static function getHandledMessages(): iterable - { - yield MultiHandlesForInTheSameHandlerQuery::class; - yield MultiHandlesForInTheSameHandlerQuery::class => ['priority' => '0']; - } - - public function __invoke(MultiHandlesForInTheSameHandlerQuery $query): bool - { - return true; - } -} - class MultiHandlersForTheSameMessageQuery {} class MultiHandlersForTheSameMessageHandler1 { @@ -99,15 +48,9 @@ public function __invoke() { assertType(RegularQueryResult::class, $this->handle(new RegularQuery())); - assertType('bool', $this->handle(new BooleanQuery())); - assertType('int', $this->handle(new IntQuery())); - assertType('float', $this->handle(new FloatQuery())); - assertType('string', $this->handle(new StringQuery())); - assertType(TaggedResult::class, $this->handle(new TaggedQuery())); // HandleTrait will throw exception in fact due to multiple handle methods/handlers per single query - assertType('mixed', $this->handle(new MultiHandlesForInTheSameHandlerQuery())); assertType('mixed', $this->handle(new MultiHandlersForTheSameMessageQuery())); } } diff --git a/tests/Type/Symfony/data/messenger_handle_trait_with_subscriber.php b/tests/Type/Symfony/data/messenger_handle_trait_with_subscriber.php new file mode 100644 index 00000000..03456231 --- /dev/null +++ b/tests/Type/Symfony/data/messenger_handle_trait_with_subscriber.php @@ -0,0 +1,72 @@ + ['method' => 'handleInt']; + yield FloatQuery::class => ['method' => 'handleFloat']; + yield StringQuery::class => ['method' => 'handleString']; + } + + public function __invoke(BooleanQuery $query): bool + { + return true; + } + + public function handleInt(IntQuery $query): int + { + return 0; + } + + public function handleFloat(FloatQuery $query): float + { + return 0.0; + } + + public function handleString(StringQuery $query): string + { + return 'string result'; + } +} + +class MultiHandlesForInTheSameHandlerQuery {} +class MultiHandlesForInTheSameHandler implements MessageSubscriberInterface +{ + public static function getHandledMessages(): iterable + { + yield MultiHandlesForInTheSameHandlerQuery::class; + yield MultiHandlesForInTheSameHandlerQuery::class => ['priority' => '0']; + } + + public function __invoke(MultiHandlesForInTheSameHandlerQuery $query): bool + { + return true; + } +} + +class HandleTraitClass { + use HandleTrait; + + public function __invoke() + { + assertType('bool', $this->handle(new BooleanQuery())); + assertType('int', $this->handle(new IntQuery())); + assertType('float', $this->handle(new FloatQuery())); + assertType('string', $this->handle(new StringQuery())); + + // HandleTrait will throw exception in fact due to multiple handle methods/handlers per single query + assertType('mixed', $this->handle(new MultiHandlesForInTheSameHandlerQuery())); + } +}