Skip to content

Commit f2acd82

Browse files
feature symfony#61418 [Validator] Deprecate implementing __sleep/wakeup() on GenericMetadata implementations (nicolas-grekas)
This PR was merged into the 7.4 branch. Discussion ---------- [Validator] Deprecate implementing `__sleep/wakeup()` on GenericMetadata implementations | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Issues | - | License | MIT Similar to symfony#61412 Commits ------- 5c68128 [Validator] Deprecate implementing `__sleep/wakeup()` on GenericMetadata implementations
2 parents 8ac3fb5 + 5c68128 commit f2acd82

File tree

10 files changed

+130
-32
lines changed

10 files changed

+130
-32
lines changed

UPGRADE-7.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Translation
8484
Validator
8585
---------
8686

87+
* Deprecate implementing `__sleep/wakeup()` on `GenericMetadata` implementations; use `__(un)serialize()` instead
8788
* Deprecate passing a list of choices to the first argument of the `Choice` constraint. Use the `choices` option instead
8889
* Deprecate `getRequiredOptions()` and `getDefaultOption()` methods of the `All`, `AtLeastOneOf`, `CardScheme`, `Collection`,
8990
`CssColor`, `Expression`, `Regex`, `Sequentially`, `Type`, and `When` constraints

src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ public function __unserialize(array $data): void
136136
}
137137

138138
/**
139-
* @internal since Symfony 7.4, will be removed in 8.0
140-
*
141-
* @final since Symfony 7.4, will be removed in 8.0
139+
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
142140
*/
143141
public function __sleep(): array
144142
{
@@ -148,9 +146,7 @@ public function __sleep(): array
148146
}
149147

150148
/**
151-
* @internal since Symfony 7.4, will be removed in 8.0
152-
*
153-
* @final since Symfony 7.4, will be removed in 8.0
149+
* @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
154150
*/
155151
public function __wakeup(): void
156152
{

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,7 @@ public function __unserialize(array $data): void
846846
}
847847

848848
/**
849-
* @internal since Symfony 7.4, will be removed in 8.0
850-
*
851-
* @final since Symfony 7.4, will be removed in 8.0
849+
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
852850
*/
853851
public function __sleep(): array
854852
{
@@ -858,9 +856,7 @@ public function __sleep(): array
858856
}
859857

860858
/**
861-
* @internal since Symfony 7.4, will be removed in 8.0
862-
*
863-
* @final since Symfony 7.4, will be removed in 8.0
859+
* @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
864860
*/
865861
public function __wakeup(): void
866862
{

src/Symfony/Component/String/AbstractString.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,7 @@ public function __serialize(): array
729729
}
730730

731731
/**
732-
* @internal since Symfony 7.4, will be removed in 8.0
733-
*
734-
* @final since Symfony 7.4, will be removed in 8.0
732+
* @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
735733
*/
736734
public function __sleep(): array
737735
{

src/Symfony/Component/String/LazyString.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ public function __serialize(): array
126126
}
127127

128128
/**
129-
* @internal since Symfony 7.4, will be removed in 8.0
130-
*
131-
* @final since Symfony 7.4, will be removed in 8.0
129+
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
132130
*/
133131
public function __sleep(): array
134132
{
133+
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
134+
135135
$this->__toString();
136136

137137
return ['value'];

src/Symfony/Component/String/UnicodeString.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ public function __unserialize(array $data): void
406406
}
407407

408408
/**
409-
* @internal since Symfony 7.4, will be removed in 8.0
410-
*
411-
* @final since Symfony 7.4, will be removed in 8.0
409+
* @deprecated since Symfony 7.4, will be replaced by `__unserialize()` in 8.0
412410
*/
413411
public function __wakeup(): void
414412
{

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7+
* Deprecate implementing `__sleep/wakeup()` on `GenericMetadata` implementations; use `__(un)serialize()` instead
78
* Deprecate passing a list of choices to the first argument of the `Choice` constraint. Use the `choices` option instead
89
* Add the `min` and `max` parameter to the `Length` constraint violation
910
* Deprecate `getRequiredOptions()` and `getDefaultOption()` methods of the `All`, `AtLeastOneOf`, `CardScheme`, `Collection`,

src/Symfony/Component/Validator/Mapping/ClassMetadata.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,53 @@ public function __construct(string $class)
119119
}
120120
}
121121

