You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 passing 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
if (!array_intersect((array_keys($options)), ['normalizationContext', 'groups', 'context', 'value', 'denormalizationContext'])) {
39
42
// gracefully supports context as first, unnamed attribute argument if it cannot be confused with Doctrine-style options
40
43
$context = $options;
41
44
} 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
+
42
47
// If at least one of the options match, it's likely to be Doctrine-style options. Search for the context inside:
if (!\is_string($groups) && !\is_array($groups)) {
52
+
thrownew \TypeError(sprintf('"%s": Expected parameter $groups to be a string or an array of strings, got "%s".', __METHOD__, get_debug_type($groups)));
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Annotation/Groups.php
+11-2Lines changed: 11 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@
17
17
* Annotation class for @Groups().
18
18
*
19
19
* @Annotation
20
+
* @NamedArgumentConstructor
20
21
* @Target({"PROPERTY", "METHOD"})
21
22
*
22
23
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -30,11 +31,19 @@ class Groups
30
31
private$groups;
31
32
32
33
/**
34
+
* @param string|string[] $groups
35
+
*
33
36
* @throws InvalidArgumentException
34
37
*/
35
-
publicfunction__construct(array$groups)
38
+
publicfunction__construct($groups)
36
39
{
37
-
if (isset($groups['value'])) {
40
+
if (\is_string($groups)) {
41
+
$groups = (array) $groups;
42
+
} elseif (!\is_array($groups)) {
43
+
thrownew \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__);
Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Annotation/MaxDepth.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@
17
17
* Annotation class for @MaxDepth().
18
18
*
19
19
* @Annotation
20
+
* @NamedArgumentConstructor
20
21
* @Target({"PROPERTY", "METHOD"})
21
22
*
22
23
* @author Kévin Dunglas <dunglas@gmail.com>
@@ -30,11 +31,13 @@ class MaxDepth
30
31
private$maxDepth;
31
32
32
33
/**
33
-
* @param int|array $maxDepth
34
+
* @param int $maxDepth
34
35
*/
35
36
publicfunction__construct($maxDepth)
36
37
{
37
38
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
+
38
41
if (!isset($maxDepth['value'])) {
39
42
thrownewInvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
trigger_deprecation('symfony/serializer', '5.3', 'Passing an array as first argument to "%s" is deprecated. Use named arguments instead.', __METHOD__);
40
+
38
41
if (!isset($serializedName['value'])) {
39
42
thrownewInvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
0 commit comments