File tree Expand file tree Collapse file tree 3 files changed +71
-4
lines changed
src/Symfony/Component/VarDumper Expand file tree Collapse file tree 3 files changed +71
-4
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Symfony \Component \VarDumper \Cloner \Internal ;
4
+
5
+ /**
6
+ * Flags a typed property that has no default value.
7
+ *
8
+ * This dummy object is used to distinguish a property with a default value of null
9
+ * from a property that is uninitialized by default.
10
+ *
11
+ * @internal
12
+ */
13
+ enum NoDefault
14
+ {
15
+ case NoDefault;
16
+ }
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 ($ v === NoDefault::NoDefault || $ 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
+ namespace Symfony \Component \VarDumper \Tests \Cloner ;
4
+
5
+ use PHPUnit \Framework \TestCase ;
6
+ use Symfony \Component \VarDumper \Cloner \Stub ;
7
+
8
+ final class StubTest extends TestCase
9
+ {
10
+ public function testUnserializeNullValue ()
11
+ {
12
+ $ stub = new Stub ();
13
+ $ stub ->value = null ;
14
+
15
+ $ stub = unserialize (serialize ($ stub ));
16
+
17
+ self ::assertNull ($ stub ->value );
18
+ }
19
+
20
+ public function testUnserializeNullInTypedProperty ()
21
+ {
22
+ $ stub = new MyStub ();
23
+ $ stub ->myProp = null ;
24
+
25
+ $ stub = unserialize (serialize ($ stub ));
26
+
27
+ self ::assertNull ($ stub ->myProp );
28
+ }
29
+
30
+ public function testUninitializedStubPropertiesAreLeftUninitialized ()
31
+ {
32
+ $ stub = new MyStub ();
33
+
34
+ $ stub = unserialize (serialize ($ stub ));
35
+
36
+ $ r = new \ReflectionProperty (MyStub::class, 'myProp ' );
37
+ self ::assertFalse ($ r ->isInitialized ($ stub ));
38
+ }
39
+ }
40
+
41
+ final class MyStub extends Stub
42
+ {
43
+ public mixed $ myProp ;
44
+ }
You can’t perform that action at this time.
0 commit comments