File tree 3 files changed +89
-4
lines changed
3 files changed +89
-4
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <[email protected] >
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \VarDumper \Cloner \Internal ;
13
+
14
+ /**
15
+ * Flags a typed property that has no default value.
16
+ *
17
+ * This dummy object is used to distinguish a property with a default value of null
18
+ * from a property that is uninitialized by default.
19
+ *
20
+ * @internal
21
+ */
22
+ enum NoDefault
23
+ {
24
+ case NoDefault;
25
+ }
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \VarDumper \Cloner ;
13
13
14
+ use Symfony \Component \VarDumper \Cloner \Internal \NoDefault ;
15
+
14
16
/**
15
17
* Represents the main properties of a PHP variable.
16
18
*
@@ -50,15 +52,20 @@ public function __sleep(): array
50
52
$ properties = [];
51
53
52
54
if (!isset (self ::$ defaultProperties [$ c = static ::class])) {
53
- self ::$ defaultProperties [$ c ] = get_class_vars ($ c );
55
+ $ reflection = new \ReflectionClass ($ c );
56
+ self ::$ defaultProperties [$ c ] = [];
57
+
58
+ foreach ($ reflection ->getProperties () as $ p ) {
59
+ if ($ p ->isStatic ()) {
60
+ continue ;
61
+ }
54
62
55
- foreach ((new \ReflectionClass ($ c ))->getStaticProperties () as $ k => $ v ) {
56
- unset(self ::$ defaultProperties [$ c ][$ k ]);
63
+ self ::$ defaultProperties [$ c ][$ p ->name ] = $ p ->hasDefaultValue () ? $ p ->getDefaultValue () : ($ p ->hasType () ? NoDefault::NoDefault : null );
57
64
}
58
65
}
59
66
60
67
foreach (self ::$ defaultProperties [$ c ] as $ k => $ v ) {
61
- if ($ this ->$ k !== $ v ) {
68
+ if (NoDefault::NoDefault === $ v || $ this ->$ k !== $ v ) {
62
69
$ properties [] = $ k ;
63
70
}
64
71
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <[email protected] >
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+ namespace Symfony \Component \VarDumper \Tests \Cloner ;
13
+
14
+ use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \VarDumper \Cloner \Stub ;
16
+
17
+ final class StubTest extends TestCase
18
+ {
19
+ public function testUnserializeNullValue ()
20
+ {
21
+ $ stub = new Stub ();
22
+ $ stub ->value = null ;
23
+
24
+ $ stub = unserialize (serialize ($ stub ));
25
+
26
+ self ::assertNull ($ stub ->value );
27
+ }
28
+
29
+ public function testUnserializeNullInTypedProperty ()
30
+ {
31
+ $ stub = new MyStub ();
32
+ $ stub ->myProp = null ;
33
+
34
+ $ stub = unserialize (serialize ($ stub ));
35
+
36
+ self ::assertNull ($ stub ->myProp );
37
+ }
38
+
39
+ public function testUninitializedStubPropertiesAreLeftUninitialized ()
40
+ {
41
+ $ stub = new MyStub ();
42
+
43
+ $ stub = unserialize (serialize ($ stub ));
44
+
45
+ $ r = new \ReflectionProperty (MyStub::class, 'myProp ' );
46
+ self ::assertFalse ($ r ->isInitialized ($ stub ));
47
+ }
48
+ }
49
+
50
+ final class MyStub extends Stub
51
+ {
52
+ public mixed $ myProp ;
53
+ }
You can’t perform that action at this time.
0 commit comments