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

Skip to content

Commit 0f3622a

Browse files
committed
Fixing nicolas-grekas' review
1 parent 423c4d9 commit 0f3622a

File tree

5 files changed

+23
-122
lines changed

5 files changed

+23
-122
lines changed

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

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -27,78 +27,27 @@ class MissingConstructorArgumentsException extends RuntimeException
2727
private $class = null;
2828

2929
/**
30-
* @var array{int:MissingConstructorArgumentException}
30+
* @param string[] $missingArguments
31+
* @param class-string|null $class
3132
*/
32-
private $missingConstructorArgumentExceptions = [];
33-
34-
/**
35-
* @param string[]|MissingConstructorArgumentException[] $missingArguments
36-
*/
37-
public function __construct(string $message, int $code = 0, \Throwable $previous = null, array $missingArguments = [])
33+
public function __construct(string $message, int $code = 0, \Throwable $previous = null, array $missingArguments = [], ?string $class = null)
3834
{
39-
if (count(array_filter($missingArguments, fn($missingArgument) => is_string($missingArgument))) > 1) {
40-
trigger_deprecation(
41-
'symfony/serializer',
42-
'6.3',
43-
'Passing strings in "missingArguments" arguments of "%s" class is deprecated, use "%s" instead.', __CLASS__, MissingConstructorArgumentException::class
44-
);
45-
}
46-
47-
foreach ($missingArguments as $position => $missingArgument) {
48-
if (is_string($missingArgument)) {
49-
$this->missingConstructorArguments[$position] = $missingArgument;
50-
}
51-
if ($missingArgument instanceof MissingConstructorArgumentException) {
52-
$this->missingConstructorArgumentExceptions[$position] = $missingArgument;
53-
}
54-
}
55-
56-
if (0 === count($this->missingConstructorArgumentExceptions)) {
57-
return;
58-
}
59-
60-
$classes = array_unique(array_map(
61-
fn (MissingConstructorArgumentException $missingArgument) => $missingArgument->getClass(),
62-
$this->missingConstructorArgumentExceptions
63-
));
64-
if (count($classes) > 1) {
65-
throw new \InvalidArgumentException(sprintf(
66-
'All instances of "%s" must concern the same class. Classes given : "%s".',
67-
MissingConstructorArgumentException::class,
68-
implode('", "', $classes)
69-
));
70-
}
71-
72-
$this->class = $classes[0];
35+
$this->missingConstructorArguments = $missingArguments;
36+
$this->class = $class;
7337

7438
parent::__construct($message, $code, $previous);
7539
}
7640

7741
/**
78-
* @return string[]
42+
* @return array{int:string}
7943
*/
8044
public function getMissingConstructorArguments(): array
8145
{
82-
$missingConstructorArguments = $this->missingConstructorArguments;
83-
foreach ($this->missingConstructorArgumentExceptions as $position => $missingArgument) {
84-
$missingConstructorArguments[$position] = $missingArgument->getMissingConstructorArgument();
85-
}
86-
87-
ksort($missingConstructorArguments);
88-
89-
return $missingConstructorArguments;
46+
return $this->missingConstructorArguments;
9047
}
9148

9249
public function getClass(): ?string
9350
{
9451
return $this->class;
9552
}
96-
97-
/**
98-
* @return MissingConstructorArgumentException[]
99-
*/
100-
public function getMissingConstructorArgumentExceptions(): array
101-
{
102-
return $this->missingConstructorArgumentExceptions;
103-
}
10453
}

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

