minor #32229 [Serializer] [5.0] Add type-hints to serializer interface (Simperfit)

This PR was merged into the 5.0-dev branch.

Discussion
----------

[Serializer] [5.0] Add type-hints to serializer interface

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #32179   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | none <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

This PR adds type-hints to the serializer component ;).

Commits
-------

68b588cf45 [Serializer] [5.0] Add type-hints to serializer interface
This commit is contained in:
Fabien Potencier 2019-06-29 08:28:05 +02:00
commit 0781b6090c
31 changed files with 83 additions and 86 deletions

View File

@ -35,7 +35,7 @@ class ChainDecoder implements ContextAwareDecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
final public function decode($data, $format, array $context = []) final public function decode(string $data, string $format, array $context = [])
{ {
return $this->getDecoder($format, $context)->decode($data, $format, $context); return $this->getDecoder($format, $context)->decode($data, $format, $context);
} }
@ -43,7 +43,7 @@ class ChainDecoder implements ContextAwareDecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDecoding($format, array $context = []): bool public function supportsDecoding(string $format, array $context = []): bool
{ {
try { try {
$this->getDecoder($format, $context); $this->getDecoder($format, $context);

View File

@ -35,7 +35,7 @@ class ChainEncoder implements ContextAwareEncoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
final public function encode($data, $format, array $context = []) final public function encode($data, string $format, array $context = [])
{ {
return $this->getEncoder($format, $context)->encode($data, $format, $context); return $this->getEncoder($format, $context)->encode($data, $format, $context);
} }
@ -43,7 +43,7 @@ class ChainEncoder implements ContextAwareEncoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsEncoding($format, array $context = []): bool public function supportsEncoding(string $format, array $context = []): bool
{ {
try { try {
$this->getEncoder($format, $context); $this->getEncoder($format, $context);
@ -56,11 +56,8 @@ class ChainEncoder implements ContextAwareEncoderInterface
/** /**
* Checks whether the normalization is needed for the given format. * Checks whether the normalization is needed for the given format.
*
* @param string $format
* @param array $context
*/ */
public function needsNormalization($format, array $context = []): bool public function needsNormalization(string $format, array $context = []): bool
{ {
$encoder = $this->getEncoder($format, $context); $encoder = $this->getEncoder($format, $context);

View File

@ -23,5 +23,5 @@ interface ContextAwareDecoderInterface extends DecoderInterface
* *
* @param array $context options that decoders have access to * @param array $context options that decoders have access to
*/ */
public function supportsDecoding($format, array $context = []); public function supportsDecoding(string $format, array $context = []);
} }

View File

@ -23,5 +23,5 @@ interface ContextAwareEncoderInterface extends EncoderInterface
* *
* @param array $context options that encoders have access to * @param array $context options that encoders have access to
*/ */
public function supportsEncoding($format, array $context = []); public function supportsEncoding(string $format, array $context = []);
} }

View File

@ -51,7 +51,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function encode($data, $format, array $context = []) public function encode($data, string $format, array $context = [])
{ {
$handle = fopen('php://temp,', 'w+'); $handle = fopen('php://temp,', 'w+');
@ -110,7 +110,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function decode($data, $format, array $context = []) public function decode(string $data, string $format, array $context = [])
{ {
$handle = fopen('php://temp', 'r+'); $handle = fopen('php://temp', 'r+');
fwrite($handle, $data); fwrite($handle, $data);

View File

@ -36,7 +36,7 @@ interface DecoderInterface
* *
* @throws UnexpectedValueException * @throws UnexpectedValueException
*/ */
public function decode($data, $format, array $context = []); public function decode(string $data, string $format, array $context = []);
/** /**
* Checks whether the deserializer can decode from given format. * Checks whether the deserializer can decode from given format.
@ -45,5 +45,5 @@ interface DecoderInterface
* *
* @return bool * @return bool
*/ */
public function supportsDecoding($format); public function supportsDecoding(string $format);
} }

View File

@ -31,7 +31,7 @@ interface EncoderInterface
* *
* @throws UnexpectedValueException * @throws UnexpectedValueException
*/ */
public function encode($data, $format, array $context = []); public function encode($data, string $format, array $context = []);
/** /**
* Checks whether the serializer can encode to given format. * Checks whether the serializer can encode to given format.
@ -40,5 +40,5 @@ interface EncoderInterface
* *
* @return bool * @return bool
*/ */
public function supportsEncoding($format); public function supportsEncoding(string $format);
} }

View File

@ -72,7 +72,7 @@ class JsonDecode implements DecoderInterface
* *
* @see http://php.net/json_decode json_decode * @see http://php.net/json_decode json_decode
*/ */
public function decode($data, $format, array $context = []) public function decode(string $data, string $format, array $context = [])
{ {
$associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE]; $associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH]; $recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
@ -98,7 +98,7 @@ class JsonDecode implements DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDecoding($format) public function supportsDecoding(string $format)
{ {
return JsonEncoder::FORMAT === $format; return JsonEncoder::FORMAT === $format;
} }

View File

@ -36,7 +36,7 @@ class JsonEncode implements EncoderInterface
* *
* {@inheritdoc} * {@inheritdoc}
*/ */
public function encode($data, $format, array $context = []) public function encode($data, string $format, array $context = [])
{ {
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS]; $options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
@ -60,7 +60,7 @@ class JsonEncode implements EncoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsEncoding($format) public function supportsEncoding(string $format)
{ {
return JsonEncoder::FORMAT === $format; return JsonEncoder::FORMAT === $format;
} }

View File

@ -32,7 +32,7 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function encode($data, $format, array $context = []) public function encode($data, string $format, array $context = [])
{ {
return $this->encodingImpl->encode($data, self::FORMAT, $context); return $this->encodingImpl->encode($data, self::FORMAT, $context);
} }
@ -40,7 +40,7 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function decode($data, $format, array $context = []) public function decode(string $data, string $format, array $context = [])
{ {
return $this->decodingImpl->decode($data, self::FORMAT, $context); return $this->decodingImpl->decode($data, self::FORMAT, $context);
} }
@ -48,7 +48,7 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsEncoding($format) public function supportsEncoding(string $format)
{ {
return self::FORMAT === $format; return self::FORMAT === $format;
} }
@ -56,7 +56,7 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDecoding($format) public function supportsDecoding(string $format)
{ {
return self::FORMAT === $format; return self::FORMAT === $format;
} }

View File

@ -79,7 +79,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function encode($data, $format, array $context = []) public function encode($data, string $format, array $context = [])
{ {
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES]; $encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
$ignorePiNode = \in_array(XML_PI_NODE, $encoderIgnoredNodeTypes, true); $ignorePiNode = \in_array(XML_PI_NODE, $encoderIgnoredNodeTypes, true);
@ -107,7 +107,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function decode($data, $format, array $context = []) public function decode(string $data, string $format, array $context = [])
{ {
if ('' === trim($data)) { if ('' === trim($data)) {
throw new NotEncodableValueException('Invalid XML data, it can not be empty.'); throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
@ -176,7 +176,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsEncoding($format) public function supportsEncoding(string $format)
{ {
return self::FORMAT === $format; return self::FORMAT === $format;
} }
@ -184,7 +184,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDecoding($format) public function supportsDecoding(string $format)
{ {
return self::FORMAT === $format; return self::FORMAT === $format;
} }

View File

@ -43,7 +43,7 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function encode($data, $format, array $context = []) public function encode($data, string $format, array $context = [])
{ {
$context = array_merge($this->defaultContext, $context); $context = array_merge($this->defaultContext, $context);
@ -53,7 +53,7 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsEncoding($format) public function supportsEncoding(string $format)
{ {
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format; return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
} }
@ -61,7 +61,7 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function decode($data, $format, array $context = []) public function decode(string $data, string $format, array $context = [])
{ {
$context = array_merge($this->defaultContext, $context); $context = array_merge($this->defaultContext, $context);
@ -71,7 +71,7 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDecoding($format) public function supportsDecoding(string $format)
{ {
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format; return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
} }

View File

@ -34,7 +34,7 @@ interface AttributeMetadataInterface
* *
* @param string $group * @param string $group
*/ */
public function addGroup($group); public function addGroup(string $group);
/** /**
* Gets groups of this attribute. * Gets groups of this attribute.
@ -48,7 +48,7 @@ interface AttributeMetadataInterface
* *
* @param int|null $maxDepth * @param int|null $maxDepth
*/ */
public function setMaxDepth($maxDepth); public function setMaxDepth(?int $maxDepth);
/** /**
* Gets the serialization max depth for this attribute. * Gets the serialization max depth for this attribute.

View File

@ -34,7 +34,7 @@ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($propertyName) public function normalize(string $propertyName)
{ {
if (null === $this->attributes || \in_array($propertyName, $this->attributes)) { if (null === $this->attributes || \in_array($propertyName, $this->attributes)) {
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName))); return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
@ -46,7 +46,7 @@ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($propertyName) public function denormalize(string $propertyName)
{ {
$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) { $camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]); return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);

View File

@ -25,7 +25,7 @@ interface NameConverterInterface
* *
* @return string * @return string
*/ */
public function normalize($propertyName); public function normalize(string $propertyName);
/** /**
* Converts a property name to its denormalized value. * Converts a property name to its denormalized value.
@ -34,5 +34,5 @@ interface NameConverterInterface
* *
* @return string * @return string
*/ */
public function denormalize($propertyName); public function denormalize(string $propertyName);
} }

View File

@ -121,7 +121,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return \is_object($data) && !$data instanceof \Traversable; return \is_object($data) && !$data instanceof \Traversable;
} }
@ -129,7 +129,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if (!isset($context['cache_key'])) { if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context); $context['cache_key'] = $this->getCacheKey($format, $context);
@ -233,7 +233,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* *
* @return string[] * @return string[]
*/ */
protected function getAttributes($object, $format = null, array $context) protected function getAttributes($object, string $format = null, array $context)
{ {
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object); $class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
$key = $class.'-'.$context['cache_key']; $key = $class.'-'.$context['cache_key'];
@ -274,7 +274,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* *
* @return string[] * @return string[]
*/ */
abstract protected function extractAttributes($object, $format = null, array $context = []); abstract protected function extractAttributes($object, string $format = null, array $context = []);
/** /**
* Gets the attribute value. * Gets the attribute value.
@ -286,12 +286,12 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
* *
* @return mixed * @return mixed
*/ */
abstract protected function getAttributeValue($object, $attribute, $format = null, array $context = []); abstract protected function getAttributeValue($object, string $attribute, string $format = null, array $context = []);
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type)); return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
} }
@ -299,7 +299,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
if (!isset($context['cache_key'])) { if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context); $context['cache_key'] = $this->getCacheKey($format, $context);

View File

@ -23,5 +23,5 @@ interface ContextAwareDenormalizerInterface extends DenormalizerInterface
* *
* @param array $context options that denormalizers have access to * @param array $context options that denormalizers have access to
*/ */
public function supportsDenormalization($data, $type, $format = null, array $context = []); public function supportsDenormalization($data, string $type, string $format = null, array $context = []);
} }

View File

@ -23,5 +23,5 @@ interface ContextAwareNormalizerInterface extends NormalizerInterface
* *
* @param array $context options that normalizers have access to * @param array $context options that normalizers have access to
*/ */
public function supportsNormalization($data, $format = null, array $context = []); public function supportsNormalization($data, string $format = null, array $context = []);
} }

View File

@ -25,7 +25,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
return $object->normalize($this->serializer, $format, $context); return $object->normalize($this->serializer, $format, $context);
} }
@ -33,7 +33,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
$object = $this->extractObjectToPopulate($class, $context) ?: new $class(); $object = $this->extractObjectToPopulate($class, $context) ?: new $class();
$object->denormalize($this->serializer, $data, $format, $context); $object->denormalize($this->serializer, $data, $format, $context);
@ -49,7 +49,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
* *
* @return bool * @return bool
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return $data instanceof NormalizableInterface; return $data instanceof NormalizableInterface;
} }
@ -63,7 +63,7 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
* *
* @return bool * @return bool
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return is_subclass_of($type, DenormalizableInterface::class); return is_subclass_of($type, DenormalizableInterface::class);
} }

