33namespace PHPStan \Symfony ;
44
55use PHPStan \Reflection \ClassReflection ;
6- use PHPStan \Reflection \MissingMethodFromReflectionException ;
76use PHPStan \Reflection \ReflectionProvider ;
87use PHPStan \ShouldNotHappenException ;
98use Symfony \Component \Messenger \Handler \MessageSubscriberInterface ;
@@ -47,9 +46,14 @@ public function create(): MessageMap
4746 continue ;
4847 }
4948
49+ if (!$ this ->reflectionProvider ->hasClass ($ serviceClass )) {
50+ continue ;
51+ }
52+
53+ $ reflectionClass = $ this ->reflectionProvider ->getClass ($ serviceClass );
54+
5055 /** @var array{handles?: class-string, method?: string} $tagAttributes */
5156 $ tagAttributes = $ tag ->getAttributes ();
52- $ reflectionClass = $ this ->reflectionProvider ->getClass ($ serviceClass );
5357
5458 if (isset ($ tagAttributes ['handles ' ])) {
5559 $ handles = [$ tagAttributes ['handles ' ] => ['method ' => $ tagAttributes ['method ' ] ?? self ::DEFAULT_HANDLER_METHOD ]];
@@ -58,7 +62,13 @@ public function create(): MessageMap
5862 }
5963
6064 foreach ($ handles as $ messageClassName => $ options ) {
61- $ methodReflection = $ reflectionClass ->getNativeMethod ($ options ['method ' ] ?? self ::DEFAULT_HANDLER_METHOD );
65+ $ methodName = $ options ['method ' ] ?? self ::DEFAULT_HANDLER_METHOD ;
66+
67+ if (!$ reflectionClass ->hasNativeMethod ($ methodName )) {
68+ continue ;
69+ }
70+
71+ $ methodReflection = $ reflectionClass ->getNativeMethod ($ methodName );
6272
6373 foreach ($ methodReflection ->getVariants () as $ variant ) {
6474 $ returnTypesMap [$ messageClassName ][] = $ variant ->getReturnType ();
@@ -79,27 +89,33 @@ public function create(): MessageMap
7989 return new MessageMap ($ messageMap );
8090 }
8191
82- /** @return array<class- string, array<string, string>> */
92+ /** @return iterable< string, array<string, string>> */
8393 private function guessHandledMessages (ClassReflection $ reflectionClass ): iterable
8494 {
8595 if ($ reflectionClass ->implementsInterface (MessageSubscriberInterface::class)) {
86- foreach ($ reflectionClass ->getName ()::getHandledMessages () as $ index => $ value ) {
87- if (self ::containOptions ($ index , $ value )) {
88- yield $ index => $ value ;
89- } else {
90- yield $ value => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
96+ $ className = $ reflectionClass ->getName ();
97+
98+ foreach ($ className ::getHandledMessages () as $ index => $ value ) {
99+ try {
100+ if (self ::containOptions ($ index , $ value )) {
101+ yield $ index => $ value ;
102+ } else {
103+ yield $ value => ['method ' => self ::DEFAULT_HANDLER_METHOD ];
104+ }
105+ } catch (ShouldNotHappenException $ e ) {
106+ continue ;
91107 }
92108 }
93109
94110 return ;
95111 }
96112
97- try {
98- $ methodReflection = $ reflectionClass ->getNativeMethod (self ::DEFAULT_HANDLER_METHOD );
99- } catch (MissingMethodFromReflectionException $ e ) {
113+ if (!$ reflectionClass ->hasNativeMethod (self ::DEFAULT_HANDLER_METHOD )) {
100114 return ;
101115 }
102116
117+ $ methodReflection = $ reflectionClass ->getNativeMethod (self ::DEFAULT_HANDLER_METHOD );
118+
103119 $ variants = $ methodReflection ->getVariants ();
104120 if (count ($ variants ) !== 1 ) {
105121 return ;
@@ -111,7 +127,6 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
111127 return ;
112128 }
113129
114- /** @var class-string[] $classNames */
115130 $ classNames = $ parameters [0 ]->getType ()->getObjectClassNames ();
116131
117132 if (count ($ classNames ) !== 1 ) {
@@ -124,10 +139,10 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
124139 /**
125140 * @param mixed $index
126141 * @param mixed $value
127- * @phpstan-assert-if-true class-string $index
128- * @phpstan-assert-if-true array<string, mixed> $value
129- * @phpstan-assert-if-false int $index
130- * @phpstan-assert-if-false class-string $value
142+ * @phpstan-assert-if-true = class-string $index
143+ * @phpstan-assert-if-true = array<string, mixed> $value
144+ * @phpstan-assert-if-false = int $index
145+ * @phpstan-assert-if-false = class-string $value
131146 */
132147 private static function containOptions ($ index , $ value ): bool
133148 {
0 commit comments