8000 [Serializer] [5.0] Add type-hints to serializer interface by Simperfit · Pull Request #32229 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Original file line number Diff line number Diff line change
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, 10000 array $context = []): bool
{
try {
$this->getDecoder($format, $context);
Expand Down
9 changes: 3 additions & 6 deletions src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 = []);
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 = [])
{
$jsonEncodeOptions = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
$encodedJson = json_encode($data, $jsonEncodeOptions);
Expand All @@ -55,7 +55,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 = []);
}
Original file line number Diff line number Diff line change
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 = []);
}
Loading
0