Navigation Menu

Skip to content

Commit

Permalink
minor #33185 [Serializer] Add more parameter types (derrabus)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Tobion committed Aug 17, 2019
2 parents 6450d79 + 73b17a8 commit 64d2be5
Show file tree
Hide file tree
Showing 34 changed files with 109 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -102,7 +102,7 @@ public function encode($data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding(string $format)
{
return self::FORMAT === $format;
}
Expand Down Expand Up @@ -184,7 +184,7 @@ public function decode(string $data, string $format, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
public function supportsDecoding(string $format)
{
return self::FORMAT === $format;
}
Expand Down
Expand Up @@ -30,7 +30,7 @@ public function __construct(PropertyListExtractorInterface $propertyListExtracto
/**
* {@inheritdoc}
*/
public function getProperties($object, array $context = []): ?array
public function getProperties(object $object, array $context = []): ?array
{
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);

Expand Down
Expand Up @@ -19,9 +19,7 @@ interface ObjectPropertyListExtractorInterface
/**
* Gets the list of properties available for the given object.
*
* @param object $object
*
* @return string[]|null
*/
public function getProperties($object, array $context = []): ?array;
public function getProperties(object $object, array $context = []): ?array;
}
Expand Up @@ -66,7 +66,7 @@ public function getName()
/**
* {@inheritdoc}
*/
public function addGroup($group)
public function addGroup(string $group)
{
if (!\in_array($group, $this->groups)) {
$this->groups[] = $group;
Expand All @@ -84,7 +84,7 @@ public function getGroups()
/**
* {@inheritdoc}
*/
public function setMaxDepth($maxDepth)
public function setMaxDepth(?int $maxDepth)
{
$this->maxDepth = $maxDepth;
}
Expand Down
Expand Up @@ -21,10 +21,10 @@ interface AdvancedNameConverterInterface extends NameConverterInterface
/**
* {@inheritdoc}
*/
public function normalize($propertyName, string $class = null, string $format = null, array $context = []);
public function normalize(string $propertyName, string $class = null, string $format = null, array $context = []);

/**
* {@inheritdoc}
*/
public function denormalize($propertyName, string $class = null, string $format = null, array $context = []);
public function denormalize(string $propertyName, string $class = null, string $format = null, array $context = []);
}
Expand Up @@ -40,7 +40,7 @@ public function __construct(ClassMetadataFactoryInterface $metadataFactory, Name
/**
* {@inheritdoc}
*/
public function normalize($propertyName, string $class = null, string $format = null, array $context = []): string
public function normalize(string $propertyName, string $class = null, string $format = null, array $context = []): string
{
if (null === $class) {
return $this->normalizeFallback($propertyName, $class, $format, $context);
Expand All @@ -56,7 +56,7 @@ public function normalize($propertyName, string $class = null, string $format =
/**
* {@inheritdoc}
*/
public function denormalize($propertyName, string $class = null, string $format = null, array $context = []): string
public function denormalize(string $propertyName, string $class = null, string $format = null, array $context = []): string
{
if (null === $class) {
return $this->denormalizeFallback($propertyName, $class, $format, $context);
Expand Down
19 changes: 6 additions & 13 deletions src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Expand Up @@ -170,14 +170,11 @@ public function hasCacheableSupportsMethod(): bool
/**
* Detects if the configured circular reference limit is reached.
*
* @param object $object
* @param array $context
*
* @return bool
*
* @throws CircularReferenceException
*/
protected function isCircularReference($object, &$context)
protected function isCircularReference(object $object, array &$context)
{
$objectHash = spl_object_hash($object);

Expand Down Expand Up @@ -229,7 +226,7 @@ protected function handleCircularReference(object $object, string $format = null
*
* @return string[]|AttributeMetadataInterface[]|bool
*/
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
protected function getAllowedAttributes($classOrObject, array $context, bool $attributesAsString = false)
{
$allowExtraAttributes = $context[self::ALLOW_EXTRA_ATTRIBUTES] ?? $this->defaultContext[self::ALLOW_EXTRA_ATTRIBUTES];
if (!$this->classMetadataFactory) {
Expand Down Expand Up @@ -265,12 +262,10 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu
* Is this attribute allowed?
*
* @param object|string $classOrObject
* @param string $attribute
* @param string|null $format
*
* @return bool
*/
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = [])
protected function isAllowedAttribute($classOrObject, string $attribute, string $format = null, array $context = [])
{
$ignoredAttributes = $context[self::IGNORED_ATTRIBUTES] ?? $this->defaultContext[self::IGNORED_ATTRIBUTES];
if (\in_array($attribute, $ignoredAttributes)) {
Expand Down Expand Up @@ -307,12 +302,11 @@ protected function prepareForDenormalization($data)
* Returns the method to use to construct an object. This method must be either
* the object constructor or static.
*
* @param string $class
* @param array|bool $allowedAttributes
*
* @return \ReflectionMethod|null
*/
protected function getConstructor(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
protected function getConstructor(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
{
return $reflectionClass->getConstructor();
}
Expand All @@ -325,15 +319,14 @@ protected function getConstructor(array &$data, $class, array &$context, \Reflec
* is removed from the context before being returned to avoid side effects
* when recursively normalizing an object graph.
*
* @param string $class
* @param array|bool $allowedAttributes
*
* @return object
*
* @throws RuntimeException
* @throws MissingConstructorArgumentsException
*/
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
{
if (null !== $object = $this->extractObjectToPopulate($class, $context, self::OBJECT_TO_POPULATE)) {
unset($context[self::OBJECT_TO_POPULATE]);
Expand Down Expand Up @@ -408,7 +401,7 @@ protected function instantiateObject(array &$data, $class, array &$context, \Ref
/**
* @internal
*/
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, $parameterData, array $context, string $format = null)
{
try {
if (null !== $parameter->getClass()) {
Expand Down
Expand Up @@ -211,7 +211,7 @@ public function normalize($object, string $format = null, array $context = [])
/**
* {@inheritdoc}
*/
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes, string $format = null)
{
if ($this->classDiscriminatorResolver && $mapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
if (!isset($data[$mapping->getTypeProperty()])) {
Expand Down Expand Up @@ -272,33 +272,29 @@ protected function getAttributes($object, string $format = null, array $context)
/**
* Extracts attributes to normalize from the class of the given object, format and context.
*
* @param object $object
*
* @return string[]
*/
abstract protected function extractAttributes($object, string $format = null, array $context = []);
abstract protected function extractAttributes(object $object, string $format = null, array $context = []);

/**
* Gets the attribute value.
*
* @param object $object
*
* @return mixed
*/
abstract protected function getAttributeValue($object, string $attribute, string $format = null, array $context = []);
abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []);

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, string $format = null)
public function supportsDenormalization($data, string $type, string $format = null)
{
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
}

/**
* {@inheritdoc}
*/
public function denormalize($data, $type, string $format = null, array $context = [])
public function denormalize($data, string $type, string $format = null, array $context = [])
{
if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context);
Expand Down Expand Up @@ -349,13 +345,8 @@ public function denormalize($data, $type, string $format = null, array $context

/**
* Sets attribute value.
*
* @param object $object
* @param string $attribute
* @param mixed $value
* @param string|null $format
*/
abstract protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []);
abstract protected function setAttributeValue(object $object, string $attribute, $value, string $format = null, array $context = []);

/**
* Validates the submitted data and denormalizes it.
Expand Down Expand Up @@ -435,7 +426,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
/**
* @internal
*/
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, $parameterName, $parameterData, array $context, $format = null)
protected function denormalizeParameter(\ReflectionClass $class, \ReflectionParameter $parameter, string $parameterName, $parameterData, array $context, string $format = null)
{
if (null === $this->propertyTypeExtractor || null === $types = $this->propertyTypeExtractor->getTypes($class->getName(), $parameterName)) {
return parent::denormalizeParameter($class, $parameter, $parameterName, $parameterData, $context, $format);
Expand Down
Expand Up @@ -36,7 +36,7 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Serializer
*
* @throws NotNormalizableValueException
*/
public function denormalize($data, $type, $format = null, array $context = [])
public function denormalize($data, string $type, string $format = null, array $context = [])
{
if (null === $this->serializer) {
throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
Expand Down Expand Up @@ -66,7 +66,7 @@ public function denormalize($data, $type, $format = null, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null, array $context = [])
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
{
return '[]' === substr($type, -2)
&& $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public function __construct($defaultContext = [], NameConverterInterface $nameCo
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = [])
public function normalize($object, string $format = null, array $context = [])
{
$violations = [];
$messages = [];
Expand Down Expand Up @@ -83,7 +83,7 @@ public function normalize($object, $format = null, array $context = [])
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
public function supportsNormalization($data, string $format = null)
{
return $data instanceof ConstraintViolationListInterface;
}
Expand Down
Expand Up @@ -33,7 +33,7 @@ public function normalize($object, string $format = null, array $context = [])
/**
* {@inheritdoc}
*/
public function denormalize($data, $type, string $format = null, array $context = [])
public function denormalize($data, string $type, string $format = null, array $context = [])
{
$object = $this->extractObjectToPopulate($type, $context) ?: new $type();
$object->denormalize($this->serializer, $data, $format, $context);
Expand Down Expand Up @@ -63,7 +63,7 @@ public function supportsNormalization($data, string $format = null)
*
* @return bool
*/
public function supportsDenormalization($data, $type, string $format = null)
public function supportsDenormalization($data, string $type, string $format = null)
{
return is_subclass_of($type, DenormalizableInterface::class);
}
Expand Down
Expand Up @@ -89,7 +89,7 @@ public function supportsNormalization($data, string $format = null)
* @throws InvalidArgumentException
* @throws NotNormalizableValueException
*/
public function denormalize($data, $type, string $format = null, array $context = [])
public function denormalize($data, string $type, 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)) {
throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
Expand Down Expand Up @@ -118,7 +118,7 @@ public function denormalize($data, $type, string $format = null, array $context
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, string $format = null)
public function supportsDenormalization($data, string $type, string $format = null)
{
return isset(self::$supportedTypes[$type]);
}
Expand Down
Expand Up @@ -69,7 +69,7 @@ public function hasCacheableSupportsMethod(): bool
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*/
public function denormalize($data, $type, string $format = null, array $context = [])
public function denormalize($data, string $type, string $format = null, array $context = [])
{
if (!\is_string($data)) {
throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data)));
Expand Down Expand Up @@ -118,7 +118,7 @@ public function denormalize($data, $type, string $format = null, array $context
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, string $format = null)
public function supportsDenormalization($data, string $type, string $format = null)
{
return \DateInterval::class === $type;
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public function supportsNormalization($data, string $format = null)
*
* @throws NotNormalizableValueException
*/
public function denormalize($data, $type, string $format = null, array $context = [])
public function denormalize($data, string $type, string $format = null, array $context = [])
{
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
$timezone = $this->getTimezone($context);
Expand Down Expand Up @@ -113,7 +113,7 @@ public function denormalize($data, $type, string $format = null, array $context
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, string $format = null)
public function supportsDenormalization($data, string $type, string $format = null)
{
return isset(self::$supportedTypes[$type]);
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public function supportsNormalization($data, string $format = null)
*
* @throws NotNormalizableValueException
*/
public function denormalize($data, $type, string $format = null, array $context = [])
public function denormalize($data, string $type, string $format = null, array $context = [])
{
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.');
Expand All @@ -64,7 +64,7 @@ public function denormalize($data, $type, string $format = null, array $context
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, string $format = null)
public function supportsDenormalization($data, string $type, string $format = null)
{
return \DateTimeZone::class === $type;
}
Expand Down
Expand Up @@ -36,5 +36,5 @@ interface DenormalizableInterface
*
* @return object|object[]
*/
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = []);
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = []);
}

0 comments on commit 64d2be5

Please sign in to comment.