5
5
use PHPStan \Reflection \ClassReflection ;
6
6
use PHPStan \Reflection \MissingMethodFromReflectionException ;
7
7
use PHPStan \Reflection \ReflectionProvider ;
8
+ use PHPStan \ShouldNotHappenException ;
8
9
use Symfony \Component \Messenger \Handler \MessageSubscriberInterface ;
10
+ use function class_exists ;
9
11
use function count ;
12
+ use function is_array ;
10
13
use function is_int ;
11
- use function is_null ;
12
14
use function is_string ;
13
15
14
16
final class MessageMapFactory
@@ -36,7 +38,7 @@ public function create(): MessageMap
36
38
foreach ($ this ->serviceMap ->getServices () as $ service ) {
37
39
$ serviceClass = $ service ->getClass ();
38
40
39
- if (is_null ( $ serviceClass) ) {
41
+ if ($ serviceClass === null ) {
40
42
continue ;
41
43
}
42
44
@@ -45,6 +47,7 @@ public function create(): MessageMap
45
47
continue ;
46
48
}
47
49
50
+ /** @var array{handles?: class-string, method?: string} $tagAttributes */
48
51
$ tagAttributes = $ tag ->getAttributes ();
49
52
$ reflectionClass = $ this ->reflectionProvider ->getClass ($ serviceClass );
50
53
@@ -76,15 +79,15 @@ public function create(): MessageMap
76
79
return new MessageMap ($ messageMap );
77
80
}
78
81
79
- /** @return array<string, array<string, string>> */
82
+ /** @return array<class- string, array<string, string>> */
80
83
private function guessHandledMessages (ClassReflection $ reflectionClass ): iterable
81
84
{
82
85
if ($ reflectionClass ->implementsInterface (MessageSubscriberInterface::class)) {
83
86
foreach ($ reflectionClass ->getName ()::getHandledMessages () as $ index => $ value ) {
84
- if (is_int ($ index ) && is_string ($ value )) {
85
- yield $ value => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
86
- } else {
87
+ if (self ::containOptions ($ index , $ value )) {
87
88
yield $ index => $ value ;
89
+ } else {
90
+ yield $ value => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
88
91
}
89
92
}
90
93
@@ -108,6 +111,7 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
108
111
return ;
109
112
}
110
113
114
+ /** @var class-string[] $classNames */
111
115
$ classNames = $ parameters [0 ]->getType ()->getObjectClassNames ();
112
116
113
117
if (count ($ classNames ) !== 1 ) {
@@ -117,4 +121,21 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
117
121
yield $ classNames [0 ] => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
118
122
}
119
123
124
+ /**
125
+ * @phpstan-assert-if-true class-string $index
126
+ * @phpstan-assert-if-true array<string, mixed> $value
127
+ * @phpstan-assert-if-false int $index
128
+ * @phpstan-assert-if-false class-string $value
129
+ */
130
+ private static function containOptions (mixed $ index , mixed $ value ): bool
131
+ {
132
+ if (is_string ($ index ) && class_exists ($ index ) && is_array ($ value )) {
133
+ return true ;
134
+ } elseif (is_int ($ index ) && is_string ($ value ) && class_exists ($ value )) {
135
+ return false ;
136
+ }
137
+
138
+ throw new ShouldNotHappenException ();
139
+ }
140
+
120
141
}
0 commit comments