Skip to content

Commit 68acca9

Browse files
[String] Deprecate implementing __sleep/wakeup() on string implementations
1 parent 00bb9f6 commit 68acca9

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

AbstractString.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,37 @@ public function wordwrap(int $width = 75, string $break = "\n", bool $cut = fals
706706
return $str;
707707
}
708708

709+
public function __serialize(): array
710+
{
711+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
712+
return ['string' => $this->string];
713+
}
714+
715+
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
716+
717+
$data = [];
718+
foreach ($this->__sleep() as $key) {
719+
try {
720+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
721+
$data[$key] = $r->getValue($this);
722+
}
723+
} catch (\ReflectionException) {
724+
$data[$key] = $this->$key;
725+
}
726+
}
727+
728+
return $data;
729+
}
730+
731+
/**
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
735+
*/
709736
public function __sleep(): array
710737
{
738+
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
739+
711740
return ['string'];
712741
}
713742

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Deprecate implementing `__sleep/wakeup()` on string implementations
8+
49
7.3
510
---
611

LazyString.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,35 @@ public function __toString(): string
101101
}
102102
}
103103

104+
public function __serialize(): array
105+
{
106+
if (self::class === (new \ReflectionMethod($this, '__sleep'))->class) {
107+
$this->__toString();
108+
109+
return ['value' => $this->value];
110+
}
111+
112+
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__sleep()" is deprecated, use "__serialize()" instead.', get_debug_type($this));
113+
114+
$data = [];
115+
foreach ($this->__sleep() as $key) {
116+
try {
117+
if (($r = new \ReflectionProperty($this, $key))->isInitialized($this)) {
118+
$data[$key] = $r->getValue($this);
119+
}
120+
} catch (\ReflectionException) {
121+
$data[$key] = $this->$key;
122+
}
123+
}
124+
125+
return $data;
126+
}
127+
128+
/**
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
132+
*/
104133
public function __sleep(): array
105134
{
106135
$this->__toString();

UnicodeString.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,54 @@ public function startsWith(string|iterable|AbstractString $prefix): bool
366366
return $prefix === $grapheme;
367367
}
368368

369+
public function __unserialize(array $data): void
370+
{
371+
if ($wakeup = self::class !== (new \ReflectionMethod($this, '__wakeup'))->class) {
372+
trigger_deprecation('symfony/string', '7.4', 'Implementing "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
373+
}
374+
375+
try {
376+
if (\in_array(array_keys($data), [['string'], ["\0*\0string"]], true)) {
377+
$this->string = $data['string'] ?? $data["\0*\0string"];
378+
379+
if ($wakeup) {
380+
$this->__wakeup();
381+
}
382+
383+
return;
384+
}
385+
386+
trigger_deprecation('symfony/string', '7.4', 'Passing more than just key "string" to "%s::__unserialize()" is deprecated, populate properties in "%s::__unserialize()" instead.', self::class, get_debug_type($this));
387+
388+
\Closure::bind(function ($data) use ($wakeup) {
389+
foreach ($data as $key => $value) {
390+
$this->{("\0" === $key[0] ?? '') ? substr($key, 1 + strrpos($key, "\0")) : $key} = $value;
391+
}
392+
393+
if ($wakeup) {
394+
$this->__wakeup();
395+
}
396+
}, $this, static::class)($data);
397+
} finally {
398+
if (!$wakeup) {
399+
if (!\is_string($this->string)) {
400+
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
401+
}
402+
403+
normalizer_is_normalized($this->string) ?: $this->string = normalizer_normalize($this->string);
404+
}
405+
}
406+
}
407+
408+
/**
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
412+
*/
369413
public function __wakeup(): void
370414
{
415+
trigger_deprecation('symfony/string', '7.4', 'Calling "%s::__wakeup()" is deprecated, use "__unserialize()" instead.', get_debug_type($this));
416+
371417
if (!\is_string($this->string)) {
372418
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
373419
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=8.2",
20+
"symfony/deprecation-contracts": "^2.5|^3.0",
2021
"symfony/polyfill-ctype": "~1.8",
2122
"symfony/polyfill-intl-grapheme": "~1.33",
2223
"symfony/polyfill-intl-normalizer": "~1.0",

0 commit comments

Comments
 (0)