Skip to content

Commit bb3ee76

Browse files
committed
[Serializer] Optimize max depth checking
1 parent a813cb7 commit bb3ee76

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1818
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1919
use Symfony\Component\PropertyInfo\Type;
20+
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
2021
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2122
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
2223

@@ -67,9 +68,10 @@ public function normalize($object, $format = null, array $context = array())
6768
$stack = array();
6869
$attributes = $this->getAttributes($object, $format, $context);
6970
$class = get_class($object);
71+
$attributesMetadata = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null;
7072

7173
foreach ($attributes as $attribute) {
72-
if ($this->isMaxDepthReached($class, $attribute, $context)) {
74+
if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $attribute, $context)) {
7375
continue;
7476
}
7577

@@ -303,42 +305,35 @@ private function updateData(array $data, $attribute, $attributeValue)
303305
/**
304306
* Is the max depth reached for the given attribute?
305307
*
306-
* @param string $class
307-
* @param string $attribute
308-
* @param array $context
308+
* @param AttributeMetadataInterface[] $attributesMetadata
309+
* @param string $class
310+
* @param string $attribute
311+
* @param array $context
309312
*
310313
* @return bool
311314
*/
312-
private function isMaxDepthReached($class, $attribute, array &$context)
315+
private function isMaxDepthReached(array $attributesMetadata, $class, $attribute, array &$context)
313316
{
314-
if (!$this->classMetadataFactory || !isset($context[static::ENABLE_MAX_DEPTH])) {
317+
if (
318+
!isset($context[static::ENABLE_MAX_DEPTH]) ||
319+
!isset($attributesMetadata[$attribute]) ||
320+
null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
321+
) {
315322
return false;
316323
}
317324

318-
$classMetadata = $this->classMetadataFactory->getMetadataFor($class);
319-
$attributesMetadata = $classMetadata->getAttributesMetadata();
320-
321-
if (!isset($attributesMetadata[$attribute])) {
322-
return false;
323-
}
325+
$key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute);
326+
if (!isset($context[$key])) {
327+
$context[$key] = 1;
324328

325-
$maxDepth = $attributesMetadata[$attribute]->getMaxDepth();
326-
if (null === $maxDepth) {
327329
return false;
328330
}
329331

330-
$key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute);
331-
$keyExist = isset($context[$key]);
332-
333-
if ($keyExist && $context[$key] === $maxDepth) {
332+
if ($context[$key] === $maxDepth) {
334333
return true;
335334
}
336335

337-
if ($keyExist) {
338-
++$context[$key];
339-
} else {
340-
$context[$key] = 1;
341-
}
336+
++$context[$key];
342337

343338
return false;
344339
}

0 commit comments

Comments
 (0)