@@ -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,10 +300,19 @@ 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
305
try {
318
306
$ reflectionClass = new \ReflectionClass ($ class );
307
+
308
+ if (\PHP_VERSION_ID >= 70400 ) {
309
+ $ reflectionProperty = $ reflectionClass ->getProperty ($ property );
310
+ $ reflectionPropertyType = $ reflectionProperty ->getType ();
311
+
312
+ if (null !== $ reflectionPropertyType && $ types = $ this ->extractFromReflectionType ($ reflectionPropertyType , $ reflectionProperty ->getDeclaringClass ())) {
313
+ return $ types ;
314
+ }
315
+ }
319
316
} catch (\ReflectionException $ e ) {
320
317
return null ;
321
318
}
@@ -328,7 +325,7 @@ private function extractFromDefaultValue(string $class, string $property): ?arra
328
325
329
326
$ type = \gettype ($ defaultValue );
330
327
331
- return [new Type (static ::MAP_TYPES [$ type ] ?? $ type )];
328
+ return [new Type (static ::MAP_TYPES [$ type ] ?? $ type, $ this -> isNullableProperty ( $ class , $ property ) )];
332
329
}
333
330
334
331
private function extractFromReflectionType (\ReflectionType $ reflectionType , \ReflectionClass $ declaringClass ): array
@@ -368,6 +365,25 @@ private function resolveTypeName(string $name, \ReflectionClass $declaringClass)
368
365
return $ name ;
369
366
}
370
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
+
371
387
private function isAllowedProperty (string $ class , string $ property ): bool
372
388
{
373
389
try {
0 commit comments