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

Skip to content

Commit 25cf787

Browse files
[Serializer] Add wildcard support to getSupportedTypes()
1 parent 3c9b00d commit 25cf787

27 files changed

+188
-164
lines changed

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

Lines changed: 57 additions & 66 deletions
Large diffs are not rendered by default.

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

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public function __construct(PropertyNormalizer $normalizer)
4444

4545
public function getSupportedTypes(?string $format): array
4646
{
47+
$isCacheable = __CLASS__ === static::class || $this->hasCacheableSupportsMethod();
48+
4749
return [
48-
Message::class => true,
49-
Headers::class => true,
50-
HeaderInterface::class => true,
51-
Address::class => true,
52-
AbstractPart::class => true,
50+
Message::class => $isCacheable,
51+
Headers::class => $isCacheable,
52+
HeaderInterface::class => $isCacheable,
53+
Address::class => $isCacheable,
54+
AbstractPart::class => $isCacheable,
5355
];
5456
}
5557

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @author Jordi Boggiano <[email protected]>
2121
*
22-
* @method getSupportedTypes(?string $format): ?array
22+
* @method getSupportedTypes(?string $format): array
2323
*/
2424
interface NormalizerInterface
2525
{
@@ -56,18 +56,19 @@ public function normalize(mixed $object, string $format = null, array $context =
5656
*/
5757
public function supportsNormalization(mixed $data, string $format = null /* , array $context = [] */);
5858

59-
/*
60-
* Return the types supported for normalization by this normalizer for this
61-
* format associated to a boolean value indicating if the result of
62-
* supports*() methods can be cached or if the result can not be cached
63-
* because it depends on the context.
64-
* Returning null means this normalizer will be considered for
65-
* every format/class.
66-
* Return an empty array if no type is supported for this format.
59+
/**
60+
* Returns the types potentially supported by this normalizer.
61+
*
62+
* For each supported formats (if applicable), the supported types should be
63+
* returned as keys, and each type should be mapped to a boolean indicating
64+
* if the result of supportsNormalization() can be cached or not
65+
* (a result cannot be cached when it depends on the context or on the data.)
6766
*
68-
* @param string $format The format being (de-)serialized from or into
67+
* The special type '*' can be used to indicate that the normalizer might
68+
* support any types. A null value means that the normalizer does not support
69+
* the corresponding type.
6970
*
70-
* @return array<class-string|string, bool>|null
71+
* @return array<class-string|'*'|string, bool|null>
7172
*/
72-
/* public function getSupportedTypes(?string $format): ?array; */
73+
/* public function getSupportedTypes(?string $format): array; */
7374
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
4747
$this->objectClassResolver = $objectClassResolver ?? fn ($class) => \is_object($class) ? $class::class : $class;
4848
}
4949

50-
public function getSupportedTypes(?string $format): ?array
50+
public function getSupportedTypes(?string $format): array
5151
{
52-
return null;
52+
return ['*' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
5353
}
5454

5555
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(bool $debug = false, array $defaultContext = [])
4343
public function getSupportedTypes(?string $format): array
4444
{
4545
return [
46-
FlattenException::class => __CLASS__ === self::class,
46+
FlattenException::class => __CLASS__ === self::class || $this->hasCacheableSupportsMethod(),
4747
];
4848
}
4949

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
5454
}
5555
}
5656

57-
public function getSupportedTypes(?string $format): ?array
57+
public function getSupportedTypes(?string $format): array
5858
{
59-
return null;
59+
return ['*' => __CLASS__ === static::class || $this->hasCacheableSupportsMethod()];
6060
}
6161

6262
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function __construct(PropertyAccessorInterface $propertyAccessor = null)
3232
$this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
3333
}
3434

35-
public function getSupportedTypes(?string $format): ?array
35+
public function getSupportedTypes(?string $format): array
3636
{
37-
return null;
37+
return ['*' => false];
3838
}
3939

4040
public function denormalize(mixed $data, string $class, string $format = null, array $context = []): mixed

0 commit comments

Comments
 (0)