View File

@ -48,7 +48,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if (!$object instanceof \SplFileInfo) { if (!$object instanceof \SplFileInfo) {
throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".'); throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
@ -74,7 +74,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return $data instanceof \SplFileInfo; return $data instanceof \SplFileInfo;
} }
@ -89,7 +89,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
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)) { 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)) {
throw new NotNormalizableValueException('The provided "data:" URI is not valid.'); throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
@ -118,7 +118,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return isset(self::$supportedTypes[$type]); return isset(self::$supportedTypes[$type]);
} }

View File

@ -38,7 +38,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if (!$object instanceof \DateInterval) { if (!$object instanceof \DateInterval) {
throw new InvalidArgumentException('The object must be an instance of "\DateInterval".'); throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
@ -50,7 +50,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return $data instanceof \DateInterval; return $data instanceof \DateInterval;
} }
@ -69,7 +69,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @throws UnexpectedValueException * @throws UnexpectedValueException
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
if (!\is_string($data)) { if (!\is_string($data)) {
throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data))); throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data)));
@ -96,7 +96,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return \DateInterval::class === $type; return \DateInterval::class === $type;
} }

View File

@ -46,7 +46,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if (!$object instanceof \DateTimeInterface) { if (!$object instanceof \DateTimeInterface) {
throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".'); throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".');
@ -66,7 +66,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return $data instanceof \DateTimeInterface; return $data instanceof \DateTimeInterface;
} }
@ -76,7 +76,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
* *
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null; $dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
$timezone = $this->getTimezone($context); $timezone = $this->getTimezone($context);
@ -113,7 +113,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return isset(self::$supportedTypes[$type]); return isset(self::$supportedTypes[$type]);
} }

