handle finding returnType on property type on parent classes

This commit is contained in:
Julien Fastré 2021-12-01 15:39:44 +01:00
parent de4e83b3fb
commit 9f43c99acc

View File

@ -86,19 +86,34 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
private function getExpectedType(AttributeMetadata $attribute, ReflectionClass $reflection): string private function getExpectedType(AttributeMetadata $attribute, ReflectionClass $reflection): string
{ {
// we have to get the expected content $type = null;
if ($reflection->hasProperty($attribute->getName())) {
$type = $reflection->getProperty($attribute->getName())->getType();
} elseif ($reflection->hasMethod($attribute->getName())) {
$type = $reflection->getMethod($attribute->getName())->getReturnType();
} else {
throw new \LogicException(sprintf(
'Could not determine how the content is determined for the attribute %s. Add attribute property only on property or method',
$attribute->getName()
));
}
if (null === $type) { do {
// we have to get the expected content
if ($reflection->hasProperty($attribute->getName())) {
if (!$reflection->getProperty($attribute->getName())->hasType()) {
throw new \LogicException(sprintf(
'Could not determine how the content is determined for the attribute %s. Add a type on this property',
$attribute->getName()
));
}
$type = $reflection->getProperty($attribute->getName())->getType();
} elseif ($reflection->hasMethod($attribute->getName())) {
if (!$reflection->getMethod($attribute->getName())->hasReturnType()) {
throw new \LogicException(sprintf(
'Could not determine how the content is determined for the attribute %s. Add a return type on the method',
$attribute->getName()
));
}
$type = $reflection->getMethod($attribute->getName())->getReturnType();
} else {
$reflection = $reflection->getParentClass();
}
} while (null === $type && $reflection instanceof ReflectionClass);
if (null === $type ?? null) {
throw new \LogicException(sprintf( throw new \LogicException(sprintf(
'Could not determine the type for this attribute: %s. Add a return type to the method or property declaration', 'Could not determine the type for this attribute: %s. Add a return type to the method or property declaration',
$attribute->getName() $attribute->getName()