Skip to content

Commit a47a515

Browse files
committed
Merge branch '3.1' into 3.2
* 3.1: Write an exception message in a one heading line [Finder] Refine phpdoc about argument for NumberComparator Fixed max width from ajax request url element (td) Fix unresolved parameters from default bundle configs in debug:config [github] Tweak PR template [Serializer] Optimize max depth checking
2 parents c629e2b + f4b3093 commit a47a515

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

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)