8000 [Serializer] Construct annotations using named arguments · symfony/symfony@c116662 · GitHub
[go: up one dir, main page]

Skip to content

Commit c116662

Browse files
committed
[Serializer] Construct annotations using named arguments
1 parent 3ca3de5 commit c116662

14 files changed

+296
-49
lines changed

UPGRADE-5.3.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ PropertyInfo
9292
Routing
9393
-------
9494

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

9797
Security
9898
--------
@@ -209,7 +209,8 @@ SecurityBundle
209209
Serializer
210210
----------
211211

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

214215
Uid
215216
---

UPGRADE-6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Serializer
305305

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

309310
TwigBundle
310311
----------

src/Symfony/Component/Serializer/Annotation/Context.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Annotation class for @Context().
1818
*
1919
* @Annotation
20+
* @NamedArgumentConstructor
2021
* @Target({"PROPERTY", "METHOD"})
2122
*
2223
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
@@ -30,19 +31,26 @@ final class Context
3031
private $groups;
3132

3233
/**
34+
* @param string|string[] $groups
35+
*
3336
* @throws InvalidArgumentException
3437
*/
35-
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], array $groups = [])
38+
public function __construct(array $options = [], array $context = [], array $normalizationContext = [], array $denormalizationContext = [], $groups = [])
3639
{
3740
if (!$context) {
3841
if (!array_intersect((array_keys($options)), ['normalizationContext', 'groups', 'context', 'value', 'denormalizationContext'])) {
3942
// gracefully supports context as first, unnamed attribute argument if it cannot be confused with Doctrine-style options
4043
$context = $options;
4144
} else {
45+
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array of properties as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
46+
4247
// If at least one of the options match, it's likely to be Doctrine-style options. Search for the context inside:
4348
$context = $options['value'] ?? $options['context'] ?? [];
4449
}
4550
}
51+
if (!\is_string($groups) && !\is_array($groups)) {
52+
throw new \TypeError(sprintf('"%s": Expected parameter $groups to be a string or an array of strings, got "%s".', __METHOD__, get_debug_type($groups)));
53+
}
4654

4755
$normalizationContext = $options['normalizationContext'] ?? $normalizationContext;
4856
$denormalizationContext = $options['denormalizationContext'] ?? $denormalizationContext;

src/Symfony/Component/Serializer/Annotation/DiscriminatorMap.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Annotation class for @DiscriminatorMap().
1818
*
1919
* @Annotation
20+
* @NamedArgumentConstructor
2021
* @Target({"CLASS"})
2122
*
2223
* @author Samuel Roze <samuel.roze@gmail.com>
@@ -35,13 +36,15 @@ class DiscriminatorMap
3536
private $mapping;
3637

3738
/**
38-
* @param string|array $typeProperty
39+
* @param string $typeProperty
3940
*
4041
* @throws InvalidArgumentException
4142
*/
4243
public function __construct($typeProperty, array $mapping = null)
4344
{
4445
if (\is_array($typeProperty)) {
46+
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
47+
4548
$mapping = $typeProperty['mapping'] ?? null;
4649
$typeProperty = $typeProperty['typeProperty'] ?? null;
4750
} elseif (!\is_string($typeProperty)) {

src/Symfony/Component/Serializer/Annotation/Groups.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Annotation class for @Groups().
1818
*
1919
* @Annotation
20+
* @NamedArgumentConstructor
2021
* @Target({"PROPERTY", "METHOD"})
2122
*
2223
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -30,11 +31,19 @@ class Groups
3031
private $groups;
3132

3233
/**
34+
* @param string|string[] $groups
35+
*
3336
* @throws InvalidArgumentException
3437
*/
35-
public function __construct(array $groups)
38+
public function __construct($groups)
3639
{
37-
if (isset($groups['value'])) {
40+
if (\is_string($groups)) {
41+
$groups = (array) $groups;
42+
} elseif (!\is_array($groups)) {
43+
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)));
44+
} elseif (isset($groups['value'])) {
45+
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array of properties as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
46+
3847
$groups = (array) $groups['value'];
3948
}
4049
if (empty($groups)) {

src/Symfony/Component/Serializer/Annotation/MaxDepth.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Annotation class for @MaxDepth().
1818
*
1919
* @Annotation
20+
* @NamedArgumentConstructor
2021
* @Target({"PROPERTY", "METHOD"})
2122
*
2223
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -30,11 +31,13 @@ class MaxDepth
3031
private $maxDepth;
3132

3233
/**
33-
* @param int|array $maxDepth
34+
* @param int $maxDepth
3435
*/
3536
public function __construct($maxDepth)
3637
{
3738
if (\is_array($maxDepth)) {
39+
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
40+
3841
if (!isset($maxDepth['value'])) {
3942
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
4043
}

src/Symfony/Component/Serializer/Annotation/SerializedName.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Annotation class for @SerializedName().
1818
*
1919
* @Annotation
20+
* @NamedArgumentConstructor
2021
* @Target({"PROPERTY", "METHOD"})
2122
*
2223
* @author Fabien Bourigault <bourigaultfabien@gmail.com>
@@ -30,11 +31,13 @@ final class SerializedName
3031
private $serializedName;
3132

3233
/**
33-
* @param string|array $serializedName
34+
* @param string $serializedName
3435
*/
3536
public function __construct($serializedName)
3637
{
3738
if (\is_array($serializedName)) {
39+
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
40+
3841
if (!isset($serializedName['value'])) {
3942
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
4043
}

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
99
* Add normalization formats to `UidNormalizer`
1010
* Add `CsvEncoder::END_OF_LINE` context option
11+
* Deprecate creating instances of the annotation classes by passing an array of parameters, use named arguments instead
1112

1213
5.2.0
1314
-----

0 commit comments

Comments
 (0)
0