3
3
namespace PHPStan \Symfony ;
4
4
5
5
use PHPStan \Reflection \ClassReflection ;
6
- use PHPStan \Reflection \MissingMethodFromReflectionException ;
7
6
use PHPStan \Reflection \ReflectionProvider ;
8
7
use PHPStan \ShouldNotHappenException ;
9
8
use Symfony \Component \Messenger \Handler \MessageSubscriberInterface ;
@@ -47,9 +46,14 @@ public function create(): MessageMap
47
46
continue ;
48
47
}
49
48
49
+ if (!$ this ->reflectionProvider ->hasClass ($ serviceClass )) {
50
+ continue ;
51
+ }
52
+
53
+ $ reflectionClass = $ this ->reflectionProvider ->getClass ($ serviceClass );
54
+
50
55
/** @var array{handles?: class-string, method?: string} $tagAttributes */
51
56
$ tagAttributes = $ tag ->getAttributes ();
52
- $ reflectionClass = $ this ->reflectionProvider ->getClass ($ serviceClass );
53
57
54
58
if (isset ($ tagAttributes ['handles ' ])) {
55
59
$ handles = [$ tagAttributes ['handles ' ] => ['method ' => $ tagAttributes ['method ' ] ?? self ::DEFAULT_HANDLER_METHOD ]];
@@ -58,7 +62,13 @@ public function create(): MessageMap
58
62
}
59
63
60
64
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 );
62
72
63
73
foreach ($ methodReflection ->getVariants () as $ variant ) {
64
74
$ returnTypesMap [$ messageClassName ][] = $ variant ->getReturnType ();
@@ -79,27 +89,33 @@ public function create(): MessageMap
79
89
return new MessageMap ($ messageMap );
80
90
}
81
91
82
- /** @return array<class- string, array<string, string>> */
92
+ /** @return iterable< string, array<string, string>> */
83
93
private function guessHandledMessages (ClassReflection $ reflectionClass ): iterable
84
94
{
85
95
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 ;
91
107
}
92
108
}
93
109
94
110
return ;
95
111
}
96
112
97
- try {
98
- $ methodReflection = $ reflectionClass ->getNativeMethod (self ::DEFAULT_HANDLER_METHOD );
99
- } catch (MissingMethodFromReflectionException $ e ) {
113
+ if (!$ reflectionClass ->hasNativeMethod (self ::DEFAULT_HANDLER_METHOD )) {
100
114
return ;
101
115
}
102
116
117
+ $ methodReflection = $ reflectionClass ->getNativeMethod (self ::DEFAULT_HANDLER_METHOD );
118
+
103
119
$ variants = $ methodReflection ->getVariants ();
104
120
if (count ($ variants ) !== 1 ) {
105
121
return ;
@@ -111,7 +127,6 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
111
127
return ;
112
128
}
113
129
114
- /** @var class-string[] $classNames */
115
130
$ classNames = $ parameters [0 ]->getType ()->getObjectClassNames ();
116
131
117
132
if (count ($ classNames ) !== 1 ) {
@@ -124,10 +139,10 @@ private function guessHandledMessages(ClassReflection $reflectionClass): iterabl
124
139
/**
125
140
* @param mixed $index
126
141
* @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
131
146
*/
132
147
private static function containOptions ($ index , $ value ): bool
133
148
{
0 commit comments