Skip to content

Commit

Permalink
minor #32229 [Serializer] [5.0] Add type-hints to serializer interfac…
Browse files Browse the repository at this point in the history
…e (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
-------

68b588c [Serializer] [5.0] Add type-hints to serializer interface
  • Loading branch information
fabpot committed Jun 29, 2019
2 parents aac5765 + 68b588c commit 0781b60
Show file tree
Hide file tree
Showing 31 changed files with 83 additions and 86 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Expand Up @@ -35,15 +35,15 @@ public function __construct(array $decoders = [])
/**
* {@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);
}

/**
* {@inheritdoc}
*/
public function supportsDecoding($format, array $context = []): bool
public function supportsDecoding(string $format, array $context = []): bool
{
try {
$this->getDecoder($format, $context);
Expand Down
9 changes: 3 additions & 6 deletions src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Expand Up @@ -35,15 +35,15 @@ public function __construct(array $encoders = [])
/**
* {@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);
}

/**
* {@inheritdoc}
*/
public function supportsEncoding($format, array $context = []): bool
public function supportsEncoding(string $format, array $context = []): bool
{
try {
$this->getEncoder($format, $context);
Expand All @@ -56,11 +56,8 @@ public function supportsEncoding($format, array $context = []): bool

/**
* 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);

Expand Down
Expand Up @@ -23,5 +23,5 @@ interface ContextAwareDecoderInterface extends DecoderInterface
*
* @param array $context options that decoders have access to
*/
public function supportsDecoding($format, array $context = []);
public function supportsDecoding(string $format, array $context = []);
}
Expand Up @@ -23,5 +23,5 @@ interface ContextAwareEncoderInterface extends EncoderInterface
*
* @param array $context options that encoders have access to
*/
public function supportsEncoding($format, array $context = []);
public function supportsEncoding(string $format, array $context = []);
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -51,7 +51,7 @@ public function __construct(array $defaultContext = [])
/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = [])
public function encode($data, string $format, array $context = [])
{
$handle = fopen('php://temp,', 'w+');

Expand Down Expand Up @@ -110,7 +110,7 @@ public function supportsEncoding($format)
/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = [])
public function decode(string $data, string $format, array $context = [])
{
$handle = fopen('php://temp', 'r+');
fwrite($handle, $data);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/DecoderInterface.php
Expand Up @@ -36,7 +36,7 @@ interface DecoderInterface
*
* @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.
Expand All @@ -45,5 +45,5 @@ public function decode($data, $format, array $context = []);
*
* @return bool
*/
public function supportsDecoding($format);
public function supportsDecoding(string $format);
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/EncoderInterface.php
Expand Up @@ -31,7 +31,7 @@ interface EncoderInterface
*
* @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.
Expand All @@ -40,5 +40,5 @@ public function encode($data, $format, array $context = []);
*
* @return bool
*/
public function supportsEncoding($format);
public function supportsEncoding(string $format);
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Expand Up @@ -72,7 +72,7 @@ public function __construct(array $defaultContext = [])
*
* @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];
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
Expand All @@ -98,7 +98,7 @@ public function decode($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
public function supportsDecoding(string $format)
{
return JsonEncoder::FORMAT === $format;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Expand Up @@ -36,7 +36,7 @@ public function __construct(array $defaultContext = [])
*
* {@inheritdoc}
*/
public function encode($data, $format, array $context = [])
public function encode($data, string $format, array $context = [])
{
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];

Expand All @@ -60,7 +60,7 @@ public function encode($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding(string $format)
{
return JsonEncoder::FORMAT === $format;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/JsonEncoder.php
Expand Up @@ -32,31 +32,31 @@ public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodin
/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = [])
public function encode($data, string $format, array $context = [])
{
return $this->encodingImpl->encode($data, self::FORMAT, $context);
}

/**
* {@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);
}

/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding(string $format)
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
public function supportsDecoding(string $format)
{
return self::FORMAT === $format;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -79,7 +79,7 @@ public function __construct(array $defaultContext = [])
/**
* {@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];
$ignorePiNode = \in_array(XML_PI_NODE, $encoderIgnoredNodeTypes, true);
Expand Down Expand Up @@ -107,7 +107,7 @@ public function encode($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = [])
public function decode(string $data, string $format, array $context = [])
{
if ('' === trim($data)) {
throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
Expand Down Expand Up @@ -176,15 +176,15 @@ public function decode($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding(string $format)
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
public function supportsDecoding(string $format)
{
return self::FORMAT === $format;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/YamlEncoder.php
Expand Up @@ -43,7 +43,7 @@ public function __construct(Dumper $dumper = null, Parser $parser = null, array
/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = [])
public function encode($data, string $format, array $context = [])
{
$context = array_merge($this->defaultContext, $context);

Expand All @@ -53,15 +53,15 @@ public function encode($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding(string $format)
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}

/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = [])
public function decode(string $data, string $format, array $context = [])
{
$context = array_merge($this->defaultContext, $context);

Expand All @@ -71,7 +71,7 @@ public function decode($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
public function supportsDecoding(string $format)
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function getName();
*
* @param string $group
*/
public function addGroup($group);
public function addGroup(string $group);

/**
* Gets groups of this attribute.
Expand All @@ -48,7 +48,7 @@ public function getGroups();
*
* @param int|null $maxDepth
*/
public function setMaxDepth($maxDepth);
public function setMaxDepth(?int $maxDepth);

/**
* Gets the serialization max depth for this attribute.
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function __construct(array $attributes = null, bool $lowerCamelCase = tru
/**
* {@inheritdoc}
*/
public function normalize($propertyName)
public function normalize(string $propertyName)
{
if (null === $this->attributes || \in_array($propertyName, $this->attributes)) {
return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
Expand All @@ -46,7 +46,7 @@ public function normalize($propertyName)
/**
* {@inheritdoc}
*/
public function denormalize($propertyName)
public function denormalize(string $propertyName)
{
$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
Expand Down
Expand Up @@ -25,7 +25,7 @@ interface NameConverterInterface
*
* @return string
*/
public function normalize($propertyName);
public function normalize(string $propertyName);

/**
* Converts a property name to its denormalized value.
Expand All @@ -34,5 +34,5 @@ public function normalize($propertyName);
*
* @return string
*/
public function denormalize($propertyName);
public function denormalize(string $propertyName);
}
Expand Up @@ -121,15 +121,15 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, string $format = null)
{
return \is_object($data) && !$data instanceof \Traversable;
}

/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
public function normalize($object, string $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context);
Expand Down Expand Up @@ -233,7 +233,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
*
* @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);
$key = $class.'-'.$context['cache_key'];
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function getAttributes($object, $format = null, array $context)
*
* @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.
Expand All @@ -286,20 +286,20 @@ abstract protected function extractAttributes($object, $format = null, array $co
*
* @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}
*/
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));
}

/**
* {@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'])) {
$context['cache_key'] = $this->getCacheKey($format, $context);
Expand Down
Expand Up @@ -23,5 +23,5 @@ interface ContextAwareDenormalizerInterface extends DenormalizerInterface
*
* @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 = []);
}
Expand Up @@ -23,5 +23,5 @@ interface ContextAwareNormalizerInterface extends NormalizerInterface
*
* @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 = []);
}

0 comments on commit 0781b60

Please sign in to comment.