Skip to content

Commit ac2dd0f

Browse files
author
Bartłomiej Nowak
committed
handle optional subscriber if exist
1 parent 78b6b5a commit ac2dd0f

File tree

4 files changed

+80
-58
lines changed

4 files changed

+80
-58
lines changed

src/Symfony/MessageMapFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function create(): MessageMap
9191
/** @return iterable<string, array<string, string>> */
9292
private function guessHandledMessages(ClassReflection $reflectionClass): iterable
9393
{
94-
if ($reflectionClass->implementsInterface(MessageSubscriberInterface::class)) {
94+
if (class_exists(MessageSubscriberInterface::class) && $reflectionClass->implementsInterface(MessageSubscriberInterface::class)) {
9595
$className = $reflectionClass->getName();
9696

9797
foreach ($className::getHandledMessages() as $index => $value) {

tests/Type/Symfony/ExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class ExtensionTest extends TypeInferenceTestCase
1414
/** @return mixed[] */
1515
public function dataFileAsserts(): iterable
1616
{
17+
if (class_exists('Symfony\Component\Messenger\Handler\MessageSubscriberInterface')) {
18+
// todo temporary check
19+
die('test if case is triggered');
20+
21+
yield from $this->gatherAssertTypes(__DIR__ . '/data/messenger_handle_trait_with_subscriber.php');
22+
}
23+
1724
yield from $this->gatherAssertTypes(__DIR__ . '/data/messenger_handle_trait.php');
1825
yield from $this->gatherAssertTypes(__DIR__ . '/data/envelope_all.php');
1926
yield from $this->gatherAssertTypes(__DIR__ . '/data/header_bag_get.php');

tests/Type/Symfony/data/messenger_handle_trait.php

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MessengerHandleTrait;
44

5-
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
65
use Symfony\Component\Messenger\HandleTrait;
76
use function PHPStan\Testing\assertType;
87

@@ -16,41 +15,6 @@ public function __invoke(RegularQuery $query): RegularQueryResult
1615
}
1716
}
1817

19-
class BooleanQuery {}
20-
class StringQuery {}
21-
class IntQuery {}
22-
class FloatQuery {}
23-
class MultiQueryHandler implements MessageSubscriberInterface
24-
{
25-
public static function getHandledMessages(): iterable
26-
{
27-
yield BooleanQuery::class;
28-
yield IntQuery::class => ['method' => 'handleInt'];
29-
yield FloatQuery::class => ['method' => 'handleFloat'];
30-
yield StringQuery::class => ['method' => 'handleString'];
31-
}
32-
33-
public function __invoke(BooleanQuery $query): bool
34-
{
35-
return true;
36-
}
37-
38-
public function handleInt(IntQuery $query): int
39-
{
40-
return 0;
41-
}
42-
43-
public function handleFloat(FloatQuery $query): float
44-
{
45-
return 0.0;
46-
}
47-
48-
public function handleString(StringQuery $query): string
49-
{
50-
return 'string result';
51-
}
52-
}
53-
5418
class TaggedQuery {}
5519
class TaggedResult {}
5620
class TaggedHandler
@@ -61,21 +25,6 @@ public function handle(TaggedQuery $query): TaggedResult
6125
}
6226
}
6327

64-
class MultiHandlesForInTheSameHandlerQuery {}
65-
class MultiHandlesForInTheSameHandler implements MessageSubscriberInterface
66-
{
67-
public static function getHandledMessages(): iterable
68-
{
69-
yield MultiHandlesForInTheSameHandlerQuery::class;
70-
yield MultiHandlesForInTheSameHandlerQuery::class => ['priority' => '0'];
71-
}
72-
73-
public function __invoke(MultiHandlesForInTheSameHandlerQuery $query): bool
74-
{
75-
return true;
76-
}
77-
}
78-
7928
class MultiHandlersForTheSameMessageQuery {}
8029
class MultiHandlersForTheSameMessageHandler1
8130
{
@@ -99,15 +48,9 @@ public function __invoke()
9948
{
10049
assertType(RegularQueryResult::class, $this->handle(new RegularQuery()));
10150

102-
assertType('bool', $this->handle(new BooleanQuery()));
103-
assertType('int', $this->handle(new IntQuery()));
104-
assertType('float', $this->handle(new FloatQuery()));
105-
assertType('string', $this->handle(new StringQuery()));
106-
10751
assertType(TaggedResult::class, $this->handle(new TaggedQuery()));
10852

10953
// HandleTrait will throw exception in fact due to multiple handle methods/handlers per single query
110-
assertType('mixed', $this->handle(new MultiHandlesForInTheSameHandlerQuery()));
11154
assertType('mixed', $this->handle(new MultiHandlersForTheSameMessageQuery()));
11255
}
11356
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace MessengerHandleTrait;
4+
5+
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
6+
use Symfony\Component\Messenger\HandleTrait;
7+
use function PHPStan\Testing\assertType;
8+
9+
class BooleanQuery {}
10+
class StringQuery {}
11+
class IntQuery {}
12+
class FloatQuery {}
13+
class MultiQueryHandler implements MessageSubscriberInterface
14+
{
15+
public static function getHandledMessages(): iterable
16+
{
17+
yield BooleanQuery::class;
18+
yield IntQuery::class => ['method' => 'handleInt'];
19+
yield FloatQuery::class => ['method' => 'handleFloat'];
20+
yield StringQuery::class => ['method' => 'handleString'];
21+
}
22+
23+
public function __invoke(BooleanQuery $query): bool
24+
{
25+
return true;
26+
}
27+
28+
public function handleInt(IntQuery $query): int
29+
{
30+
return 0;
31+
}
32+
33+
public function handleFloat(FloatQuery $query): float
34+
{
35+
return 0.0;
36+
}
37+
38+
public function handleString(StringQuery $query): string
39+
{
40+
return 'string result';
41+
}
42+
}
43+
44+
class MultiHandlesForInTheSameHandlerQuery {}
45+
class MultiHandlesForInTheSameHandler implements MessageSubscriberInterface
46+
{
47+
public static function getHandledMessages(): iterable
48+
{
49+
yield MultiHandlesForInTheSameHandlerQuery::class;
50+
yield MultiHandlesForInTheSameHandlerQuery::class => ['priority' => '0'];
51+
}
52+
53+
public function __invoke(MultiHandlesForInTheSameHandlerQuery $query): bool
54+
{
55+
return true;
56+
}
57+
}
58+
59+
class HandleTraitClass {
60+
use HandleTrait;
61+
62+
public function __invoke()
63+
{
64+
assertType('bool', $this->handle(new BooleanQuery()));
65+
assertType('int', $this->handle(new IntQuery()));
66+
assertType('float', $this->handle(new FloatQuery()));
67+
assertType('string', $this->handle(new StringQuery()));
68+
69+
// HandleTrait will throw exception in fact due to multiple handle methods/handlers per single query
70+
assertType('mixed', $this->handle(new MultiHandlesForInTheSameHandlerQuery()));
71+
}
72+
}

0 commit comments

Comments
 (0)