View File

@ -26,7 +26,7 @@ class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterfa
* *
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if (!$object instanceof \DateTimeZone) { if (!$object instanceof \DateTimeZone) {
throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".'); throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
@ -38,7 +38,7 @@ class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return $data instanceof \DateTimeZone; return $data instanceof \DateTimeZone;
} }
@ -48,7 +48,7 @@ class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterfa
* *
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
if ('' === $data || null === $data) { if ('' === $data || null === $data) {
throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.'); 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 @@ class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return \DateTimeZone::class === $type; return \DateTimeZone::class === $type;
} }

View File

@ -44,7 +44,7 @@ interface DenormalizerInterface
* @throws RuntimeException Occurs if the class cannot be instantiated * @throws RuntimeException Occurs if the class cannot be instantiated
* @throws ExceptionInterface Occurs for all the other cases of errors * @throws ExceptionInterface Occurs for all the other cases of errors
*/ */
public function denormalize($data, $class, $format = null, array $context = []); public function denormalize($data, string $class, string $format = null, array $context = []);
/** /**
* Checks whether the given class is supported for denormalization by this normalizer. * Checks whether the given class is supported for denormalization by this normalizer.
@ -55,5 +55,5 @@ interface DenormalizerInterface
* *
* @return bool * @return bool
*/ */
public function supportsDenormalization($data, $type, $format = null); public function supportsDenormalization($data, string $type, string $format = null);
} }

