Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] Replace the MissingConstructorArgumentsException class with MissingConstructorArgumentException #49013

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/expected-missing-return-types.diff
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-6.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ Validator
---------

* Implementing the `ConstraintViolationInterface` without implementing the `getConstraint()` method is deprecated

Serializer
----------

* Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException`
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `XmlEncoder::SAVE_OPTIONS` context option
* Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException`

6.2
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Serializer\Exception;

/**
* @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException} instead
*
* @author Maxime VEBER <maxime.veber@nekland.fr>
*/
class MissingConstructorArgumentsException extends RuntimeException
Expand All @@ -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);
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
}

$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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -565,7 +565,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
}

$extraAttributesException ??= $e;
} catch (MissingConstructorArgumentsException $e) {
} catch (MissingConstructorArgumentException $e) {
if (!$isUnionType) {
throw $e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
}