Skip to content

Commit 5c46664

Browse files
committed
extract type from property in seperate method
1 parent 4251050 commit 5c46664

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/ImmutableRecordLogic.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,24 +277,38 @@ private static function buildPropTypeMap()
277277
continue;
278278
}
279279

280-
if (! $prop->hasType()) {
281-
throw new \RuntimeException(
282-
\sprintf(
283-
'Missing type hint for property %s of record %s',
284-
$prop->getName(),
285-
__CLASS__
286-
)
287-
);
288-
}
289-
290-
$type = $prop->getType();
280+
$type = self::typeFromProperty($prop, $refObj);
291281

292282
$propTypeMap[$prop->getName()] = [$type->getName(), self::isScalarType($type->getName()), $type->allowsNull()];
293283
}
294284

295285
return $propTypeMap;
296286
}
297287

288+
private static function typeFromProperty(\ReflectionProperty $prop, \ReflectionClass $refObj) : \ReflectionType
289+
{
290+
if ($prop->hasType()) {
291+
return $prop->getType();
292+
}
293+
294+
if ($refObj->hasMethod($prop->getName())) {
295+
$method = $refObj->getMethod($prop->getName());
296+
297+
if ($method->hasReturnType()) {
298+
return $method->getReturnType();
299+
}
300+
}
301+
302+
throw new \RuntimeException(
303+
\sprintf(
304+
'Could not find a type for property %s of record %s. ' .
305+
'No type hint or getter method with a return type is given.',
306+
$prop->getName(),
307+
__CLASS__
308+
)
309+
);
310+
}
311+
298312
private static function isScalarType(string $type): bool
299313
{
300314
switch ($type) {

0 commit comments

Comments
 (0)