|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\VarDumper\Cloner; |
13 | 13 |
|
14 | | -use Symfony\Component\VarDumper\Cloner\Internal\NoDefault; |
15 | | - |
16 | 14 | /** |
17 | 15 | * Represents the main properties of a PHP variable. |
18 | 16 | * |
@@ -42,34 +40,37 @@ class Stub |
42 | 40 | public $position = 0; |
43 | 41 | public $attr = []; |
44 | 42 |
|
45 | | - private static array $defaultProperties = []; |
46 | | - |
47 | 43 | /** |
48 | 44 | * @internal |
49 | 45 | */ |
50 | | - public function __sleep(): array |
51 | | - { |
52 | | - $properties = []; |
| 46 | + protected static array $propertyDefaults = []; |
53 | 47 |
|
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(); |
57 | 51 |
|
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; |
61 | 58 | } |
62 | | - |
63 | | - self::$defaultProperties[$c][$p->name] = $p->hasDefaultValue() ? $p->getDefaultValue() : ($p->hasType() ? NoDefault::NoDefault : null); |
64 | 59 | } |
| 60 | + |
| 61 | + return $data; |
65 | 62 | } |
66 | 63 |
|
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 | + } |
70 | 71 | } |
71 | | - } |
72 | 72 |
|
73 | | - return $properties; |
| 73 | + return $data; |
| 74 | + }, $this, $this::class)(); |
74 | 75 | } |
75 | 76 | } |
0 commit comments