8000 minor #33185 [Serializer] Add more parameter types (derrabus) · symfony/symfony@64d2be5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 64d2be5

Browse files
committed
minor #33185 [Serializer] Add more parameter types (derrabus)
This PR was merged into the 5.0-dev branch. Discussion ---------- [Serializer] Add more parameter types | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #32179 | License | MIT | Doc PR | N/A We missed quite a lot in the Serializer, so I decided to open a separate PR instead of merging the changes into #33154. Commits ------- 73b17a8 [Serializer] Add more parameter types.
2 parents 6450d79 + 73b17a8 commit 64d2be5

34 files changed

+109
-127
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function encode($data, string $format, array $context = [])
102102
/**
103103
* {@inheritdoc}
104104
*/
105-
public function supportsEncoding($format)
105+
public function supportsEncoding(string $format)
106106
{
107107
return self::FORMAT === $format;
108108
}
@@ -184,7 +184,7 @@ public function decode(string $data, string $format, array $context = [])
184184
/**
185185
* {@inheritdoc}
186186
*/
187-
public function supportsDecoding($format)
187+
public function supportsDecoding(string $format)
188188
{
189189
return self::FORMAT === $format;
190190
}

src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(PropertyListExtractorInterface $propertyListExtracto
3030
/**
3131
* {@inheritdoc}
3232
*/
33-
public function getProperties($object, array $context = []): ?array
33+
public function getProperties(object $object, array $context = []): ?array
3434
{
3535
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
3636

src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractorInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ interface ObjectPropertyListExtractorInterface
1919
/**
2020
* Gets the list of properties available for the given object.
2121
*
22-
* @param object $object
23-
*
2422
* @return string[]|null
2523
*/
26-
public function getProperties($object, array $context = []): ?array;
24+
public function getProperties(object $object, array $context = []): ?array;
2725
}

src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getName()
6666
/**
6767
* {@inheritdoc}
6868
*/
69-
public function addGroup($group)
69+
public function addGroup(string $group)
7070
{
7171
if (!\in_array($group, $this->groups)) {
7272
$this->groups[] = $group;
@@ -84,7 +84,7 @@ public function getGroups()
8484
/**
8585
* {@inheritdoc}
8686
*/
87-
public function setMaxDepth($maxDepth)
87+
public function setMaxDepth(?int $maxDepth)
8888
{
8989
$this->maxDepth = $maxDepth;
9090
}

src/Symfony/Component/Serializer/NameConverter/AdvancedNameConverterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ interface AdvancedNameConverterInterface extends NameConverterInterface
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
public function normalize($propertyName, string $class = null, string $format = null, array $context = []);
24+
public function normalize(string $propertyName, string $class = null, string $format = null, array $context = []);
2525

2626
/**
2727
* {@inheritdoc}
2828
*/
29-
public function denormalize($propertyName, string $class = null, string $format = null, array $context = []);
29+
public function denormalize(string $propertyName, string $class = null, string $format = null, array $context = []);
3030
}

src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(ClassMetadataFactoryInterface $metadataFactory, Name
4040
/**
4141
* {@inheritdoc}
4242
*/
43-
public function normalize($propertyName, string $class = null, string $format = null, array $context = []): string
43+
public function normalize(string $propertyName, string $class = null, string $format = null, array $context = []): string
4444
{
4545
if (null === $class) {
4646
return $this->normalizeFallback($propertyName, $class, $format, $context);
@@ -56,7 +56,7 @@ public function normalize($propertyName, string $class = null, string $format =
5656
/**
5757
* {@inheritdoc}
5858
*/
59-
public function denormalize($propertyName, string $class = null, string $format = null, array $context = []): string
59+
public function denormalize(string $propertyName, string $class = null, string $format = null, array $context = []): string
6060
{
6161
if (null === $class) {
6262
return $this->denormalizeFallback($propertyName, $class, $format, $context);

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,11 @@ public function hasCacheableSupportsMethod(): bool
170170
/**
171171
* Detects if the configured circular reference limit is reached.
172172
*
173-
* @param object $object
174-
* @param array $context
175-
*
176173
* @return bool
177174
*
178175
* @throws CircularReferenceException
179176
*/
180-
protected function isCircularReference($object, &$context)
177+
protected function isCircularReference(object $object, array &$context)
181178
{
182179
$objectHash = spl_object_hash($object);
183180

@@ -229,7 +226,7 @@ protected function handleCircularReference(object $object, string $format = null
229226
*
230227
* @return string[]|AttributeMetadataInterface[]|bool
231228
*/
232-
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
229+
protected function getAllowedAttributes($classOrObject, array $context, bool $attributesAsString = false)
233230
{
234231
$allowExtraAttributes = $context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES];
235232
if (!$this->classMetadataFactory) {
@@ -265,12 +262,10 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
265262
* Is this attribute allowed?
266263
*
267264
* @param object|string $classOrObject
268-
* @param string $attribute
269-
* @param string|null $format
270265
*
271266
* @return bool
272267
*/
273-
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
268+
protected function isAllowedAttribute($classOrObject, string $attribute, string $format = null, array $context = [])
274269
{
275270
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES];
276271
if (\in_array($attribute, $ignoredAttributes)) {
@@ -307,12 +302,11 @@ protected function prepareForDenormalization($data)
307302
* Returns the method to use to construct an object. This method must be either
308303
* the object constructor or static.
309304
*
310-
* @param string $class
311305
* @param array|bool $allowedAttributes
312306
*
313307
* @return \ReflectionMethod|null
314308
*/
315-
protected function getConstructor(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
309+
protected function getConstructor(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
316310
{
317311
return $reflectionClass->getConstructor();
318312
}
@@ -325,15 +319,14 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
325319
* is removed from the context before being returned to avoid side effects
326320
* when recursively normalizing an object graph.
327321
*
328-
* @param string $class
329322
* @param array|bool $allowedAttributes
330323
*
331324
* @return object
332325
*
333326
* @throws RuntimeException
334327
* @throws MissingConstructorArgumentsException
335328
*/
336-
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
329+
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
337330
{
338331
if (null !== $object = $this->extractObjectToPopulate($class, $context, self::OBJECT_TO_POPULATE)) {
339332
unset($context[self::OBJECT_TO_POPULATE]);
@@ -408,7 +401,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
408401
/**
409402
* @internal
410403
*/
411-
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
404+
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, $parameterData, array $context, string $format = null)
412405
{
413406
try {
414407
if (null !== $parameter->getClass()) {

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public function normalize($object, string $format = null, array $context = [])
211211
/**
212212
* {@inheritdoc}
213213
*/
214-
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
214+
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
215215
{
216216
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
217217
if (!isset($data[$mapping->getTypeProperty()])) {
@@ -272,33 +272,29 @@ protected function getAttributes($object, string $format = null, array $context)
272272
/**
273273
* Extracts attributes to normalize from the class of the given object, format and context.
274274
*
275-
* @param object $object
276-
*
277275
* @return string[]
278276
*/
279-
abstract protected function extractAttributes($object, string $format = null, array $context = []);
277+
abstract protected function extractAttributes(object $object, string $format = null, array $context = []);
280278

281279
/**
282280
* Gets the attribute value.
283281
*
284-
* @param object $object
285-
*
286282
* @return mixed
287283
*/
288-
abstract protected function getAttributeValue($object, string $attribute, string $format = null, array $context = []);
284+
abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []);
289285

290286
/**
291287
* {@inheritdoc}
292288
*/
293-
public function supportsDenormalization($data, $type, string $format = null)
289+
public function supportsDenormalization($data, string $type, string $format = null)
294290
{
295291
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
296292
}
297293

298294
/**
299295
* {@inheritdoc}
300296
*/
301-
public function denormalize($data, $type, string $format = null, array $context = [])
297+
public function denormalize($data, string $type, string $format = null, array $context = [])
302298
{
303299
if (!isset($context['cache_key'])) {
304300
$context['cache_key'] = $this->getCacheKey($format, $context);
@@ -349,13 +345,8 @@ public function denormalize($data, $type, string $format = null, array $context
349345

350346
/**
351347
* Sets attribute value.
352-
*
353-
* @param object $object
354-
* @param string $attribute
355-
* @param mixed $value
356-
* @param string|null $format
357348
*/
358-
abstract protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []);
349+
abstract protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = []);
359350

360351
/**
361352
* Validates the submitted data and denormalizes it.
@@ -435,7 +426,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
435426
/**
436427
* @internal
437428
*/
438-
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
429+
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, $parameterData, array $context, string $format = null)
439430
{
440431
if (null === $this->propertyTypeExtractor || null === $types = $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) {
441432
return parent::denormalizeParameter($class, $parameter, $parameterName, $parameterData, $context, $format);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Serializer
3636
*
3737
* @throws NotNormalizableValueException
3838
*/
39-
public function denormalize($data, $type, $format = null, array $context = [])
39+
public function denormalize($data, string $type, string $format = null, array $context = [])
4040
{
4141
if (null === $this->serializer) {
4242
throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
@@ -66,7 +66,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
6666
/**
6767
* {@inheritdoc}
6868
*/
69-
public function supportsDenormalization($data, $type, $format = null, array $context = [])
69+
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
7070
{
7171
return '[]' === substr($type, -2)
7272
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct($defaultContext = [], NameConverterInterface $nameCo
4141
/**
4242
* {@inheritdoc}
4343
*/
44-
public function normalize($object, $format = null, array $context = [])
44+
public function normalize($object, string $format = null, array $context = [])
4545
{
4646
$violations = [];
4747
$messages = [];
@@ -83,7 +83,7 @@ public function normalize($object, $format = null, array $context = [])
8383
/**
8484
* {@inheritdoc}
8585
*/
86-
public function supportsNormalization($data, $format = null)
86+
public function supportsNormalization($data, string $format = null)
8787
{
8888
return $data instanceof ConstraintViolationListInterface;
8989
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function normalize($object, string $format = null, array $context = [])
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
public function denormalize($data, $type, string $format = null, array $context = [])
36+
public function denormalize($data, string $type, string $format = null, array $context = [])
3737
{
3838
$object = $this->extractObjectToPopulate($type, $context) ?: new $type();
3939
$object->denormalize($this->serializer, $data, $format, $context);
@@ -63,7 +63,7 @@ public function supportsNormalization($data, string $format = null)
6363
*
6464
* @return bool
6565
*/
66-
public function supportsDenormalization($data, $type, string $format = null)
66+
public function supportsDenormalization($data, string $type, string $format = null)
6767
{
6868
return is_subclass_of($type, DenormalizableInterface::class);
6969
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function supportsNormalization($data, string $format = null)
8989
* @throws InvalidArgumentException
9090
* @throws NotNormalizableValueException
9191
*/
92-
public function denormalize($data, $type, string $format = null, array $context = [])
92+
public function denormalize($data, string $type, string $format = null, array $context = [])
9393
{
9494
if (!preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i', $data)) {
9595
throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
@@ -118,7 +118,7 @@ public function denormalize($data, $type, string $format = null, array $context
118118
/**
119119
* {@inheritdoc}
120120
*/
121-
public function supportsDenormalization($data, $type, string $format = null)
121+
public function supportsDenormalization($data, string $type, string $format = null)
122122
{
123123
return isset(self::$supportedTypes[$type]);
124124
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function hasCacheableSupportsMethod(): bool
6969
* @throws InvalidArgumentException
7070
* @throws UnexpectedValueException
7171
*/
72-
public function denormalize($data, $type, string $format = null, array $context = [])
72+
public function denormalize($data, string $type, string $format = null, array $context = [])
7373
{
7474
if (!\is_string($data)) {
7575
throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data)));
@@ -118,7 +118,7 @@ public function denormalize($data, $type, string $format = null, array $context
118118
/**
119119
* {@inheritdoc}
120120
*/
121-
public function supportsDenormalization($data, $type, string $format = null)
121+
public function supportsDenormalization($data, string $type, string $format = null)
122122
{
123123
return \DateInterval::class === $type;
124124
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function supportsNormalization($data, string $format = null)
7676
*
7777
* @throws NotNormalizableValueException
7878
*/
79-
public function denormalize($data, $type, string $format = null, array $context = [])
79+
public function denormalize($data, string $type, string $format = null, array $context = [])
8080
{
8181
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
8282
$timezone = $this->getTimezone($context);
@@ -113,7 +113,7 @@ public function denormalize($data, $type, string $format = null, array $context
113113
/**
114114
* {@inheritdoc}
115115
*/
116-
public function supportsDenormalization($data, $type, string $format = null)
116+
public function supportsDenormalization($data, string $type, string $format = null)
117117
{
118118
return isset(self::$supportedTypes[$type]);
119119
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function supportsNormalization($data, string $format = null)
4848
*
4949
* @throws NotNormalizableValueException
5050
*/
51-
public function denormalize($data, $type, string $format = null, array $context = [])
51+
public function denormalize($data, string $type, string $format = null, array $context = [])
5252
{
5353
if ('' === $data || null === $data) {
5454
throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.');
@@ -64,7 +64,7 @@ public function denormalize($data, $type, string $format = null, array $context
6464
/**
6565
* {@inheritdoc}
6666
*/
67-
public function supportsDenormalization($data, $type, string $format = null)
67+
public function supportsDenormalization($data, string $type, string $format = null)
6868
{
6969
return \DateTimeZone::class === $type;
7070
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ interface DenormalizableInterface
3636
*
3737
* @return object|object[]
3838
*/
39-
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = []);
39+
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = []);
4040
}

0 commit comments

Comments
 (0)
0