View File

@ -39,7 +39,7 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data)); return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data));
} }
@ -47,7 +47,7 @@ class GetSetMethodNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
} }

View File

@ -24,7 +24,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function normalize($object, $format = null, array $context = []) public function normalize($object, string $format = null, array $context = [])
{ {
if ($this->isCircularReference($object, $context)) { if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object); return $this->handleCircularReference($object);
@ -44,7 +44,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return $data instanceof \JsonSerializable; return $data instanceof \JsonSerializable;
} }
@ -52,7 +52,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return false; return false;
} }
@ -60,7 +60,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $class, string $format = null, array $context = [])
{ {
throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class)); throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class));
} }

View File

@ -35,5 +35,5 @@ interface NormalizableInterface
* *
* @return array|string|int|float|bool * @return array|string|int|float|bool
*/ */
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []); public function normalize(NormalizerInterface $normalizer, string $format = null, array $context = []);
} }

View File

@ -38,7 +38,7 @@ interface NormalizerInterface
* @throws LogicException Occurs when the normalizer is not called in an expected context * @throws LogicException Occurs when the normalizer is not called in an expected context
* @throws ExceptionInterface Occurs for all the other cases of errors * @throws ExceptionInterface Occurs for all the other cases of errors
*/ */
public function normalize($object, $format = null, array $context = []); public function normalize($object, string $format = null, array $context = []);
/** /**
* Checks whether the given class is supported for normalization by this normalizer. * Checks whether the given class is supported for normalization by this normalizer.
@ -48,5 +48,5 @@ interface NormalizerInterface
* *
* @return bool * @return bool
*/ */
public function supportsNormalization($data, $format = null); public function supportsNormalization($data, string $format = null);
} }

