Skip to content

Commit

Permalink
-refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexndlm committed Oct 29, 2024
1 parent eca40f9 commit b9e9656
Showing 1 changed file with 13 additions and 64 deletions.
77 changes: 13 additions & 64 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,27 +304,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex

return $object;
}
// clean up even if no match
unset($context[static::OBJECT_TO_POPULATE]);

$class = $this->getClassDiscriminatorResolvedClass($data, $class, $context);
$reflectionClass = new \ReflectionClass($class);

$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
if ($constructor) {
$context['has_constructor'] = true;
if (true !== $constructor->isPublic()) {
return $reflectionClass->newInstanceWithoutConstructor();
}

$constructorParameters = $constructor->getParameters();
$missingConstructorArguments = [];
$params = [];
$unsetKeys = [];

$params = [];
$missingConstructorArguments = [];
foreach ($constructorParameters as $constructorParameter) {
$paramName = $constructorParameter->name;
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;

$allowed = false === $allowedAttributes || (\is_array($allowedAttributes) && \in_array($paramName, $allowedAttributes, true));
Expand All @@ -335,22 +326,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
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));
}

$variadicParameters = [];
foreach ($data[$key] as $parameterKey => $parameterData) {
try {
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
} catch (NotNormalizableValueException $exception) {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw $exception;
}

$context['not_normalizable_value_exceptions'][] = $exception;
$params[$paramName] = $parameterData;
}
}

$params = array_merge(array_values($params), $variadicParameters);
$unsetKeys[] = $key;
$params[] = $data[$paramName];
}
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
$constructorContext = $context;
Expand All @@ -365,21 +341,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
}

// Don't run set for a parameter passed to the constructor
$unsetKeys[] = $key;
} elseif (\array_key_exists($key, $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
$params[$paramName] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif (\array_key_exists($key, $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
$params[$paramName] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
unset($data[$key]);
} elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
$params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
} elseif ($constructorParameter->isDefaultValueAvailable()) {
$params[$paramName] = $constructorParameter->getDefaultValue();
} elseif (!($context[self::REQUIRE_ALL_PROPERTIES] ?? $this->defaultContext[self::REQUIRE_ALL_PROPERTIES] ?? false) && $constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
$params[$paramName] = null;
$params[] = $constructorParameter->getDefaultValue();
} else {
if (!isset($context['not_normalizable_value_exceptions'])) {
$missingConstructorArguments[] = $constructorParameter->name;
continue;
}

$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
$constructorParameterType = 'unknown';
$reflectionType = $constructorParameter->getType();
if ($reflectionType instanceof \ReflectionNamedType) {
Expand All @@ -401,39 +374,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
throw new MissingConstructorArgumentsException(\sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "$%s".', $class, implode('", "$', $missingConstructorArguments)), 0, null, $missingConstructorArguments, $class);
}

if (!$constructor->isConstructor()) {
$instance = $constructor->invokeArgs(null, $params);

// do not set a parameter that has been set in the constructor
foreach ($unsetKeys as $key) {
unset($data[$key]);
}

return $instance;
}

try {
$instance = $reflectionClass->newInstanceArgs($params);

// do not set a parameter that has been set in the constructor
foreach ($unsetKeys as $key) {
unset($data[$key]);
}

return $instance;
} catch (\TypeError $e) {
if (!isset($context['not_normalizable_value_exceptions'])) {
throw $e;
}

if (\count($context['not_normalizable_value_exceptions'] ?? []) > 0) {
return $reflectionClass->newInstanceWithoutConstructor();
}
}

unset($context['has_constructor']);
if ($constructor->isConstructor()) {
return $reflectionClass->newInstanceArgs($params);
}

if (!$reflectionClass->isInstantiable()) {
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Failed to create object because the class "%s" is not instantiable.', $class), $data, ['unknown'], $context['deserialization_path'] ?? null);
return $constructor->invokeArgs(null, $params);
}

return new $class();
Expand Down

0 comments on commit b9e9656

Please sign in to comment.