Lines changed: 3 additions & 6 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\MissingConstructorArgumentException;
17+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
1818
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1919
use Symfony\Component\Serializer\Exception\RuntimeException;
2020
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
@@ -386,7 +386,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
386386
$resolvedConstructorParameters[] = null;
387387
} else {
388388
if (!isset($context['not_normalizable_value_exceptions'])) {
389-
$missingConstructorArguments[] = new MissingConstructorArgumentException($class, $constructorParameter->name);
389+
$missingConstructorArguments[] = $constructorParameter->name;
390390
continue;
391391
}
392392

@@ -404,10 +404,6 @@ protected function instantiateObject(array &$data, string $class, array &$contex
404404
}
405405

406406
if (!empty($missingConstructorArguments)) {
407-
$missingConstructorArguments = array_map(
408-
fn(MissingConstructorArgumentException $exception) => $exception->getMissingConstructorArgument(),
409-
$missingConstructorArguments
410-
);
411407
throw new MissingConstructorArgumentsException(
412408
sprintf(
413409
'Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "%s".',
@@ -417,6 +413,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
417413
0,
418414
null,
419415
$missingConstructorArguments,
416+
$class,
420417
);
421418
}
422419

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

Lines changed: 7 additions & 7 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\MissingConstructorArgumentException;
25+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
2626
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2727
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
2828
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
@@ -419,15 +419,15 @@ abstract protected function setAttributeValue(object $object, string $attribute,
419419
*
420420
* @throws NotNormalizableValueException
421421
* @throws ExtraAttributesException
422-
* @throws MissingConstructorArgumentException
422+
* @throws MissingConstructorArgumentsException
423423
* @throws LogicException
424424
*/
425425
private function validateAndDenormalize(array $types, string $currentClass, string $attribute, mixed $data, ?string $format, array $context): mixed
426426
{
427427
$expectedTypes = [];
428428
$isUnionType = \count($types) > 1;
429429
$extraAttributesException = null;
430-
$missingConstructorArgumentException = null;
430+
$missingConstructorArgumentsException = null;
431431
foreach ($types as $type) {
432432
if (null === $data && $type->isNullable()) {
433433
return null;
@@ -567,21 +567,21 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
567567
}
568568

569569
$extraAttributesException ??= $e;
570-
} catch (MissingConstructorArgumentException $e) {
570+
} catch (MissingConstructorArgumentsException $e) {
571571
if (!$isUnionType) {
572572
throw $e;
573573
}
574574

575-
$missingConstructorArgumentException ??= $e;
575+
$missingConstructorArgumentsException ??= $e;
576576
}
577577
}
578578

579579
if ($extraAttributesException) {
580580
throw $extraAttributesException;
581581
}
582582

583-
if ($missingConstructorArgumentException) {
584-
throw $missingConstructorArgumentException;
583+
if ($missingConstructorArgumentsException) {
584+
throw $missingConstructorArgumentsException;
585585
}
586586

587587
if ($context[self::DISABLE_TYPE_ENFORCEMENT] ?? $this->defaultContext[self::DISABLE_TYPE_ENFORCEMENT] ?? false) {

src/Symfony/Component/Serializer/Tests/Exception/MissingConstructorArgumentsExceptionTest.php

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Exception;
44

55
use PHPUnit\Framework\TestCase;
6-
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException;
76
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
87
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
98

@@ -20,62 +19,19 @@ public function testPassingAnArrayOfStringInMissingArgumentsDoesNotBreaksBC()
2019

2120
$this->assertSame(['foo', 'bar'], $exception->getMissingConstructorArguments());
2221
$this->assertNull($exception->getClass());
23-
$this->assertSame([], $exception->getMissingConstructorArgumentExceptions());
2422
}
2523

26-
public function testPassingAnArrayOfMissingConstructorArgumentExceptionInMissingArgumentsDoesNotBreaksBC()
24+
public function testPassingAnArrayOfMissingConstructorArgumentsAndAClassStringDoesNotBreaksBC()
2725
{
28-
$missingConstructorArgumentExceptions = [
29-
new MissingConstructorArgumentException(Dummy::class, 'foo'),
30-
new MissingConstructorArgumentException(Dummy::class, 'bar'),
31-
];
3226
$exception = new MissingConstructorArgumentsException(
3327
'MissingConstructorArgumentsException',
3428
0,
3529
null,
36-
$missingConstructorArgumentExceptions
30+
['foo', 'bar'],
31+
Dummy::class
3732
);
3833

3934
$this->assertSame(['foo', 'bar'], $exception->getMissingConstructorArguments());
4035
$this->assertSame(Dummy::class, $exception->getClass());
41-
$this->assertSame($missingConstructorArgumentExceptions, $exception->getMissingConstructorArgumentExceptions());
42-
}
43-
44-
public function testPassingAnArrayOfMissingConstructorArgumentExceptionInMissingArgumentsWithDifferentClassesThrowsInvalidArgumentException()
45-
{
46-
$this->expectException(\InvalidArgumentException::class);
47-
$this->expectExceptionMessage(sprintf(
48-
'All instances of "%s" must concern the same class. Classes given : "%s".',
49-
MissingConstructorArgumentException::class,
50-
implode('", "', [Dummy::class, \stdClass::class])
51-
));
52-
53-
$exception = new MissingConstructorArgumentsException(
54-
'MissingConstructorArgumentsException',
55-
0,
56-
null,
57-
[
58-
new MissingConstructorArgumentException(Dummy::class, 'foo'),
59-
new MissingConstructorArgumentException(\stdClass::class, 'bar'),
60-
]
61-
);
62-
}
63-
64-
public function testPassingAMixOfStringsAndMissingConstructorArgumentExceptionInMissingArgumentsDoesNotBreaksBC()
65-
{
66-
$missingConstructorArgumentExceptions = [
67-
new MissingConstructorArgumentException(Dummy::class, 'foo'),
68-
new MissingConstructorArgumentException(Dummy::class, 'bar'),
69-
];
70-
$exception = new MissingConstructorArgumentsException(
71-
'MissingConstructorArgumentsException',
72-
0,
73-
null,
74-
[...$missingConstructorArgumentExceptions, 'baz']
75-
);
76-
77-
$this->assertSame(['foo', 'bar', 'baz'], $exception->getMissingConstructorArguments());
78-
$this->assertSame(Dummy::class, $exception->getClass());
79-
$this->assertSame($missingConstructorArgumentExceptions, $exception->getMissingConstructorArgumentExceptions());
8036
}
8137
}

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

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

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

14-
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException;
1514
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
1615
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1716
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
@@ -70,9 +69,9 @@ public function testConstructorWithMissingData()
7069
self::assertSame(sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "bar", "baz".', ConstructorArgumentsObject::class), $e->getMessage());
7170
self::assertSame(ConstructorArgumentsObject::class, $e->getClass());
7271
self::assertSame(['bar', 'baz'], $e->getMissingConstructorArguments());
73-
foreach ($e->getMissingConstructorArgumentExceptions() as $missingConstructorArgumentException) {
74-
self::assertContains($missingConstructorArgumentException->getMissingConstructorArgument(), ['bar', 'baz']);
75-
self::assertSame($missingConstructorArgumentException->getClass(), ConstructorArgumentsObject::class);
72+
foreach ($e->getMissingConstructorArguments() as $missingConstructorArgument) {
73+
self::assertContains($missingConstructorArgument->getMissingConstructorArgument(), ['bar', 'baz']);
74+
self::assertSame($e->getClass(), ConstructorArgumentsObject::class);
7675
}
7776
}
7877
}

0 commit comments

Comments
 (0)