Skip to content

Commit

Permalink
[Serializer] AbstractObjectNormalizer improve perf when checking types
Browse files Browse the repository at this point in the history
  • Loading branch information
martiis committed Aug 26, 2018
1 parent ff1727e commit 4224145
Showing 1 changed file with 11 additions and 3 deletions.
Expand Up @@ -37,6 +37,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
const DISABLE_TYPE_ENFORCEMENT = 'disable_type_enforcement';

private $propertyTypeExtractor;
private $typesCache = array();
private $attributesCache = array();

/**
Expand Down Expand Up @@ -366,24 +367,31 @@ private function getTypes(string $currentClass, string $attribute)
return null;
}

$key = $currentClass.'::'.$attribute;
if (isset($this->typesCache[$key])) {
return false === $this->typesCache[$key] ? null : $this->typesCache[$key];
}

if (null !== $types = $this->propertyTypeExtractor->getTypes($currentClass, $attribute)) {
return $types;
return $this->typesCache[$key] = $types;
}

if (null !== $this->classDiscriminatorResolver && null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($currentClass)) {
if ($discriminatorMapping->getTypeProperty() === $attribute) {
return array(
return $this->typesCache[$key] = array(
new Type(Type::BUILTIN_TYPE_STRING),
);
}

foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
if (null !== $types = $this->propertyTypeExtractor->getTypes($mappedClass, $attribute)) {
return $types;
return $this->typesCache[$key] = $types;
}
}
}

$this->typesCache[$key] = false;

return null;
}

Expand Down

0 comments on commit 4224145

Please sign in to comment.