Skip to content

Commit 5dc7445

Browse files
committed
Fix WebMamba's reviews
1 parent 9796cf7 commit 5dc7445

File tree

7 files changed

+64
-33
lines changed

7 files changed

+64
-33
lines changed

UPGRADE-6.3.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,3 @@ Validator
104104
---------
105105

106106
* Implementing the `ConstraintViolationInterface` without implementing the `getConstraint()` method is deprecated
107-
108-
Serializer
109-
----------
110-
111-
* Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException`

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ CHANGELOG
77
* Add `XmlEncoder::SAVE_OPTIONS` context option
88
* Add `BackedEnumNormalizer::ALLOW_INVALID_VALUES` context option
99
* Add method `getSupportedTypes(?string $format)` to `NormalizerInterface` and `DenormalizerInterface`
10-
* Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException`
1110
* Deprecate `CacheableSupportsMethodInterface` in favor of the new `getSupportedTypes(?string $format)` methods
1211

1312
6.2

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111

1212
namespace Symfony\Component\Serializer\Exception;
1313

14-
class MissingConstructorArgumentException extends MissingConstructorArgumentsException
14+
class MissingConstructorArgumentException extends RuntimeException
1515
{
1616
private string $class;
17-
private string $missingArgument;
17+
private string $missingConstructorArgument;
1818

1919
/**
2020
* @param class-string $class
2121
*/
22-
public function __construct(string $class, string $missingArgument, int $code = 0, \Throwable $previous = null)
22+
public function __construct(string $class, string $missingConstructorArgument, int $code = 0, \Throwable $previous = null)
2323
{
2424
$this->class = $class;
25-
$this->missingArgument = $missingArgument;
25+
$this->missingConstructorArgument = $missingConstructorArgument;
2626

27-
$message = sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $missingArgument);
27+
$message = sprintf('Cannot create an instance of "%s" from serialized data because its constructor requires parameter "%s" to be present.', $class, $missingConstructorArgument);
2828

29-
parent::__construct($message, $code, $previous, [$missingArgument]);
29+
parent::__construct($message, $code, $previous);
3030
}
3131

3232
public function getClass(): string
3333
{
3434
return $this->class;
3535
}
3636

37-
public function getMissingArgument(): string
37+
public function getMissingConstructorArgument(): string
3838
{
39-
return $this->missingArgument;
39+
return $this->missingConstructorArgument;
4040
}
4141
}

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,61 @@
1212
namespace Symfony\Component\Serializer\Exception;
1313

