19
19
use ReflectionClass ;
20
20
use ReflectionException ;
21
21
use ReflectionFunction ;
22
+ use ReflectionObject ;
22
23
use ReflectionProperty ;
23
24
use ReflectionZendExtension ;
25
+ use function extension_loaded ;
24
26
use function is_a ;
25
27
26
28
class ReflectionExtension implements DynamicConstructorThrowTypeExtension
@@ -45,6 +47,10 @@ public function getThrowTypeFromConstructor(MethodReflection $methodReflection,
45
47
{
46
48
$ className = $ methodReflection ->getDeclaringClass ()->getName ();
47
49
50
+ if (is_a ($ className , ReflectionObject::class, true )) {
51
+ return new VoidType ();
52
+ }
53
+
48
54
if (is_a ($ className , ReflectionClass::class, true )) {
49
55
return $ this ->resolveReflectionClass ($ newNode , $ scope );
50
56
}
@@ -58,7 +64,7 @@ public function getThrowTypeFromConstructor(MethodReflection $methodReflection,
58
64
}
59
65
60
66
if (is_a ($ className , ReflectionZendExtension::class, true )) {
61
- return $ this ->resolveReflectionClass ($ newNode , $ scope );
67
+ return $ this ->resolveReflectionExtension ($ newNode , $ scope );
62
68
}
63
69
64
70
throw new UnsupportedClassException ();
@@ -153,4 +159,28 @@ private function resolveReflectionProperty(New_ $newNode, Scope $scope): Type
153
159
return new VoidType ();
154
160
}
155
161
162
+ private function resolveReflectionExtension (New_ $ newNode , Scope $ scope ): Type
163
+ {
164
+ $ reflectionExceptionType = new ObjectType (ReflectionException::class);
165
+ if (!isset ($ newNode ->args [0 ])) {
166
+ return $ reflectionExceptionType ;
167
+ }
168
+
169
+ $ valueType = $ scope ->getType ($ newNode ->args [0 ]->value );
170
+
171
+ foreach (TypeUtils::getConstantStrings ($ valueType ) as $ constantString ) {
172
+ if (!extension_loaded ($ constantString ->getValue ())) {
173
+ return $ reflectionExceptionType ;
174
+ }
175
+
176
+ $ valueType = TypeCombinator::remove ($ valueType , $ constantString );
177
+ }
178
+
179
+ if (!$ valueType instanceof NeverType) {
180
+ return $ reflectionExceptionType ;
181
+ }
182
+
183
+ return new VoidType ();
184
+ }
185
+
156
186
}
0 commit comments