Skip to content

Commit 720d6f2

Browse files
joelwurtzKorbeil
authored andcommitted
Remove unsetting data, simplify block
1 parent 45ffde2 commit 720d6f2

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

src/Symfony/Component/Serializer/Instantiator/Instantiator.php

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,48 @@ public function instantiate(string $class, $data, $format = null, array $context
4949
$reflectionClass = new \ReflectionClass($class);
5050
$constructor = $reflectionClass->getConstructor();
5151

52-
if ($constructor) {
53-
$constructorParameters = $constructor->getParameters();
54-
55-
$params = [];
56-
foreach ($constructorParameters as $constructorParameter) {
57-
$paramName = $constructorParameter->name;
58-
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
59-
$allowed = null === $allowedAttributes || \in_array($paramName, $allowedAttributes, true);
60-
61-
if ($allowed && $constructorParameter->isVariadic() && \array_key_exists($key, $data)) {
62-
if (!\is_array($data[$paramName])) {
63-
throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
64-
}
65-
66-
$params = array_merge($params, $data[$paramName]);
67-
} elseif ($allowed && \array_key_exists($key, $data)) {
68-
$parameterData = $data[$key];
69-
if (null === $parameterData && $constructorParameter->allowsNull()) {
70-
$params[] = null;
71-
// Don't run set for a parameter passed to the constructor
72-
unset($data[$key]);
73-
continue;
74-
}
75-
76-
// Don't run set for a parameter passed to the constructor
77-
$params[] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
78-
unset($data[$key]);
79-
} elseif (\array_key_exists($key, $context[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
80-
$params[] = $context[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
81-
} elseif ($constructorParameter->isDefaultValueAvailable()) {
82-
$params[] = $constructorParameter->getDefaultValue();
83-
} else {
84-
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
52+
if (null === $constructor) {
53+
return new $class();
54+
}
55+
56+
$constructorParameters = $constructor->getParameters();
57+
58+
$params = [];
59+
foreach ($constructorParameters as $constructorParameter) {
60+
$paramName = $constructorParameter->name;
61+
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
62+
$allowed = null === $allowedAttributes || \in_array($paramName, $allowedAttributes, true);
63+
64+
if ($allowed && $constructorParameter->isVariadic() && \array_key_exists($key, $data)) {
65+
if (!\is_array($data[$paramName])) {
66+
throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
8567
}
86-
}
8768

88-
if ($constructor->isConstructor()) {
89-
return $reflectionClass->newInstanceArgs($params);
69+
$params = array_merge($params, $data[$paramName]);
70+
} elseif ($allowed && \array_key_exists($key, $data)) {
71+
$parameterData = $data[$key];
72+
73+
if (null === $parameterData && $constructorParameter->allowsNull()) {
74+
$params[] = null;
75+
76+
continue;
77+
}
78+
79+
$params[] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $context, $format);
80+
} elseif (\array_key_exists($key, $context[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
81+
$params[] = $context[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
82+
} elseif ($constructorParameter->isDefaultValueAvailable()) {
83+
$params[] = $constructorParameter->getDefaultValue();
84+
} else {
85+
throw new MissingConstructorArgumentsException(sprintf('Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.', $class, $constructorParameter->name));
9086
}
87+
}
9188

92-
return $constructor->invokeArgs(null, $params);
89+
if ($constructor->isConstructor()) {
90+
return $reflectionClass->newInstanceArgs($params);
9391
}
9492

95-
return new $class();
93+
return $constructor->invokeArgs(null, $params);
9694
}
9795

9896
private function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)

0 commit comments

Comments
 (0)