@@ -154,20 +154,8 @@ public function getTypes($class, $property, array $context = []): ?array
154
154
return $ fromConstructor ;
155
155
}
156
156
157
- if ($ fromDefaultValue = $ this ->extractFromDefaultValue ($ class , $ property )) {
158
- return $ fromDefaultValue ;
159
- }
160
-
161
- if (\PHP_VERSION_ID >= 70400 ) {
162
- try {
163
- $ reflectionProperty = new \ReflectionProperty ($ class , $ property );
164
- $ type = $ reflectionProperty ->getType ();
165
- if (null !== $ type && $ types = $ this ->extractFromReflectionType ($ type , $ reflectionProperty ->getDeclaringClass ())) {
166
- return $ types ;
167
- }
168
- } catch (\ReflectionException $ e ) {
169
- // noop
170
- }
157
+ if ($ fromPropertyDeclaration = $ this ->extractFromPropertyDeclaration ($ class , $ property )) {
158
+ return $ fromPropertyDeclaration ;
171
159
}
172
160
173
161
return null ;
@@ -312,17 +300,20 @@ private function extractFromConstructor(string $class, string $property): ?array
312
300
return null ;
313
301
}
314
302
315
- private function extractFromDefaultValue (string $ class , string $ property ): ?array
303
+ private function extractFromPropertyDeclaration (string $ class , string $ property ): ?array
316
304
{
317
- $ isNullable = false ;
305
+ $ isNullable = $ this -> isNullableProperty ( $ class , $ property ) ;
318
306
319
307
try {
320
308
$ reflectionClass = new \ReflectionClass ($ class );
321
309
322
310
if (\PHP_VERSION_ID >= 70400 ) {
323
- $ type = $ reflectionClass ->getProperty ($ property )->getType ();
311
+ $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
312
+ $ reflectionPropertyType = $ reflectionProperty ->getType ();
324
313
325
- $ isNullable = null !== $ type && $ type ->allowsNull ();
314
+ if (null !== $ reflectionPropertyType && $ types = $ this ->extractFromReflectionType ($ reflectionPropertyType , $ reflectionProperty ->getDeclaringClass ())) {
315
+ return $ types ;
316
+ }
326
317
}
327
318
} catch (\ReflectionException $ e ) {
328
319
return null ;
@@ -376,6 +367,25 @@ private function resolveTypeName(string $name, \ReflectionClass $declaringClass)
376
367
return $ name ;
377
368
}
378
369
370
+ private function isNullableProperty (string $ class , string $ property ): bool
371
+ {
372
+ try {
373
+ $ reflectionProperty = new \ReflectionProperty ($ class , $ property );
374
+
375
+ if (\PHP_VERSION_ID >= 70400 ) {
376
+ $ reflectionPropertyType = $ reflectionProperty ->getType ();
377
+
378
+ return null !== $ reflectionPropertyType && $ reflectionPropertyType ->allowsNull ();
379
+ }
380
+
381
+ return false ;
382
+ } catch (\ReflectionException $ e ) {
383
+ // Return false if the property doesn't exist
384
+ }
385
+
386
+ return false ;
387
+ }
388
+
379
389
private function isAllowedProperty (string $ class , string $ property ): bool
380
390
{
381
391
try {
0 commit comments