View File

@ -60,7 +60,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function extractAttributes($object, $format = null, array $context = []) protected function extractAttributes($object, string $format = null, array $context = [])
{ {
// If not using groups, detect manually // If not using groups, detect manually
$attributes = []; $attributes = [];
@ -118,7 +118,7 @@ class ObjectNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getAttributeValue($object, $attribute, $format = null, array $context = []) protected function getAttributeValue($object, $attribute, string $format = null, array $context = [])
{ {
$cacheKey = \get_class($object); $cacheKey = \get_class($object);
if (!\array_key_exists($cacheKey, $this->discriminatorCache)) { if (!\array_key_exists($cacheKey, $this->discriminatorCache)) {

View File

@ -33,7 +33,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsNormalization($data, $format = null) public function supportsNormalization($data, string $format = null)
{ {
return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data)); return parent::supportsNormalization($data, $format) && $this->supports(\get_class($data));
} }
@ -41,7 +41,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function supportsDenormalization($data, $type, $format = null) public function supportsDenormalization($data, $type, string $format = null)
{ {
return parent::supportsDenormalization($data, $type, $format) && $this->supports($type); return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
} }
@ -97,7 +97,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function extractAttributes($object, $format = null, array $context = []) protected function extractAttributes($object, string $format = null, array $context = [])
{ {
$reflectionObject = new \ReflectionObject($object); $reflectionObject = new \ReflectionObject($object);
$attributes = []; $attributes = [];
@ -118,7 +118,7 @@ class PropertyNormalizer extends AbstractObjectNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getAttributeValue($object, $attribute, $format = null, array $context = []) protected function getAttributeValue($object, $attribute, string $format = null, array $context = [])
{ {
try { try {
$reflectionProperty = $this->getReflectionProperty($object, $attribute); $reflectionProperty = $this->getReflectionProperty($object, $attribute);

View File

@ -27,7 +27,7 @@ interface SerializerInterface
* *
* @return string * @return string
*/ */
public function serialize($data, $format, array $context = []); public function serialize($data, string $format, array $context = []);
/** /**
* Deserializes data into the given type. * Deserializes data into the given type.
@ -39,5 +39,5 @@ interface SerializerInterface
* *
* @return object * @return object
*/ */
public function deserialize($data, $type, $format, array $context = []); public function deserialize($data, string $type, string $format, array $context = []);
} }