Skip to content

Commit cb0a724

Browse files
committed
minor symfony#59496 [Serializer] Correct all implementations of the NormalizerInterface to have the correct method signature (Kefisu)
This PR was merged into the 7.3 branch. Discussion ---------- [Serializer] Correct all implementations of the `NormalizerInterface` to have the correct method signature | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#59495 | License | MIT This PR corrects the signatures of all normalizers implementing the NormalizerInterface of the Serializer component effectively changing: `function normalize($object, ... )` to `function normalize(mixed $data, ...)` as is in line with the interface. Whilst Named Arguments is against the BC promise of Symfony, with the current implementation of the normalizers it is impossible to use named arguments with the Strategy Pattern using the Serializer component, with this change it is possible. Commits ------- 7336e7f Correct all implementations of the NormalizerInterface to have the correct method signature
2 parents bc08cb4 + 7336e7f commit cb0a724

23 files changed

+111
-111
lines changed

src/Symfony/Component/Messenger/Transport/Serialization/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ final class FlattenExceptionNormalizer implements DenormalizerInterface, Normali
2626
{
2727
use NormalizerAwareTrait;
2828

29-
public function normalize(mixed $object, ?string $format = null, array $context = []): array
29+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
3030
{
3131
return [
32-
'message' => $object->getMessage(),
33-
'code' => $object->getCode(),
34-
'headers' => $object->getHeaders(),
35-
'class' => $object->getClass(),
36-
'file' => $object->getFile(),
37-
'line' => $object->getLine(),
38-
'previous' => null === $object->getPrevious() ? null : $this->normalize($object->getPrevious(), $format, $context),
39-
'status' => $object->getStatusCode(),
40-
'status_text' => $object->getStatusText(),
41-
'trace' => $object->getTrace(),
42-
'trace_as_string' => $object->getTraceAsString(),
32+
'message' => $data->getMessage(),
33+
'code' => $data->getCode(),
34+
'headers' => $data->getHeaders(),
35+
'class' => $data->getClass(),
36+
'file' => $data->getFile(),
37+
'line' => $data->getLine(),
38+
'previous' => null === $data->getPrevious() ? null : $this->normalize($data->getPrevious(), $format, $context),
39+
'status' => $data->getStatusCode(),
40+
'status_text' => $data->getStatusText(),
41+
'trace' => $data->getTrace(),
42+
'trace_as_string' => $data->getTraceAsString(),
4343
];
4444
}
4545

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public function getSupportedTypes(?string $format): array
4040
return $this->normalizer->getSupportedTypes($format);
4141
}
4242

43-
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
43+
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
4444
{
4545
if (!$this->normalizer instanceof NormalizerInterface) {
4646
throw new \BadMethodCallException(\sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".', __METHOD__, NormalizerInterface::class));
4747
}
4848

4949
$startTime = microtime(true);
50-
$normalized = $this->normalizer->normalize($object, $format, $context);
50+
$normalized = $this->normalizer->normalize($data, $format, $context);
5151
$time = microtime(true) - $startTime;
5252

5353
if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ public function deserialize(mixed $data, string $type, string $format, array $co
6666
return $result;
6767
}
6868

69-
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
69+
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
7070
{
7171
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));
7272

7373
$startTime = microtime(true);
74-
$result = $this->serializer->normalize($object, $format, $context);
74+
$result = $this->serializer->normalize($data, $format, $context);
7575
$time = microtime(true) - $startTime;
7676

7777
$caller = $this->getCaller(__FUNCTION__, NormalizerInterface::class);
7878

79-
$this->dataCollector->collectNormalize($traceId, $object, $format, $context, $time, $caller, $this->serializerName);
79+
$this->dataCollector->collectNormalize($traceId, $data, $format, $context, $time, $caller, $this->serializerName);
8080

8181
return $result;
8282
}

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function supportsNormalization(mixed $data, ?string $format = null, array
154154
return \is_object($data) && !$data instanceof \Traversable;
155155
}
156156

