Skip to content

Commit b9e9656

Browse files
committed
-refactoring
1 parent eca40f9 commit b9e9656

File tree

1 file changed

+13
-64
lines changed

1 file changed

+13
-64
lines changed

src/Serializer/AbstractItemNormalizer.php

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -304,27 +304,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
304304

305305
return $object;
306306
}
307-
// clean up even if no match
308-
unset($context[static::OBJECT_TO_POPULATE]);
309307

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

313311
$constructor = $this->getConstructor($data, $class, $context, $reflectionClass, $allowedAttributes);
314312
if ($constructor) {
315-
$context['has_constructor'] = true;
316-
if (true !== $constructor->isPublic()) {
317-
return $reflectionClass->newInstanceWithoutConstructor();
318-
}
319-
320313
$constructorParameters = $constructor->getParameters();
321-
$missingConstructorArguments = [];
322-
$params = [];
323-
$unsetKeys = [];
324314

315+
$params = [];
316+
$missingConstructorArguments = [];
325317
foreach ($constructorParameters as $constructorParameter) {
326318
$paramName = $constructorParameter->name;
327-
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
328319
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName, $class, $format, $context) : $paramName;
329320

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

338-
$variadicParameters = [];
339-
foreach ($data[$key] as $parameterKey => $parameterData) {
340-
try {
341-
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
342-
} catch (NotNormalizableValueException $exception) {
343-
if (!isset($context['not_normalizable_value_exceptions'])) {
344-
throw $exception;
345-
}
346-
347-
$context['not_normalizable_value_exceptions'][] = $exception;
348-
$params[$paramName] = $parameterData;
349-
}
350-
}
351-
352-
$params = array_merge(array_values($params), $variadicParameters);
353-
$unsetKeys[] = $key;
329+
$params[] = $data[$paramName];
354330
}
355331
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {
356332
$constructorContext = $context;
@@ -365,21 +341,18 @@ protected function instantiateObject(array &$data, string $class, array &$contex
365341
}
366342

367343
// Don't run set for a parameter passed to the constructor
368-
$unsetKeys[] = $key;
369-
} elseif (\array_key_exists($key, $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
370-
$params[$paramName] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
371-
} elseif (\array_key_exists($key, $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class] ?? [])) {
372-
$params[$paramName] = $this->defaultContext[self::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
344+
unset($data[$key]);
345+
} elseif (isset($context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key])) {
346+
$params[] = $context[static::DEFAULT_CONSTRUCTOR_ARGUMENTS][$class][$key];
373347
} elseif ($constructorParameter->isDefaultValueAvailable()) {
374-
$params[$paramName] = $constructorParameter->getDefaultValue();
375-
} elseif (!($context[self::REQUIRE_ALL_PROPERTIES] ?? $this->defaultContext[self::REQUIRE_ALL_PROPERTIES] ?? false) && $constructorParameter->hasType() && $constructorParameter->getType()->allowsNull()) {
376-
$params[$paramName] = null;
348+
$params[] = $constructorParameter->getDefaultValue();
377349
} else {
378350
if (!isset($context['not_normalizable_value_exceptions'])) {
379351
$missingConstructorArguments[] = $constructorParameter->name;
380352
continue;
381353
}
382354

355+
$attributeContext = $this->getAttributeDenormalizationContext($class, $paramName, $context);
383356
$constructorParameterType = 'unknown';
384357
$reflectionType = $constructorParameter->getType();
385358
if ($reflectionType instanceof \ReflectionNamedType) {
@@ -401,39 +374,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
401374
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);
402375
}
403376

404-
if (!$constructor->isConstructor()) {
405-
$instance = $constructor->invokeArgs(null, $params);
406-
407-
// do not set a parameter that has been set in the constructor
408-
foreach ($unsetKeys as $key) {
409-
unset($data[$key]);
410-
}
411-
412-
return $instance;
413-
}
414-
415-
try {
416-
$instance = $reflectionClass->newInstanceArgs($params);
417-
418-
// do not set a parameter that has been set in the constructor
419-
foreach ($unsetKeys as $key) {
420-
unset($data[$key]);
421-
}
422-
423-
return $instance;
424-
} catch (\TypeError $e) {
425-
if (!isset($context['not_normalizable_value_exceptions'])) {
426-
throw $e;
427-
}
428-
377+
if (\count($context['not_normalizable_value_exceptions'] ?? []) > 0) {
429378
return $reflectionClass->newInstanceWithoutConstructor();
430379
}
431-
}
432380

433-
unset($context['has_constructor']);
381+
if ($constructor->isConstructor()) {
382+
return $reflectionClass->newInstanceArgs($params);
383+
}
434384

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

439388
return new $class();

0 commit comments

Comments
 (0)