10000 [Serializer] Construct annotations using named arguments by derrabus · Pull Request #40710 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Construct annotations using named arguments #40710

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
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
5 changes: 3 additions & 2 deletions UPGRADE-5.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ PropertyInfo
Routing
-------

* Deprecated creating instances of the `Route` annotation class by passing an array of parameters, use named arguments instead
* Deprecate creating instances of the `Route` annotation class by passing an array of parameters, use named arguments instead

Security
--------
Expand Down Expand Up @@ -209,7 +209,8 @@ SecurityBundle
Serializer
----------

* Deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
* Deprecate `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
* Deprecate creating instances of the annotation classes by passing an array of parameters, use named arguments instead

Uid
---
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ Serializer

* Removed `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.
* `ArrayDenormalizer` does not implement `SerializerAwareInterface` anymore.
* The annotation classes cannot be constructed by passing an array of parameters as first argument anymore, use named arguments instead

TwigBundle
----------
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Serializer/Annotation/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Annotation class for @Context().
*
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY", "METHOD"})
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
Expand All @@ -30,19 +31,26 @@ final class Context
private $groups;

/**
* @param string|string[] $groups
*
* @throws InvalidArgumentException
*/
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], array $groups = [])
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], $groups = [])
{
if (!$context) {
if (!array_intersect((array_keys($options)), ['normalizationContext', 'groups', 'context', 'value', 'denormalizationContext'])) {
// gracefully supports context as first, unnamed attribute argument if it cannot be confused with Doctrine-style options
$context = $options;
} else {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array of properties as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

// If at least one of the options match, it's likely to be Doctrine-style options. Search for the context inside:
$context = $options['value'] ?? $options['context'] ?? [];
}
}
if (!\is_string($groups) && !\is_array($groups)) {
throw new \TypeError(sprintf('"%s": Expected parameter $groups to be a string or an array of strings, got "%s".', __METHOD__, get_debug_type($groups)));
}

$normalizationContext = $options['normalizationContext'] ?? $normalizationContext;
$denormalizationContext = $options['denormalizationContext'] ?? $denormalizationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Annotation class for @DiscriminatorMap().
*
* @Annotation
* @NamedArgumentConstructor
* @Target({"CLASS"})
*
* @author Samuel Roze <samuel.roze@gmail.com>
Expand All @@ -35,13 +36,15 @@ class DiscriminatorMap
private $mapping;

/**
* @param string|array $typeProperty
* @param string $typeProperty
*
* @throws InvalidArgumentException
*/
public function __construct($typeProperty, array $mapping = null)
{
if (\is_array($typeProperty)) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

$mapping = $typeProperty['mapping'] ?? null;
$typeProperty = $typeProperty['typeProperty'] ?? null;
} elseif (!\is_string($typeProperty)) {
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/Serializer/Annotation/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Annotation class for @Groups().
*
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY", "METHOD"})
*
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -30,11 +31,19 @@ class Groups
private $groups;

/**
* @param string|string[] $groups
*
* @throws InvalidArgumentException
*/
public function __construct(array $groups)
public function __construct($groups)
{
if (isset($groups['value'])) {
if (\is_string($groups)) {
$groups = (array) $groups;
} elseif (!\is_array($groups)) {
throw new \TypeError(sprintf('"%s": Parameter $groups is expected to be a string or an array of strings, got "%s".', __METHOD__, get_debug_type($groups)));
} elseif (isset($groups['value'])) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array of properties as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

$groups = (array) $groups['value'];
}
if (empty($groups)) {
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Serializer/Annotation/MaxDepth.php
341A
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Annotation class for @MaxDepth().
*
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY", "METHOD"})
*
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -30,11 +31,13 @@ class MaxDepth
private $maxDepth;

/**
* @param int|array $maxDepth
* @param int $maxDepth
*/
public function __construct($maxDepth)
{
if (\is_array($maxDepth)) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

if (!isset($maxDepth['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Annotation class for @SerializedName().
*
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY", "METHOD"})
*
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
Expand All @@ -30,11 +31,13 @@ final class SerializedName
private $serializedName;

/**
* @param string|array $serializedName
* @param string $serializedName
*/
public function __construct($serializedName)
{
if (\is_array($serializedName)) {
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);

if (!isset($serializedName['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Deprecate `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
* Add normalization formats to `UidNormalizer`
* Add `CsvEncoder::END_OF_LINE` context option
* Deprecate creating instances of the annotation classes by passing an array of parameters, use named arguments instead

5.2.0
-----
Expand Down
Loading
0