157-
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
157+
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
158158
{
159159
$context['_read_attributes'] = true;
160160

@@ -164,14 +164,14 @@ public function normalize(mixed $object, ?string $format = null, array $context
164164

165165
$this->validateCallbackContext($context);
166166

167-
if ($this->isCircularReference($object, $context)) {
168-
return $this->handleCircularReference($object, $format, $context);
167+
if ($this->isCircularReference($data, $context)) {
168+
return $this->handleCircularReference($data, $format, $context);
169169
}
170170

171-
$data = [];
171+
$normalizedData = [];
172172
$stack = [];
173-
$attributes = $this->getAttributes($object, $format, $context);
174-
$class = ($this->objectClassResolver)($object);
173+
$attributes = $this->getAttributes($data, $format, $context);
174+
$class = ($this->objectClassResolver)($data);
175175
$classMetadata = $this->classMetadataFactory?->getMetadataFor($class);
176176
$attributesMetadata = $this->classMetadataFactory?->getMetadataFor($class)->getAttributesMetadata();
177177
if (isset($context[self::MAX_DEPTH_HANDLER])) {
@@ -189,12 +189,12 @@ public function normalize(mixed $object, ?string $format = null, array $context
189189
continue;
190190
}
191191

192-
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
192+
$attributeContext = $this->getAttributeNormalizationContext($data, $attribute, $context);
193193

194194
try {
195-
$attributeValue = $attribute === $this->classDiscriminatorResolver?->getMappingForMappedObject($object)?->getTypeProperty()
196-
? $this->classDiscriminatorResolver?->getTypeForMappedObject($object)
197-
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
195+
$attributeValue = $attribute === $this->classDiscriminatorResolver?->getMappingForMappedObject($data)?->getTypeProperty()
196+
? $this->classDiscriminatorResolver?->getTypeForMappedObject($data)
197+
: $this->getAttributeValue($data, $attribute, $format, $attributeContext);
198198
} catch (UninitializedPropertyException|\Error $e) {
199199
if (($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e)) {
200200
continue;
@@ -203,17 +203,17 @@ public function normalize(mixed $object, ?string $format = null, array $context
203203
}
204204

205205
if ($maxDepthReached) {
206-
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $attributeContext);
206+
$attributeValue = $maxDepthHandler($attributeValue, $data, $attribute, $format, $attributeContext);
207207
}
208208

209-
$stack[$attribute] = $this->applyCallbacks($attributeValue, $object, $attribute, $format, $attributeContext);
209+
$stack[$attribute] = $this->applyCallbacks($attributeValue, $data, $attribute, $format, $attributeContext);
210210
}
211211

212212
foreach ($stack as $attribute => $attributeValue) {
213-
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
213+
$attributeContext = $this->getAttributeNormalizationContext($data, $attribute, $context);
214214

215215
if (null === $attributeValue || \is_scalar($attributeValue)) {
216-
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
216+
$normalizedData = $this->updateData($normalizedData, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
217217
continue;
218218
}
219219

@@ -223,15 +223,15 @@ public function normalize(mixed $object, ?string $format = null, array $context
223223

224224
$childContext = $this->createChildContext($attributeContext, $attribute, $format);
225225

226-
$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $childContext), $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
226+
$normalizedData = $this->updateData($normalizedData, $attribute, $this->serializer->normalize($attributeValue, $format, $childContext), $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
227227
}
228228

229229
$preserveEmptyObjects = $context[self::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[self::PRESERVE_EMPTY_OBJECTS] ?? false;
230-
if ($preserveEmptyObjects && !$data) {
230+
if ($preserveEmptyObjects && !$normalizedData) {
231231
return new \ArrayObject();
232232
}
233233

234-
return $data;
234+
return $normalizedData;
235235
}
236236

237237
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, ?string $format = null): object

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ public function getSupportedTypes(?string $format): array
3333
];
3434
}
3535

36-
public function normalize(mixed $object, ?string $format = null, array $context = []): int|string
36+
public function normalize(mixed $data, ?string $format = null, array $context = []): int|string
3737
{
38-
if (!$object instanceof \BackedEnum) {
38+
if (!$data instanceof \BackedEnum) {
3939
throw new InvalidArgumentException('The data must belong to a backed enumeration.');
4040
}
4141

42-
return $object->value;
42+
return $data->value;
4343
}
4444

4545
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getSupportedTypes(?string $format): array
4343
];
4444
}
4545

46-
public function normalize(mixed $object, ?string $format = null, array $context = []): array
46+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
4747
{
4848
if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
4949
$payloadFieldsToSerialize = $context[self::PAYLOAD_FIELDS];
@@ -59,7 +59,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
5959

6060
$violations = [];
6161
$messages = [];
62-
foreach ($object as $violation) {
62+
foreach ($data as $violation) {
6363
$propertyPath = $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), null, $format, $context) : $violation->getPropertyPath();
6464

6565
$violationEntry = [

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function getSupportedTypes(?string $format): array
3030
];
3131
}
3232

33-
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
33+
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
3434
{
35-
return $object->normalize($this->serializer, $format, $context);
35+
return $data->normalize($this->serializer, $format, $context);
3636
}
3737

3838
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,27 @@ public function getSupportedTypes(?string $format): array
4747
return self::SUPPORTED_TYPES;
4848
}
4949

50-
public function normalize(mixed $object, ?string $format = null, array $context = []): string
50+
public function normalize(mixed $data, ?string $format = null, array $context = []): string
5151
{
52-
if (!$object instanceof \SplFileInfo) {
52+
if (!$data instanceof \SplFileInfo) {
5353
throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
5454
}
5555

56-
$mimeType = $this->getMimeType($object);
57-
$splFileObject = $this->extractSplFileObject($object);
56+
$mimeType = $this->getMimeType($data);
57+
$splFileObject = $this->extractSplFileObject($data);
5858

59-
$data = '';
59+
$splFileData = '';
6060

6161
$splFileObject->rewind();
6262
while (!$splFileObject->eof()) {
63-
$data .= $splFileObject->fgets();
63+
$splFileData .= $splFileObject->fgets();
6464
}
6565

6666
if ('text' === explode('/', $mimeType, 2)[0]) {
67-
return \sprintf('data:%s,%s', $mimeType, rawurlencode($data));
67+
return \sprintf('data:%s,%s', $mimeType, rawurlencode($splFileData));
6868
}
6969

70-
return \sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
70+
return \sprintf('data:%s;base64,%s', $mimeType, base64_encode($splFileData));
7171
}
7272

7373
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public function getSupportedTypes(?string $format): array
4343
/**
4444
* @throws InvalidArgumentException
4545
*/
46-
public function normalize(mixed $object, ?string $format = null, array $context = []): string
46+
public function normalize(mixed $data, ?string $format = null, array $context = []): string
4747
{
48-
if (!$object instanceof \DateInterval) {
48+
if (!$data instanceof \DateInterval) {
4949
throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
5050
}
5151

52-
return $object->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
52+
return $data->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
5353
}
5454

5555
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ public function getSupportedTypes(?string $format): array
5656
/**
5757
* @throws InvalidArgumentException
5858
*/
59-
public function normalize(mixed $object, ?string $format = null, array $context = []): int|float|string
59+
public function normalize(mixed $data, ?string $format = null, array $context = []): int|float|string
6060
{
61-
if (!$object instanceof \DateTimeInterface) {
61+
if (!$data instanceof \DateTimeInterface) {
6262
throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".');
6363
}
6464

6565
$dateTimeFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
6666
$timezone = $this->getTimezone($context);
6767

6868
if (null !== $timezone) {
69-
$object = clone $object;
70-
$object = $object->setTimezone($timezone);
69+
$data = clone $data;
70+
$data = $data->setTimezone($timezone);
7171
}
7272

7373
return match ($context[self::CAST_KEY] ?? $this->defaultContext[self::CAST_KEY] ?? false) {
74-
'int' => (int) $object->format($dateTimeFormat),
75-
'float' => (float) $object->format($dateTimeFormat),
76-
default => $object->format($dateTimeFormat),
74+
'int' => (int) $data->format($dateTimeFormat),
75+
'float' => (float) $data->format($dateTimeFormat),
76+
default => $data->format($dateTimeFormat),
7777
};
7878
}
7979

0 commit comments

Comments
 (0)