@@ -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,18 @@ 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 ;
318
-
319
305
try {
320
306
$ reflectionClass = new \ReflectionClass ($ class );
321
307
322
308
if (\PHP_VERSION_ID >= 70400 ) {
323
- $ type = $ reflectionClass ->getProperty ($ property )->getType ();
309
+ $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
310
+ $ reflectionPropertyType = $ reflectionProperty ->getType ();
324
311
325
- $ isNullable = null !== $ type && $ type ->allowsNull ();
312
+ if (null !== $ reflectionPropertyType && $ types = $ this ->extractFromReflectionType ($ reflectionPropertyType , $ reflectionProperty ->getDeclaringClass ())) {
313
+ return $ types ;
314
+ }
326
315
}
327
316
} catch (\ReflectionException $ e ) {
328
317
return null ;
@@ -336,7 +325,7 @@ private function extractFromDefaultValue(string $class, string $property): ?arra
336
325
337
326
$ type = \gettype ($ defaultValue );
338
327
339
- return [new Type (static ::MAP_TYPES [$ type ] ?? $ type , $ isNullable )];
328
+ return [new Type (static ::MAP_TYPES [$ type ] ?? $ type , $ this -> isNullableProperty ( $ class , $ property ) )];
340
329
}
341
330
342
331
private function extractFromReflectionType (\ReflectionType $ reflectionType , \ReflectionClass $ declaringClass ): array
@@ -376,6 +365,25 @@ private function resolveTypeName(string $name, \ReflectionClass $declaringClass)
376
365
return $ name ;
377
366
}
378
367
368
+ private function isNullableProperty (string $ class , string $ property ): bool
369
+ {
370
+ try {
371
+ $ reflectionProperty = new \ReflectionProperty ($ class , $ property );
372
+
373
+ if (\PHP_VERSION_ID >= 70400 ) {
374
+ $ reflectionPropertyType = $ reflectionProperty ->getType ();
375
+
376
+ return null !== $ reflectionPropertyType && $ reflectionPropertyType ->allowsNull ();
377
+ }
378
+
379
+ return false ;
380
+ } catch (\ReflectionException $ e ) {
381
+ // Return false if the property doesn't exist
382
+ }
383
+
384
+ return false ;
385
+ }
386
+
379
387
private function isAllowedProperty (string $ class , string $ property ): bool
380
388
{
381
389
try {
0 commit comments