From b377c121bf4b40c64ebeefb9b856a28b8c1910ed Mon Sep 17 00:00:00 2001 From: HypeMC Date: Sat, 21 Jan 2023 14:00:36 +0100 Subject: [PATCH] [Serializer] Replace the MissingConstructorArgumentsException class with MissingConstructorArgumentException --- .github/expected-missing-return-types.diff | 2 +- UPGRADE-6.3.md | 5 +++ src/Symfony/Component/Serializer/CHANGELOG.md | 1 + .../MissingConstructorArgumentException.php | 41 +++++++++++++++++++ .../MissingConstructorArgumentsException.php | 10 +++++ .../Normalizer/AbstractNormalizer.php | 8 ++-- .../Normalizer/AbstractObjectNormalizer.php | 6 +-- .../ConstructorArgumentsTestTrait.php | 13 ++++-- 8 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentException.php diff --git a/.github/expected-missing-return-types.diff b/.github/expected-missing-return-types.diff index c248018a4b9f6..22d5cf09bcf22 100644 --- a/.github/expected-missing-return-types.diff +++ b/.github/expected-missing-return-types.diff @@ -929,7 +929,7 @@ index 12c778cb80..4ad55fb3e1 100644 { $ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES]; @@ -311,5 +311,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn - * @throws MissingConstructorArgumentsException + * @throws MissingConstructorArgumentException */ - protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null) + protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null): object diff --git a/UPGRADE-6.3.md b/UPGRADE-6.3.md index 89caa851669a4..9b9918aec2116 100644 --- a/UPGRADE-6.3.md +++ b/UPGRADE-6.3.md @@ -66,3 +66,8 @@ Validator --------- * Implementing the `ConstraintViolationInterface` without implementing the `getConstraint()` method is deprecated + +Serializer +---------- + + * Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException` diff --git a/src/Symfony/Component/Serializer/CHANGELOG.md b/src/Symfony/Component/Serializer/CHANGELOG.md index d7799bb50236f..19eaa6a9cb8a4 100644 --- a/src/Symfony/Component/Serializer/CHANGELOG.md +++ b/src/Symfony/Component/Serializer/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add `XmlEncoder::SAVE_OPTIONS` context option + * Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException` 6.2 --- diff --git a/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentException.php b/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentException.php new file mode 100644 index 0000000000000..3fdfaf605869e --- /dev/null +++ b/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentException.php @@ -0,0 +1,41 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Exception; + +class MissingConstructorArgumentException extends MissingConstructorArgumentsException +{ + private string $class; + private string $missingArgument; + + /** + * @param class-string $class + */ + public function __construct(string $class, string $missingArgument, int $code = 0, \Throwable $previous = null) + { + $this->class = $class; + $this->missingArgument = $missingArgument; + + $message = sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $missingArgument); + + parent::__construct($message, $code, $previous, [$missingArgument]); + } + + public function getClass(): string + { + return $this->class; + } + + public function getMissingArgument(): string + { + return $this->missingArgument; + } +} diff --git a/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php b/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php index fe984a291f074..a5a71d00cf62a 100644 --- a/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php +++ b/src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php @@ -12,6 +12,8 @@ namespace Symfony\Component\Serializer\Exception; /** + * @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException} instead + * * @author Maxime VEBER */ class MissingConstructorArgumentsException extends RuntimeException @@ -23,16 +25,24 @@ class MissingConstructorArgumentsException extends RuntimeException public function __construct(string $message, int $code = 0, \Throwable $previous = null, array $missingArguments = []) { + if (!$this instanceof MissingConstructorArgumentException) { + trigger_deprecation('symfony/serializer', '6.3', 'The "%s" class is deprecated, use "%s" instead.', __CLASS__, MissingConstructorArgumentException::class); + } + $this->missingArguments = $missingArguments; parent::__construct($message, $code, $previous); } /** + * @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException::getMissingArgument()} instead + * * @return string[] */ public function getMissingConstructorArguments(): array { + trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "%s::getMissingArgument()" instead.', __METHOD__, MissingConstructorArgumentException::class); + return $this->missingArguments; } } diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 829e178407bd2..52e985815bf99 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -14,7 +14,7 @@ use Symfony\Component\Serializer\Exception\CircularReferenceException; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\LogicException; -use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException; +use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; @@ -308,7 +308,7 @@ protected function getConstructor(array &$data, string $class, array &$context, * @return object * * @throws RuntimeException - * @throws MissingConstructorArgumentsException + * @throws MissingConstructorArgumentException */ protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null) { @@ -381,7 +381,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex $params[] = null; } else { if (!isset($context['not_normalizable_value_exceptions'])) { - 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), 0, null, [$constructorParameter->name]); + throw new MissingConstructorArgumentException($class, $constructorParameter->name); } $exception = NotNormalizableValueException::createForUnexpectedDataType( @@ -425,7 +425,7 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara } } catch (\ReflectionException $e) { throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e); - } catch (MissingConstructorArgumentsException $e) { + } catch (MissingConstructorArgumentException $e) { if (!$parameter->getType()->allowsNull()) { throw $e; } diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php index 7c4c5fb41bd49..4a02c05a80fef 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php @@ -22,7 +22,7 @@ use Symfony\Component\Serializer\Encoder\XmlEncoder; use Symfony\Component\Serializer\Exception\ExtraAttributesException; use Symfony\Component\Serializer\Exception\LogicException; -use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException; +use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException; use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata; @@ -417,7 +417,7 @@ abstract protected function setAttributeValue(object $object, string $attribute, * * @throws NotNormalizableValueException * @throws ExtraAttributesException - * @throws MissingConstructorArgumentsException + * @throws MissingConstructorArgumentException * @throws LogicException */ private function validateAndDenormalize(array $types, string $currentClass, string $attribute, mixed $data, ?string $format, array $context): mixed @@ -565,7 +565,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri } $extraAttributesException ??= $e; - } catch (MissingConstructorArgumentsException $e) { + } catch (MissingConstructorArgumentException $e) { if (!$isUnionType) { throw $e; } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php index 306c571f9c59d..9489136be2cb9 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Serializer\Tests\Normalizer\Features; -use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException; +use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy; @@ -63,8 +63,13 @@ public function testConstructorWithMissingData() $normalizer = $this->getDenormalizerForConstructArguments(); - $this->expectException(MissingConstructorArgumentsException::class); - $this->expectExceptionMessage('Cannot create an instance of "'.ConstructorArgumentsObject::class.'" from serialized data because its constructor requires parameter "bar" to be present.'); - $normalizer->denormalize($data, ConstructorArgumentsObject::class); + try { + $normalizer->denormalize($data, ConstructorArgumentsObject::class); + self::fail(sprintf('Failed asserting that exception of type "%s" is thrown.', MissingConstructorArgumentException::class)); + } catch (MissingConstructorArgumentException $e) { + self::assertSame(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "bar" to be present.', ConstructorArgumentsObject::class), $e->getMessage()); + self::assertSame(ConstructorArgumentsObject::class, $e->getClass()); + self::assertSame('bar', $e->getMissingArgument()); + } } }