8000 feature #40710 [Serializer] Construct annotations using named argumen… · symfony/symfony@30b73c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30b73c7

Browse files
committed
feature #40710 [Serializer] Construct annotations using named arguments (derrabus)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Serializer] Construct annotations using named arguments | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | N/A | License | MIT | Doc PR | Not needed This is the same as #40266, but applied to the serializer annotations. This PR proposes to bump the `doctrine/annotations` library to 1.12 to gain access to its emulation layer for named arguments. Furthermore, constructing any of the serializer's annotation classes the old way by pas 8000 sing an array of parameters is deprecated. ### Reasons for this change The constructors of our annotation classes have become unnecessarily complicated because we have to support two ways of calling them: * An array of parameters, passed as first argument, because that's the default behavior `doctrine/annotations`. * A set of named arguments because that's how PHP 8 attributes work. Since we can now tell the Doctrine annotation reader to use named arguments as well, we can simplify the constructors of our annotations significantly. ### Drawback After this change, there is no easy way anymore to construct instances of most of the annotation classes directly on PHP 7. The PR has been built under the assumption that instances of this class are usually created using either Doctrine annotations or a PHP 8 attribute. Thus, most applications should be unaffected by this change. Commits ------- c116662 [Serializer] Construct annotations using named arguments
2 parents 8fc4c1b + c116662 commit 30b73c7

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