122-
public function __sleep(): array
122+
public function __serialize(): array
123123
{
124-
$parentProperties = parent::__sleep();
124+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
125+
return [
126+
'constraints' => $this->constraints,
127+
'constraintsByGroup' => $this->constraintsByGroup,
128+
'traversalStrategy' => $this->traversalStrategy,
129+
'autoMappingStrategy' => $this->autoMappingStrategy,
130+
'getters' => $this->getters,
131+
'groupSequence' => $this->groupSequence,
132+
'groupSequenceProvider' => $this->groupSequenceProvider,
133+
'groupProvider' => $this->groupProvider,
134+
'members' => $this->members,
135+
'name' => $this->name,
136+
'properties' => $this->properties,
137+
'defaultGroup' => $this->defaultGroup,
138+
];
139+
}
140+
141+
trigger_deprecation('symfony/validator', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
142+
143+
$data = [];
144+
foreach ($this->__sleep() as $key) {
145+
try {
146+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
147+
$data[$key] = $r->getValue($this);
148+
}
149+
} catch (\ReflectionException) {
150+
$data[$key] = $this->$key;
151+
}
152+
}
125153

126-
// Don't store the cascading strategy. Classes never cascade.
127-
unset($parentProperties[array_search('cascadingStrategy', $parentProperties)]);
154+
return $data;
155+
}
156+
157+
/**
158+
* @deprecated since Symfony 7.4, will be removed in 8.0
159+
*/
160+
public function __sleep(): array
161+
{
162+
trigger_deprecation('symfony/validator', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
128163

129-
return array_merge($parentProperties, [
164+
return [
165+
'constraints',
166+
'constraintsByGroup',
167+
'traversalStrategy',
168+
'autoMappingStrategy',
130169
'getters',
131170
'groupSequence',
132171
'groupSequenceProvider',
@@ -135,7 +174,7 @@ public function __sleep(): array
135174
'name',
136175
'properties',
137176
'defaultGroup',
138-
]);
177+
];
139178
}
140179

141180
public function getClassName(): string

src/Symfony/Component/Validator/Mapping/GenericMetadata.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,41 @@ class GenericMetadata implements MetadataInterface
8383
*/
8484
public int $autoMappingStrategy = AutoMappingStrategy::NONE;
8585

86+
public function __serialize(): array
87+
{
88+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
89+
return [
90+
'constraints' => $this->constraints,
91+
'constraintsByGroup' => $this->constraintsByGroup,
92+
'cascadingStrategy' => $this->cascadingStrategy,
93+
'traversalStrategy' => $this->traversalStrategy,
94+
'autoMappingStrategy' => $this->autoMappingStrategy,
95+
];
96+
}
97+
98+
trigger_deprecation('symfony/validator', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
99+
100+
$data = [];
101+
foreach ($this->__sleep() as $key) {
102+
try {
103+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
104+
$data[$key] = $r->getValue($this);
105+
}
106+
} catch (\ReflectionException) {
107+
$data[$key] = $this->$key;
108+
}
109+
}
110+
111+
return $data;
112+
}
113+
86114
/**
87-
* Returns the names of the properties that should be serialized.
88-
*
89-
* @return string[]
115+
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
90116
*/
91117
public function __sleep(): array
92118
{
119+
trigger_deprecation('symfony/validator', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
120+
93121
return [
94122
'constraints',
95123
'constraintsByGroup',

src/Symfony/Component/Validator/Mapping/MemberMetadata.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,54 @@ public function addConstraint(Constraint $constraint): static
7676
return $this;
7777
}
7878

79+
public function __serialize(): array
80+
{
81+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
82+
return [
83+
'constraints' => $this->constraints,
84+
'constraintsByGroup' => $this->constraintsByGroup,
85+
'cascadingStrategy' => $this->cascadingStrategy,
86+
'traversalStrategy' => $this->traversalStrategy,
87+
'autoMappingStrategy' => $this->autoMappingStrategy,
88+
'class' => $this->class,
89+
'name' => $this->name,
90+
'property' => $this->property,
91+
];
92+
}
93+
94+
trigger_deprecation('symfony/validator', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
95+
96+
$data = [];
97+
foreach ($this->__sleep() as $key) {
98+
try {
99+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
100+
$data[$key] = $r->getValue($this);
101+
}
102+
} catch (\ReflectionException) {
103+
$data[$key] = $this->$key;
104+
}
105+
}
106+
107+
return $data;
108+
}
109+
110+
/**
111+
* @deprecated since Symfony 7.4, will be replaced by `__serialize()` in 8.0
112+
*/
79113
public function __sleep(): array
80114
{
81-
return array_merge(parent::__sleep(), [
115+
trigger_deprecation('symfony/validator', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
116+
117+
return [
118+
'constraints',
119+
'constraintsByGroup',
120+
'cascadingStrategy',
121+
'traversalStrategy',
122+
'autoMappingStrategy',
82123
'class',
83124
'name',
84125
'property',
85-
]);
126+
];
86127
}
87128

88129
/**

0 commit comments

Comments
 (0)