55use PHPStan \Reflection \ClassReflection ;
66use PHPStan \Reflection \MissingMethodFromReflectionException ;
77use PHPStan \Reflection \ReflectionProvider ;
8+ use PHPStan \ShouldNotHappenException ;
89use Symfony \Component \Messenger \Handler \MessageSubscriberInterface ;
10+ use function class_exists ;
911use function count ;
12+ use function is_array ;
1013use function is_int ;
11- use function is_null ;
1214use function is_string ;
1315
1416final class MessageMapFactory
@@ -36,7 +38,7 @@ public function create(): MessageMap
3638 foreach ($ this ->serviceMap ->getServices () as $ service ) {
3739 $ serviceClass = $ service ->getClass ();
3840
39- if (is_null ( $ serviceClass) ) {
41+ if ($ serviceClass === null ) {
4042 continue ;
4143 }
4244
@@ -45,6 +47,7 @@ public function create(): MessageMap
4547 continue ;
4648 }
4749
50+ /** @var array{handles?: class-string, method?: string} $tagAttributes */
4851 $ tagAttributes = $ tag ->getAttributes ();
4952 $ reflectionClass = $ this ->reflectionProvider ->getClass ($ serviceClass );
5053
@@ -76,15 +79,15 @@ public function create(): MessageMap
7679 return new MessageMap ($ messageMap );
7780 }
7881
79- /** @return array<string, array<string, string>> */
82+ /** @return array<class- string, array<string, string>> */
8083 private function guessHandledMessages (ClassReflection $ reflectionClass ): iterable
8184 {
8285 if ($ reflectionClass ->implementsInterface (MessageSubscriberInterface::class)) {
8386 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 )) {
8788 yield $ index => $ value ;
89+ } else {
90+ yield $ value => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
8891 }
8992 }
9093
@@ -108,6 +111,7 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
108111 return ;
109112 }
110113
114+ /** @var class-string[] $classNames */
111115 $ classNames = $ parameters [0 ]->getType ()->getObjectClassNames ();
112116
113117 if (count ($ classNames ) !== 1 ) {
@@ -117,4 +121,21 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
117121 yield $ classNames [0 ] => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
118122 }
119123
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+
120141}
0 commit comments