Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93abdbb

Browse files
committed
[Serializer] Replace the MissingConstructorArgumentsException class with MissingConstructorArgumentException
1 parent 90d7761 commit 93abdbb

8 files changed

+75
-12
lines changed

.github/expected-missing-return-types.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ index 12c778cb80..4ad55fb3e1 100644
929929
{
930930
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES];
931931
@@ -311,5 +311,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
932-
* @throws MissingConstructorArgumentsException
932+
* @throws MissingConstructorArgumentException
933933
*/
934934
- protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)
935935
+ protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null): object

UPGRADE-6.3.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,8 @@ Validator
6666
---------
6767

6868
* Implementing the `ConstraintViolationInterface` without implementing the `getConstraint()` method is deprecated
69+
70+
Serializer
71+
----------
72+
73+
* Deprecate the `Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException` class, use `Symfony\Component\Serializer\Exception\MissingConstructorArgumentException` instead

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
---
66

77
* Add `XmlEncoder::SAVE_OPTIONS` context option
8+
* Deprecate the `MissingConstructorArgumentsException` class
9+
* Replace the `MissingConstructorArgumentsException` class with `MissingConstructorArgumentException`
810

911
6.2
1012
---
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Exception;
13+
14+
class MissingConstructorArgumentException extends MissingConstructorArgumentsException
15+
{
16+
private string $class;
17+
private string $missingArgument;
18+
19+
/**
20+
* @param class-string $class
21+
*/
22+
public function __construct(string $class, string $missingArgument, int $code = 0, \Throwable $previous = null)
23+
{
24+
$this->class = $class;
25+
$this->missingArgument = $missingArgument;
26+
27+
$message = sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $missingArgument);
28+
29+
parent::__construct($message, $code, $previous, [$missingArgument]);
30+
}
31+
32+
public function getClass(): string
33+
{
34+
return $this->class;
35+
}
36+
37+
public function getMissingArgument(): string
38+
{
39+
return $this->missingArgument;
40+
}
41+
}

src/Symfony/Component/Serializer/Exception/MissingConstructorArgumentsException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\Serializer\Exception;
1313

1414
/**
15+
* @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException} instead
16+
*
1517
* @author Maxime VEBER <[email protected]>
1618
*/
1719
class MissingConstructorArgumentsException extends RuntimeException
@@ -23,16 +25,24 @@ class MissingConstructorArgumentsException extends RuntimeException
2325

2426
public function __construct(string $message, int $code = 0, \Throwable $previous = null, array $missingArguments = [])
2527
{
28+
if (!$this instanceof MissingConstructorArgumentException) {
29+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s" class is deprecated, use "%s" instead.', __CLASS__, MissingConstructorArgumentException::class);
30+
}
31+
2632
$this->missingArguments = $missingArguments;
2733

2834
parent::__construct($message, $code, $previous);
2935
}
3036

3137
/**
38+
* @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException::getMissingArgument()} instead
39+
*
3240
* @return string[]
3341
*/
3442
public function getMissingConstructorArguments(): array
3543
{
44+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "%s::getMissingArgument()" instead.', __METHOD__, MissingConstructorArgumentException::class);
45+
3646
return $this->missingArguments;
3747
}
3848
}

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1515
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1616
use Symfony\Component\Serializer\Exception\LogicException;
17-
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
17+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException;
1818
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1919
use Symfony\Component\Serializer\Exception\RuntimeException;
2020
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
@@ -308,7 +308,7 @@ protected function getConstructor(array &$data, string $class, array &$context,
308308
* @return object
309309
*
310310
* @throws RuntimeException
311-
* @throws MissingConstructorArgumentsException
311+
* @throws MissingConstructorArgumentException
312312
*/
313313
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)
314314
{
@@ -381,7 +381,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
381381
$params[] = null;
382382
} else {
383383
if (!isset($context['not_normalizable_value_exceptions'])) {
384-
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]);
384+
throw new MissingConstructorArgumentException($class, $constructorParameter->name);
385385
}
386386

387387
$exception = NotNormalizableValueException::createForUnexpectedDataType(
@@ -425,7 +425,7 @@ protected function denormalizeParameter(\ReflectionClass $class, \ReflectionPara
425425
}
426426
} catch (\ReflectionException $e) {
427427
throw new RuntimeException(sprintf('Could not determine the class of the parameter "%s".', $parameterName), 0, $e);
428-
} catch (MissingConstructorArgumentsException $e) {
428+
} catch (MissingConstructorArgumentException $e) {
429429
if (!$parameter->getType()->allowsNull()) {
430430
throw $e;
431431
}

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2323
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2424
use Symfony\Component\Serializer\Exception\LogicException;
25-
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
25+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException;
2626
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2727
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
2828
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
@@ -417,7 +417,7 @@ abstract protected function setAttributeValue(object $object, string $attribute,
417417
*
418418
* @throws NotNormalizableValueException
419419
* @throws ExtraAttributesException
420-
* @throws MissingConstructorArgumentsException
420+
* @throws MissingConstructorArgumentException
421421
* @throws LogicException
422422
*/
423423
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
565565
}
566566

567567
$extraAttributesException ??= $e;
568-
} catch (MissingConstructorArgumentsException $e) {
568+
} catch (MissingConstructorArgumentException $e) {
569569
if (!$isUnionType) {
570570
throw $e;
571571
}

src/Symfony/Component/Serializer/Tests/Normalizer/Features/ConstructorArgumentsTestTrait.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Serializer\Tests\Normalizer\Features;
1313

14-
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
14+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException;
1515
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1616
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
1717

@@ -63,8 +63,13 @@ public function testConstructorWithMissingData()
6363

6464
$normalizer = $this->getDenormalizerForConstructArguments();
6565

66-
$this->expectException(MissingConstructorArgumentsException::class);
67-
$this->expectExceptionMessage('Cannot create an instance of "'.ConstructorArgumentsObject::class.'" from serialized data because its constructor requires parameter "bar" to be present.');
68-
$normalizer->denormalize($data, ConstructorArgumentsObject::class);
66+
try {
67+
$normalizer->denormalize($data, ConstructorArgumentsObject::class);
68+
self::fail(sprintf('Failed asserting that exception of type "%s" is thrown.', MissingConstructorArgumentException::class));
69+
} catch (MissingConstructorArgumentException $e) {
70+
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());
71+
self::assertSame(ConstructorArgumentsObject::class, $e->getClass());
72+
self::assertSame('bar', $e->getMissingArgument());
73+
}
6974
}
7075
}

0 commit comments

Comments
 (0)