1414
/**
15-
* @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException} instead
16-
*
1715
* @author Maxime VEBER <maxime.veber@nekland.fr>
1816
*/
1917
class MissingConstructorArgumentsException extends RuntimeException
2018
{
2119
/**
22-
* @var string[]
20+
* @var string
21+
*/
22+
private $class;
23+
24+
/**
25+
* @var MissingConstructorArgumentException[]
2326
*/
24-
private $missingArguments;
27+
private $missingConstructorArgumentExceptions = [];
2528

26-
public function __construct(string $message, int $code = 0, \Throwable $previous = null, array $missingArguments = [])
29+
/**
30+
* @param MissingConstructorArgumentException[] $missingArguments
31+
*/
32+
public function __construct(array $missingArguments)
2733
{
28-
if (!$this instanceof MissingConstructorArgumentException) {
29-
trigger_deprecation('symfony/serializer', '6.3', 'The "%s" class is deprecated, use "%s" instead.', __CLASS__, MissingConstructorArgumentException::class);
34+
$this->missingConstructorArgumentExceptions = $missingArguments;
35+
$classes = array_unique(array_map(fn (MissingConstructorArgumentException $missingConstructorArgumentException) => $missingConstructorArgumentException->getClass(), $missingArguments));
36+
if (count($classes) > 1) {
37+
throw new \InvalidArgumentException(sprintf(
38+
'All instances of "%s" must concern the same class. Classes given : "%s".',
39+
MissingConstructorArgumentException::class,
40+
implode('", "', $classes)
41+
));
3042
}
43+
$this->class = $classes[0];
3144

32-
$this->missingArguments = $missingArguments;
45+
parent::__construct(sprintf(
46+
'Cannot create an instance of "%s" from serialized data because its constructor requires the following parameters to be present : "%s".',
47+
$this->class,
48+
implode('", "', $this->getMissingConstructorArguments())
49+
));
50+
}
3351

34-
parent::__construct($message, $code, $previous);
52+
public function getClass(): string
53+
{
54+
return $this->class;
3555
}
3656

3757
/**
38-
* @deprecated since Symfony 6.3, use {@see MissingConstructorArgumentException::getMissingArgument()} instead
39-
*
4058
* @return string[]
4159
*/
4260
public function getMissingConstructorArguments(): array
4361
{
44-
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "%s::getMissingArgument()" instead.', __METHOD__, MissingConstructorArgumentException::class);
62+
return array_map(fn(MissingConstructorArgumentException $exception) => $exception->getMissingConstructorArgument(), $this->missingConstructorArgumentExceptions);
63+
}
4564

46-
return $this->missingArguments;
65+
/**
66+
* @return MissingConstructorArgumentException[]
67+
*/
68+
public function getMissingConstructorArgumentExceptions(): array
69+
{
70+
return $this->missingConstructorArgumentExceptions;
4771
}
4872
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ protected function instantiateObject(array &$data, string $class, array &$contex
387387
$resolvedConstructorParameters[] = null;
388388
} else {
389389
if (!isset($context['not_normalizable_value_exceptions'])) {
390-
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]);
390+
$context['missing_constructor_arguments'][] = new MissingConstructorArgumentException($class, $constructorParameter->name);
391+
continue;
391392
}
392393

393394
$exception = NotNormalizableValueException::createForUnexpectedDataType(
@@ -404,7 +405,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
404405
}
405406

406407
if (!empty($context['missing_constructor_arguments'])) {
407-
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('", "', $context['missing_constructor_arguments'])), 0, null, $context['missing_constructor_arguments']);
408+
throw new MissingConstructorArgumentsException($context['missing_constructor_arguments']);
408409
}
409410

410411
if ($constructor->isConstructor()) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
135135
$this->objectClassResolver = $objectClassResolver;
136136
}
137137

138+
/**
139+
* @param array $context
140+
*/
138141
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */)
139142
{
140143
return \is_object($data) && !$data instanceof \Traversable;
@@ -287,9 +290,14 @@ abstract protected function extractAttributes(object $object, string $format = n
287290

288291
/**
289292
* Gets the attribute value.
293+
*
294+
* @return mixed
290295
*/
291296
abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []);
292297

298+
/**
299+
* @param array $context
300+
*/
293301
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */)
294302
{
295303
return class_exists($type) || (interface_exists($type, false) && null !== $this->classDiscriminatorResolver?->getMappingForClass($type));

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

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

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

14+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentException;
1415
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
1516
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1617
use Symfony\Component\Serializer\Tests\Fixtures\NotSerializedConstructorArgumentDummy;
@@ -62,14 +63,17 @@ public function testConstructorWithMissingData()
6263
];
6364

6465
$normalizer = $this->getDenormalizerForConstructArguments();
65-
6666
try {
6767
$normalizer->denormalize($data, ConstructorArgumentsObject::class);
68-
self::fail(sprintf('Failed asserting that exception of type "%s" is thrown.', MissingConstructorArgumentException::class));
69-
} catch (MissingConstructorArgumentException $e) {
68+
self::fail(sprintf('Failed asserting that exception of type "%s" is thrown.', MissingConstructorArgumentsException::class));
69+
} catch (MissingConstructorArgumentsException $e) {
7070
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());
7171
self::assertSame(ConstructorArgumentsObject::class, $e->getClass());
72-
self::assertSame('bar', $e->getMissingArgument());
72+
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);
76+
}
7377
}
7478
}
7579
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy