Skip to content

Commit b2b1395

Browse files
Replace __sleep/wakeup() by __(un)serialize() when BC isn't a concern
1 parent 60fd3f8 commit b2b1395

File tree

2 files changed

+21
-45
lines changed

2 files changed

+21
-45
lines changed

Cloner/Internal/NoDefault.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

Cloner/Stub.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\VarDumper\Cloner;
1313

14-
use Symfony\Component\VarDumper\Cloner\Internal\NoDefault;
15-
1614
/**
1715
* Represents the main properties of a PHP variable.
1816
*
@@ -42,34 +40,37 @@ class Stub
4240
public int $position = 0;
4341
public array $attr = [];
4442

45-
private static array $defaultProperties = [];
46-
4743
/**
4844
* @internal
4945
*/
50-
public function __sleep(): array
51-
{
52-
$properties = [];
46+
protected static array $propertyDefaults = [];
5347

54-
if (!isset(self::$defaultProperties[$c = static::class])) {
55-
$reflection = new \ReflectionClass($c);
56-
self::$defaultProperties[$c] = [];
48+
public function __serialize(): array
49+
{
50+
static $noDefault = new \stdClass();
5751

58-
foreach ($reflection->getProperties() as $p) {
59-
if ($p->isStatic()) {
60-
continue;
52+
if (self::class === static::class) {
53+
$data = [];
54+
foreach ($this as $k => $v) {
55+
$default = self::$propertyDefaults[$this::class][$k] ??= ($p = new \ReflectionProperty($this, $k))->hasDefaultValue() ? $p->getDefaultValue() : ($p->hasType() ? $noDefault : null);
56+
if ($noDefault === $default || $default !== $v) {
57+
$data[$k] = $v;
6158
}
62-
63-
self::$defaultProperties[$c][$p->name] = $p->hasDefaultValue() ? $p->getDefaultValue() : ($p->hasType() ? NoDefault::NoDefault : null);
6459
}
60+
61+
return $data;
6562
}
6663

67-
foreach (self::$defaultProperties[$c] as $k => $v) {
68-
if (NoDefault::NoDefault === $v || $this->$k !== $v) {
69-
$properties[] = $k;
64+
return \Closure::bind(function () use ($noDefault) {
65+
$data = [];
66+
foreach ($this as $k => $v) {
67+
$default = self::$propertyDefaults[$this::class][$k] ??= ($p = new \ReflectionProperty($this, $k))->hasDefaultValue() ? $p->getDefaultValue() : ($p->hasType() ? $noDefault : null);
68+
if ($noDefault === $default || $default !== $v) {
69+
$data[$k] = $v;
70+
}
7071
}
71-
}
7272

73-
return $properties;
73+
return $data;
74+
}, $this, $this::class)();
7475
}
7576
}

0 commit comments

Comments
 (0)