Skip to content

Commit e55f79b

Browse files
committed
minor symfony#20529 [Serializer] Optimize max depth checking (dunglas)
This PR was merged into the 3.1 branch. Discussion ---------- [Serializer] Optimize max depth checking | Q | A | ------------- | --- | Branch? | 3.1 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | n/a | License | MIT | Doc PR | n/a Avoid to call the metadata factory for each attribute when max depth checking is enabled. Prepare the code for the "serialized name" feature (that also requires metadata) in Symfony 3.3. Commits ------- bb3ee76 [Serializer] Optimize max depth checking
2 parents 0a6f127 + bb3ee76 commit e55f79b

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

@@ -289,42 +291,35 @@ private function updateData(array $data, $attribute, $attributeValue)
289291
/**
290292
* Is the max depth reached for the given attribute?
291293
*
292-
* @param string $class
293-
* @param string $attribute
294-
* @param array $context
294+
* @param AttributeMetadataInterface[] $attributesMetadata
295+
* @param string $class
296+
* @param string $attribute
297+
* @param array $context
295298
*
296299
* @return bool
297300
*/
298-
private function isMaxDepthReached($class, $attribute, array &$context)
301+
private function isMaxDepthReached(array $attributesMetadata, $class, $attribute, array &$context)
299302
{
300-
if (!$this->classMetadataFactory || !isset($context[static::ENABLE_MAX_DEPTH])) {
303+
if (
304+
!isset($context[static::ENABLE_MAX_DEPTH]) ||
305+
!isset($attributesMetadata[$attribute]) ||
306+
null === $maxDepth = $attributesMetadata[$attribute]->getMaxDepth()
307+
) {
301308
return false;
302309
}
303310

304-
$classMetadata = $this->classMetadataFactory->getMetadataFor($class);
305-
$attributesMetadata = $classMetadata->getAttributesMetadata();
306-
307-
if (!isset($attributesMetadata[$attribute])) {
308-
return false;
309-
}
311+
$key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute);
312+
if (!isset($context[$key])) {
313+
$context[$key] = 1;
310314

311-
$maxDepth = $attributesMetadata[$attribute]->getMaxDepth();
312-
if (null === $maxDepth) {
313315
return false;
314316
}
315317

316-
$key = sprintf(static::DEPTH_KEY_PATTERN, $class, $attribute);
317-
$keyExist = isset($context[$key]);
318-
319-
if ($keyExist && $context[$key] === $maxDepth) {
318+
if ($context[$key] === $maxDepth) {
320319
return true;
321320
}
322321

323-
if ($keyExist) {
324-
++$context[$key];
325-
} else {
326-
$context[$key] = 1;
327-
}
322+
++$context[$key];
328323

329324
return false;
330325
}

0 commit comments

Comments
 (0)