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

Skip to content

Commit e5af24a

Browse files
nicolas-grekasfabpot
authored andcommitted
[Serializer] Add wildcard support to getSupportedTypes()
1 parent 400685a commit e5af24a

27 files changed

+154
-129
lines changed

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

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8130,6 +8130,7 @@ index 1924b1ddb0..62c58c8e8b 100644
81308130
$annotatedClasses = [];
81318131
diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/ControllerArgumentValueResolverPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/ControllerArgumentValueResolverPass.php
81328132
index dff3e248ae..381db9aa8f 100644
8133+
index 6e00840c7e..8e69c81c23 100644
81338134
--- a/src/Symfony/Component/HttpKernel/DependencyInjection/ControllerArgumentValueResolverPass.php
81348135
+++ b/src/Symfony/Component/HttpKernel/DependencyInjection/ControllerArgumentValueResolverPass.php
81358136
@@ -34,5 +34,5 @@ class ControllerArgumentValueResolverPass implements CompilerPassInterface
@@ -11254,32 +11255,32 @@ index fc6336ebdb..e13a834930 100644
1125411255
{
1125511256
if (1 > \func_num_args()) {
1125611257
diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
11257-
index 52e985815b..e7d0493152 100644
11258+
index 7d138b0b26..03e28f9d20 100644
1125811259
--- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
1125911260
+++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
11260-
@@ -210,5 +210,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
11261+
@@ -215,5 +215,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
1126111262
* @throws LogicException if the 'allow_extra_attributes' context variable is false and no class metadata factory is provided
1126211263
*/
1126311264
- protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false)
1126411265
+ protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
1126511266
{
1126611267
$allowExtraAttributes = $context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES];
11267-
@@ -260,5 +260,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
11268+
@@ -265,5 +265,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
1126811269
* @return bool
1126911270
*/
1127011271
- protected function isAllowedAttribute(object|string $classOrObject, string $attribute, string $format = null, array $context = [])
1127111272
+ protected function isAllowedAttribute(object|string $classOrObject, string $attribute, string $format = null, array $context = []): bool
1127211273
{
1127311274
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES];
11274-
@@ -311,5 +311,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
11275+
@@ -316,5 +316,5 @@ abstract class AbstractNormalizer implements NormalizerInterface, DenormalizerIn
1127511276
* @throws MissingConstructorArgumentException
1127611277
*/
1127711278
- protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null)
1127811279
+ protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, string $format = null): object
1127911280
{
1128011281
if (null !== $object = $this->extractObjectToPopulate($class, $context, self::OBJECT_TO_POPULATE)) {
1128111282
diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
11282-
index a02a46b941..aedfd67c2e 100644
11283+
index 75fe3a5cb1..a28dd40568 100644
1128311284
--- a/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
1128411285
+++ b/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
1128511286
@@ -139,10 +139,10 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
@@ -11321,46 +11322,36 @@ index a02a46b941..aedfd67c2e 100644
1132111322
- public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */)
1132211323
+ public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool
1132311324
{
11324-
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
11325+
return class_exists($type) || (interface_exists($type, false) && null !== $this->classDiscriminatorResolver?->getMappingForClass($type));
1132511326
}
1132611327

1132711328
- public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
1132811329
+ public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
1132911330
{
1133011331
if (!isset($context['cache_key'])) {
11331-
diff --git a/src/Symfony/Component/Serializer/Normalizer/DenormalizerAwareTrait.php b/src/Symfony/Component/Serializer/Normalizer/DenormalizerAwareTrait.php
11332-
index c5cc86ecf6..c65534fafb 100644
11333-
--- a/src/Symfony/Component/Serializer/Normalizer/DenormalizerAwareTrait.php
11334-
+++ b/src/Symfony/Component/Serializer/Normalizer/DenormalizerAwareTrait.php
11335-
@@ -25,5 +25,5 @@ trait DenormalizerAwareTrait
11336-
* @return void
11337-
*/
11338-
- public function setDenormalizer(DenormalizerInterface $denormalizer)
11339-
+ public function setDenormalizer(DenormalizerInterface $denormalizer): void
11340-
{
11341-
$this->denormalizer = $denormalizer;
1134211332
diff --git a/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php b/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
11343-
index 1786d6fff1..04a2e62ed2 100644
11333+
index 1d83b2da11..1c632f42bf 100644
1134411334
--- a/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
1134511335
+++ b/src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
11346-
@@ -45,5 +45,5 @@ interface DenormalizerInterface
11336+
@@ -47,5 +47,5 @@ interface DenormalizerInterface
1134711337
* @throws ExceptionInterface Occurs for all the other cases of errors
1134811338
*/
1134911339
- public function denormalize(mixed $data, string $type, string $format = null, array $context = []);
1135011340
+ public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed;
1135111341

1135211342
/**
11353-
@@ -57,4 +57,4 @@ interface DenormalizerInterface
11343+
@@ -64,5 +64,5 @@ interface DenormalizerInterface
1135411344
* @return bool
1135511345
*/
1135611346
- public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */);
1135711347
+ public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */): bool;
11358-
}
11348+
11349+
/**
1135911350
diff --git a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php
11360-
index e08dd5d9ec..cc282ae4bb 100644
11351+
index 2719c8b52c..1112f7f3cc 100644
1136111352
--- a/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php
1136211353
+++ b/src/Symfony/Component/Serializer/Normalizer/GetSetMethodNormalizer.php
11363-
@@ -138,5 +138,5 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
11354+
@@ -148,5 +148,5 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
1136411355
* @return void
1136511356
*/
1136611357
- protected function setAttributeValue(object $object, string $attribute, mixed $value, string $format = null, array $context = [])
@@ -11379,38 +11370,39 @@ index 40a4fa0e8c..a1e2749aae 100644
1137911370
{
1138011371
$this->normalizer = $normalizer;
1138111372
diff --git a/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php b/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
11382-
index cb43d78cc7..d215ffe997 100644
11373+
index d6d0707ff5..9953ad3005 100644
1138311374
--- a/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
1138411375
+++ b/src/Symfony/Component/Serializer/Normalizer/NormalizerInterface.php
11385-
@@ -37,5 +37,5 @@ interface NormalizerInterface
11376+
@@ -39,5 +39,5 @@ interface NormalizerInterface
1138611377
* @throws ExceptionInterface Occurs for all the other cases of errors
1138711378
*/
1138811379
- public function normalize(mixed $object, string $format = null, array $context = []);
1138911380
+ public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null;
1139011381

1139111382
/**
11392-
@@ -48,4 +48,4 @@ interface NormalizerInterface
11383+
@@ -55,5 +55,5 @@ interface NormalizerInterface
1139311384
* @return bool
1139411385
*/
1139511386
- public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */);
1139611387
+ public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */): bool;
11397-
}
11388+
11389+
/**
1139811390
diff --git a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
11399-
index 8018cb7a49..aa06b9c50b 100644
11391+
index 140e89c6a1..f77348252b 100644
1140011392
--- a/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
1140111393
+++ b/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
11402-
@@ -133,5 +133,5 @@ class ObjectNormalizer extends AbstractObjectNormalizer
11394+
@@ -143,5 +143,5 @@ class ObjectNormalizer extends AbstractObjectNormalizer
1140311395
* @return void
1140411396
*/
1140511397
- protected function setAttributeValue(object $object, string $attribute, mixed $value, string $format = null, array $context = [])
1140611398
+ protected function setAttributeValue(object $object, string $attribute, mixed $value, string $format = null, array $context = []): void
1140711399
{
1140811400
try {
1140911401
diff --git a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
11410-
index 3dd734055d..cbc0e86d27 100644
11402+
index 645ba74290..d960bf4b20 100644
1141111403
--- a/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
1141211404
+++ b/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
11413-
@@ -175,5 +175,5 @@ class PropertyNormalizer extends AbstractObjectNormalizer
11405+
@@ -185,5 +185,5 @@ class PropertyNormalizer extends AbstractObjectNormalizer
1141411406
* @return void
1141511407
*/
1141611408
- protected function setAttributeValue(object $object, string $attribute, mixed $value, string $format = null, array $context = [])

src/Symfony/Component/Serializer/Debug/TraceableNormalizer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ public function __construct(
3333
private NormalizerInterface|DenormalizerInterface $normalizer,
3434
private SerializerDataCollector $dataCollector,
3535
) {
36+
if (!method_exists($normalizer, 'getSupportedTypes')) {
37+
trigger_deprecation('symfony/serializer', '6.3', 'Not implementing the "NormalizerInterface::getSupportedTypes()" in "%s" is deprecated.', get_debug_type($normalizer));
38+
}
3639
}
3740

38-
public function getSupportedTypes(?string $format): ?array
41+
public function getSupportedTypes(?string $format): array
3942
{
4043
// @deprecated remove condition in 7.0
4144
if (!method_exists($this->normalizer, 'getSupportedTypes')) {
42-
return null;
45+
return ['*' => $this->normalizer instanceof CacheableSupportsMethodInterface && $this->normalizer->hasCacheableSupportsMethod()];
4346
}
4447

4548
return $this->normalizer->getSupportedTypes($format);

src/Symfony/Component/Serializer/Debug/TraceableSerializer.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Serializer\DataCollector\SerializerDataCollector;
1515
use Symfony\Component\Serializer\Encoder\DecoderInterface;
1616
use Symfony\Component\Serializer\Encoder\EncoderInterface;
17+
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
1718
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
1819
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1920
use Symfony\Component\Serializer\SerializerInterface;
@@ -29,13 +30,13 @@ class TraceableSerializer implements SerializerInterface, NormalizerInterface, D
2930
{
3031
public const DEBUG_TRACE_ID = 'debug_trace_id';
3132

32-
/**
33-
* @param SerializerInterface&NormalizerInterface&DenormalizerInterface&EncoderInterface&DecoderInterface $serializer
34-
*/
3533
public function __construct(
36-
private SerializerInterface $serializer,
34+
private SerializerInterface&NormalizerInterface&DenormalizerInterface&EncoderInterface&DecoderInterface $serializer,
3735
private SerializerDataCollector $dataCollector,
3836
) {
37+
if (!method_exists($serializer, 'getSupportedTypes')) {
38+
trigger_deprecation('symfony/serializer', '6.3', 'Not implementing the "NormalizerInterface::getSupportedTypes()" in "%s" is deprecated.', get_debug_type($serializer));
39+
}
3940
}
4041

4142
public function serialize(mixed $data, string $format, array $context = []): string
@@ -128,11 +129,11 @@ public function decode(string $data, string $format, array $context = []): mixed
128129
return $result;
129130
}
130131

131-
public function getSupportedTypes(?string $format): ?array
132+
public function getSupportedTypes(?string $format): array
132133
{
133134
// @deprecated remove condition in 7.0
134135
if (!method_exists($this->serializer, 'getSupportedTypes')) {
135-
return null;
136+
return ['*' => $this->serializer instanceof CacheableSupportsMethodInterface && $this->serializer->hasCacheableSupportsMethod()];
136137
}
137138

138139
return $this->serializer->getSupportedTypes($format);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
2727
{
2828
use DenormalizerAwareTrait;
2929

30-
public function getSupportedTypes(?string $format): ?array
30+
public function setDenormalizer(DenormalizerInterface $denormalizer): void
31+
{
32+
if (!method_exists($denormalizer, 'getSupportedTypes')) {
33+
trigger_deprecation('symfony/serializer', '6.3', 'Not implementing the "DenormalizerInterface::getSupportedTypes()" in "%s" is deprecated.', get_debug_type($denormalizer));
34+
}
35+
36+
$this->denormalizer = $denormalizer;
37+
}
38+
39+
public function getSupportedTypes(?string $format): array
3140
{
3241
// @deprecated remove condition in 7.0
3342
if (!method_exists($this->denormalizer, 'getSupportedTypes')) {
34-
return null;
43+
return ['*' => $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod()];
3544
}
3645

3746
return $this->denormalizer->getSupportedTypes($format);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public function __construct(array $defaultContext = [], NameConverterInterface $
3939
$this->nameConverter = $nameConverter;
4040
}
4141

42-
public function getSupportedTypes(?string $format): ?array
42+
public function getSupportedTypes(?string $format): array
4343
{
4444
return [
45-
ConstraintViolationListInterface::class => __CLASS__ === static::class,
45+
ConstraintViolationListInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
4646
];
4747
}
4848

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
2525
public function getSupportedTypes(?string $format): array
2626
{
2727
return [
28-
NormalizableInterface::class => __CLASS__ === static::class,
28+
NormalizableInterface::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
2929
];
3030
}
3131

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
4747

4848
public function getSupportedTypes(?string $format): array
4949
{
50+
$isCacheable = __CLASS__ === static::class || $this->hasCacheableSupportsMethod();
51+
5052
return [
51-
\SplFileInfo::class => __CLASS__ === static::class,
52-
\SplFileObject::class => __CLASS__ === static::class,
53-
File::class => __CLASS__ === static::class,
53+
\SplFileInfo::class => $isCacheable,
54+
\SplFileObject::class => $isCacheable,
55+
File::class => $isCacheable,
5456
];
5557
}
5658

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(array $defaultContext = [])
3636
public function getSupportedTypes(?string $format): array
3737
{
3838
return [
39-
\DateInterval::class => __CLASS__ === static::class,
39+
\DateInterval::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
4040
];
4141
}
4242

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ public function setDefaultContext(array $defaultContext): void
4949

5050
public function getSupportedTypes(?string $format): array
5151
{
52+
$isCacheable = __CLASS__ === static::class || $this->hasCacheableSupportsMethod();
53+
5254
return [
53-
\DateTimeInterface::class => __CLASS__ === static::class,
54-
\DateTimeImmutable::class => __CLASS__ === static::class,
55-
\DateTime::class => __CLASS__ === static::class,
55+
\DateTimeInterface::class => $isCacheable,
56+
\DateTimeImmutable::class => $isCacheable,
57+
\DateTime::class => $isCacheable,
5658
];
5759
}
5860

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterfa
2525
public function getSupportedTypes(?string $format): array
2626
{
2727
return [
28-
\DateTimeZone::class => __CLASS__ === static::class,
28+
\DateTimeZone::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
2929
];
3030
}
3131

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/**
2323
* @author Jordi Boggiano <[email protected]>
2424
*
25-
* @method getSupportedTypes(?string $format): ?array
25+
* @method getSupportedTypes(?string $format): array
2626
*/
2727
interface DenormalizerInterface
2828
{
@@ -65,18 +65,19 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
6565
*/
6666
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */);
6767

68-
/*
69-
* Return the types supported for normalization by this denormalizer for
70-
* this format associated to a boolean value indicating if the result of
71-
* supports*() methods can be cached or if the result can not be cached
72-
* because it depends on the context.
73-
* Returning null means this denormalizer will be considered for
74-
* every format/class.
75-
* Return an empty array if no type is supported for this format.
68+
/**
69+
* Returns the types potentially supported by this denormalizer.
70+
*
71+
* For each supported formats (if applicable), the supported types should be
72+
* returned as keys, and each type should be mapped to a boolean indicating
73+
* if the result of supportsDenormalization() can be cached or not
74+
* (a result cannot be cached when it depends on the context or on the data.)
7675
*
77-
* @param string $format The format being (de-)serialized from or into
76+
* The special type '*' can be used to indicate that the denormalizer might
77+
* support any types. A null value means that the denormalizer does not support
78+
* the corresponding type.
7879
*
79-
* @return array<class-string|string, bool>|null
80+
* @return array<class-string|'*'|string, bool|null>
8081
*/
81-
/* public function getSupportedTypes(?string $format): ?array; */
82+
/* public function getSupportedTypes(?string $format): array; */
8283
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
3636
{
3737
private static $setterAccessibleCache = [];
3838

39-
public function getSupportedTypes(?string $format): ?array
39+
public function getSupportedTypes(?string $format): array
4040
{
41-
return null;
41+
return ['*' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
4242
}
4343

4444
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function normalize(mixed $object, string $format = null, array $context =
4141
public function getSupportedTypes(?string $format): array
4242
{
4343
return [
44-
\JsonSerializable::class => __CLASS__ === static::class,
44+
\JsonSerializable::class => __CLASS__ === static::class || $this->hasCacheableSupportsMethod(),
4545
];
4646
}
4747

0 commit comments

Comments
 (0)