@@ -133,6 +133,18 @@ public function getProperties(string $class, array $context = []): ?array
133
133
*/
134
134
public function getTypes (string $ class , string $ property , array $ context = []): ?array
135
135
{
136
+ if (\PHP_VERSION_ID >= 70400 ) {
137
+ try {
138
+ $ reflectionProperty = new \ReflectionProperty ($ class , $ property );
139
+ $ type = $ reflectionProperty ->getType ();
140
+ if (null !== $ type ) {
141
+ return [$ this ->extractFromReflectionType ($ type , $ reflectionProperty ->getDeclaringClass ())];
142
+ }
143
+ } catch (\ReflectionException $ e ) {
144
+ // noop
145
+ }
146
+ }
147
+
136
148
if ($ fromMutator = $ this ->extractFromMutator ($ class , $ property )) {
137
149
return $ fromMutator ;
138
150
}
@@ -227,7 +239,7 @@ private function extractFromMutator(string $class, string $property): ?array
227
239
if (!$ reflectionType = $ reflectionParameter ->getType ()) {
228
240
return null ;
229
241
}
230
- $ type = $ this ->extractFromReflectionType ($ reflectionType , $ reflectionMethod );
242
+ $ type = $ this ->extractFromReflectionType ($ reflectionType , $ reflectionMethod-> getDeclaringClass () );
231
243
232
244
if (\in_array ($ prefix , $ this ->arrayMutatorPrefixes )) {
233
245
$ type = new Type (Type::BUILTIN_TYPE_ARRAY , false , null , true , new Type (Type::BUILTIN_TYPE_INT ), $ type );
@@ -249,7 +261,7 @@ private function extractFromAccessor(string $class, string $property): ?array
249
261
}
250
262
251
263
if ($ reflectionType = $ reflectionMethod ->getReturnType ()) {
252
- return [$ this ->extractFromReflectionType ($ reflectionType , $ reflectionMethod )];
264
+ return [$ this ->extractFromReflectionType ($ reflectionType , $ reflectionMethod-> getDeclaringClass () )];
253
265
}
254
266
255
267
if (\in_array ($ prefix , ['is ' , 'can ' , 'has ' ])) {
@@ -284,7 +296,7 @@ private function extractFromConstructor(string $class, string $property): ?array
284
296
}
285
297
$ reflectionType = $ parameter ->getType ();
286
298
287
- return $ reflectionType ? [$ this ->extractFromReflectionType ($ reflectionType , $ constructor )] : null ;
299
+ return $ reflectionType ? [$ this ->extractFromReflectionType ($ reflectionType , $ constructor-> getDeclaringClass () )] : null ;
288
300
}
289
301
290
302
if ($ parentClass = $ reflectionClass ->getParentClass ()) {
@@ -313,7 +325,7 @@ private function extractFromDefaultValue(string $class, string $property): ?arra
313
325
return [new Type (static ::MAP_TYPES [$ type ] ?? $ type )];
314
326
}
315
327
316
- private function extractFromReflectionType (\ReflectionType $ reflectionType , \ReflectionMethod $ reflectionMethod ): Type
328
+ private function extractFromReflectionType (\ReflectionType $ reflectionType , \ReflectionClass $ declaringClass ): Type
317
329
{
318
330
$ phpTypeOrClass = $ reflectionType ->getName ();
319
331
$ nullable = $ reflectionType ->allowsNull ();
@@ -325,18 +337,18 @@ private function extractFromReflectionType(\ReflectionType $reflectionType, \Ref
325
337
} elseif ($ reflectionType ->isBuiltin ()) {
326
338
$ type = new Type ($ phpTypeOrClass , $ nullable );
327
339
} else {
328
- $ type = new Type (Type::BUILTIN_TYPE_OBJECT , $ nullable , $ this ->resolveTypeName ($ phpTypeOrClass , $ reflectionMethod ));
340
+ $ type = new Type (Type::BUILTIN_TYPE_OBJECT , $ nullable , $ this ->resolveTypeName ($ phpTypeOrClass , $ declaringClass ));
329
341
}
330
342
331
343
return $ type ;
332
344
}
333
345
334
- private function resolveTypeName (string $ name , \ReflectionMethod $ reflectionMethod ): string
346
+ private function resolveTypeName (string $ name , \ReflectionClass $ declaringClass ): string
335
347
{
336
348
if ('self ' === $ lcName = strtolower ($ name )) {
337
- return $ reflectionMethod -> getDeclaringClass () ->name ;
349
+ return $ declaringClass ->name ;
338
350
}
339
- if ('parent ' === $ lcName && $ parent = $ reflectionMethod -> getDeclaringClass () ->getParentClass ()) {
351
+ if ('parent ' === $ lcName && $ parent = $ declaringClass ->getParentClass ()) {
340
352
return $ parent ->name ;
341
353
}
342
354
0 commit comments