Skip to content

[Serializer] add return types #42512

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 1 commit into from
Aug 19, 2021
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Annotation/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(string|array $groups)
/**
* @return string[]
*/
public function getGroups()
public function getGroups(): array
{
return $this->groups;
}
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(string $format, array $context = []);
public function supportsDecoding(string $format, array $context = []): bool;
}
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(string $format, array $context = []);
public function supportsEncoding(string $format, array $context = []): bool;
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(array $defaultContext = [])
/**
* {@inheritdoc}
*/
public function encode(mixed $data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = []): string
{
$handle = fopen('php://temp,', 'w+');

Expand Down Expand Up @@ -123,15 +123,15 @@ public function encode(mixed $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding(string $format)
public function supportsEncoding(string $format): bool
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*/
public function decode(string $data, string $format, array $context = [])
public function decode(string $data, string $format, array $context = []): mixed
{
$handle = fopen('php://temp', 'r+');
fwrite($handle, $data);
Expand Down Expand Up @@ -209,7 +209,7 @@ public function decode(string $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDecoding(string $format)
public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ interface DecoderInterface
* are encouraged to document which formats they support in a non-inherited
* phpdoc comment.
*
* @return mixed
*
* @throws UnexpectedValueException
*/
public function decode(string $data, string $format, array $context = []);
public function decode(string $data, string $format, array $context = []): mixed;

/**
* Checks whether the deserializer can decode from given format.
*
* @param string $format Format name
*
* @return bool
*/
public function supportsDecoding(string $format);
public function supportsDecoding(string $format): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,14 @@ interface EncoderInterface
* @param string $format Format name
* @param array $context Options that normalizers/encoders have access to
*
* @return string
*
* @throws UnexpectedValueException
*/
public function encode(mixed $data, string $format, array $context = []);
public function encode(mixed $data, string $format, array $context = []): string;

/**
* Checks whether the serializer can encode to given format.
*
* @param string $format Format name
*
* @return bool
*/
public function supportsEncoding(string $format);
public function supportsEncoding(string $format): bool;
}
6 changes: 2 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ public function __construct(array $defaultContext = [])
* json_decode_options: integer
* Specifies additional options as per documentation for json_decode
*
* @return mixed
*
* @throws NotEncodableValueException
*
* @see https://php.net/json_decode
*/
public function decode(string $data, string $format, array $context = [])
public function decode(string $data, string $format, array $context = []): mixed
{
$associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
Expand All @@ -98,7 +96,7 @@ public function decode(string $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDecoding(string $format)
public function supportsDecoding(string $format): bool
{
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 @@ -34,7 +34,7 @@ public function __construct(array $defaultContext = [])
/**
* {@inheritdoc}
*/
public function encode(mixed $data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = []): string
{
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];

Expand All @@ -58,7 +58,7 @@ public function encode(mixed $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding(string $format)
public function supportsEncoding(string $format): bool
{
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(mixed $data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = []): string
{
return $this->encodingImpl->encode($data, self::FORMAT, $context);
}

/**
* {@inheritdoc}
*/
public function decode(string $data, string $format, array $context = [])
public function decode(string $data, string $format, array $context = []): mixed
{
return $this->decodingImpl->decode($data, self::FORMAT, $context);
}

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

/**
* {@inheritdoc}
*/
public function supportsDecoding(string $format)
public function supportsDecoding(string $format): bool
{
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 @@ -80,7 +80,7 @@ public function __construct(array $defaultContext = [])
/**
* {@inheritdoc}
*/
public function encode(mixed $data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = []): string
{
$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 @@ -108,7 +108,7 @@ public function encode(mixed $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function decode(string $data, string $format, array $context = [])
public function decode(string $data, string $format, array $context = []): mixed
{
if ('' === trim($data)) {
throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
Expand Down Expand Up @@ -175,15 +175,15 @@ public function decode(string $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding(string $format)
public function supportsEncoding(string $format): bool
{
return self::FORMAT === $format;
}

/**
* {@inheritdoc}
*/
public function supportsDecoding(string $format)
public function supportsDecoding(string $format): bool
{
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 @@ -54,7 +54,7 @@ public function __construct(Dumper $dumper = null, Parser $parser = null, array
/**
* {@inheritdoc}
*/
public function encode(mixed $data, string $format, array $context = [])
public function encode(mixed $data, string $format, array $context = []): string
{
$context = array_merge($this->defaultContext, $context);

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

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

Expand All @@ -86,7 +86,7 @@ public function decode(string $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDecoding(string $format)
public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ public function __construct(array $extraAttributes, \Throwable $previous = null)

/**
* Get the extra attributes that are not allowed.
*
* @return array
*/
public function getExtraAttributes()
public function getExtraAttributes(): array
{
return $this->extraAttributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function setMaxDepth(?int $maxDepth)
/**
* {@inheritdoc}
*/
public function getMaxDepth()
public function getMaxDepth(): ?int
{
return $this->maxDepth;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ public function merge(AttributeMetadataInterface $attributeMetadata)
*
* @return string[]
*/
public function __sleep()
public function __sleep(): array
{
return ['name', 'groups', 'maxDepth', 'serializedName', 'ignore', 'normalizationContexts', 'denormalizationContexts'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ public function setMaxDepth(?int $maxDepth);

/**
* Gets the serialization max depth for this attribute.
*
* @return int|null
*/
public function getMaxDepth();
public function getMaxDepth(): ?int;

/**
* Sets the serialization name for this attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function setClassDiscriminatorMapping(ClassDiscriminatorMapping $mapping
*
* @return string[]
*/
public function __sleep()
public function __sleep(): array
{
return [
'name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Mapping\Factory;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;

/**
* Caches metadata using a PSR-6 implementation.
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemP
/**
* {@inheritdoc}
*/
public function getMetadataFor(string|object $value)
public function getMetadataFor(string|object $value): ClassMetadataInterface
{
$class = $this->getClass($value);

Expand All @@ -67,7 +68,7 @@ public function getMetadataFor(string|object $value)
/**
* {@inheritdoc}
*/
public function hasMetadataFor(mixed $value)
public function hasMetadataFor(mixed $value): bool
{
return $this->decorated->hasMetadataFor($value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Mapping\Factory;

use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;

/**
Expand All @@ -38,7 +39,7 @@ public function __construct(LoaderInterface $loader)
/**
* {@inheritdoc}
*/
public function getMetadataFor(string|object $value)
public function getMetadataFor(string|object $value): ClassMetadataInterface
{
$class = $this->getClass($value);

Expand Down Expand Up @@ -67,7 +68,7 @@ public function getMetadataFor(string|object $value)
/**
* {@inheritdoc}
*/
public function hasMetadataFor(mixed $value)
public function hasMetadataFor(mixed $value): bool
{
return \is_object($value) || (\is_string($value) && (class_exists($value) || interface_exists($value, false)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ interface ClassMetadataFactoryInterface
* {@link \Symfony\Component\Serializer\Mapping\Loader\LoaderInterface::loadClassMetadata()} method for further
* configuration. At last, the new object is returned.
*
* @return ClassMetadataInterface
*
* @throws InvalidArgumentException
*/
public function getMetadataFor(string|object $value);
public function getMetadataFor(string|object $value): ClassMetadataInterface;

/**
* Checks if class has metadata.
*
* @return bool
*/
public function hasMetadataFor(mixed $value);
public function hasMetadataFor(mixed $value): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(Reader $reader = null)
/**
* {@inheritdoc}
*/
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
{
$reflectionClass = $classMetadata->getReflectionClass();
$className = $reflectionClass->name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(array $loaders)
/**
* {@inheritdoc}
*/
public function loadClassMetadata(ClassMetadataInterface $metadata)
public function loadClassMetadata(ClassMetadataInterface $metadata): bool
{
$success = false;

Expand All @@ -64,7 +64,7 @@ public function loadClassMetadata(ClassMetadataInterface $metadata)
/**
* @return LoaderInterface[]
*/
public function getLoaders()
public function getLoaders(): array
{
return $this->loaders;
}
Expand Down
Loading