@@ -76,33 +76,23 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
76
76
$ class = $ value ::class;
77
77
$ reflector = Registry::$ reflectors [$ class ] ??= Registry::getClassReflector ($ class );
78
78
$ properties = [];
79
+ $ sleep = null ;
80
+ $ proto = Registry::$ prototypes [$ class ];
79
81
80
82
if ($ reflector ->hasMethod ('__serialize ' )) {
81
83
if (!$ reflector ->getMethod ('__serialize ' )->isPublic ()) {
82
84
throw new \Error (\sprintf ('Call to %s method "%s::__serialize()". ' , $ reflector ->getMethod ('__serialize ' )->isProtected () ? 'protected ' : 'private ' , $ class ));
83
85
}
84
86
85
- if (!\is_array ($ serializeProperties = $ value ->__serialize ())) {
87
+ if (!\is_array ($ arrayValue = $ value ->__serialize ())) {
86
88
throw new \TypeError ($ class .'::__serialize() must return an array ' );
87
89
}
88
90
89
91
if ($ reflector ->hasMethod ('__unserialize ' )) {
90
- $ properties = $ serializeProperties ;
91
- } else {
92
- foreach ($ serializeProperties as $ n => $ v ) {
93
- $ p = $ reflector ->hasProperty ($ n ) ? $ reflector ->getProperty ($ n ) : null ;
94
- $ c = $ p && (\PHP_VERSION_ID >= 80400 ? $ p ->isProtectedSet () || $ p ->isPrivateSet () : $ p ->isReadOnly ()) ? $ p ->class : 'stdClass ' ;
95
- $ properties [$ c ][$ n ] = $ v ;
96
- }
92
+ $ properties = $ arrayValue ;
93
+ goto prepare_value;
97
94
}
98
-
99
- goto prepare_value;
100
- }
101
-
102
- $ sleep = null ;
103
- $ proto = Registry::$ prototypes [$ class ];
104
-
105
- if (($ value instanceof \ArrayIterator || $ value instanceof \ArrayObject) && null !== $ proto ) {
95
+ } elseif (($ value instanceof \ArrayIterator || $ value instanceof \ArrayObject) && null !== $ proto ) {
106
96
// ArrayIterator and ArrayObject need special care because their "flags"
107
97
// option changes the behavior of the (array) casting operator.
108
98
[$ arrayValue , $ properties ] = self ::getArrayObjectProperties ($ value , $ proto );
@@ -118,10 +108,7 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
118
108
}
119
109
$ properties = ['SplObjectStorage ' => ["\0" => $ properties ]];
120
110
$ arrayValue = (array ) $ value ;
121
- } elseif ($ value instanceof \Serializable
122
- || $ value instanceof \__PHP_Incomplete_Class
123
- || \PHP_VERSION_ID < 80200 && $ value instanceof \DatePeriod
124
- ) {
111
+ } elseif ($ value instanceof \Serializable || $ value instanceof \__PHP_Incomplete_Class || \PHP_VERSION_ID < 80200 && $ value instanceof \DatePeriod) {
125
112
++$ objectsCount ;
126
113
$ objectsPool [$ value ] = [$ id = \count ($ objectsPool ), serialize ($ value ), [], 0 ];
127
114
$ value = new Reference ($ id );
@@ -145,16 +132,15 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
145
132
$ i = 0 ;
146
133
$ n = (string ) $ name ;
147
134
if ('' === $ n || "\0" !== $ n [0 ]) {
148
- $ p = $ reflector ->hasProperty ($ n ) ? $ reflector ->getProperty ($ n ) : null ;
149
- $ c = $ p && (\PHP_VERSION_ID >= 80400 ? $ p ->isProtectedSet () || $ p ->isPrivateSet () : $ p ->isReadOnly ()) ? $ p ->class : 'stdClass ' ;
135
+ $ parent = $ reflector ;
136
+ do {
137
+ $ p = $ parent ->hasProperty ($ n ) ? $ parent ->getProperty ($ n ) : null ;
138
+ } while (!$ p && $ parent = $ parent ->getParentClass ());
139
+
140
+ $ c = $ p && (!$ p ->isPublic () || (\PHP_VERSION_ID >= 80400 ? $ p ->isProtectedSet () || $ p ->isPrivateSet () : $ p ->isReadOnly ())) ? $ p ->class : 'stdClass ' ;
150
141
} elseif ('* ' === $ n [1 ]) {
151
142
$ n = substr ($ n , 3 );
152
143
$ c = $ reflector ->getProperty ($ n )->class ;
153
- if ('Error ' === $ c ) {
154
- $ c = 'TypeError ' ;
155
- } elseif ('Exception ' === $ c ) {
156
- $ c = 'ErrorException ' ;
157
- }
158
144
} else {
159
145
$ i = strpos ($ n , "\0" , 2 );
160
146
$ c = substr ($ n , 1 , $ i - 1 );
@@ -167,8 +153,14 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
167
153
}
168
154
unset($ sleep [$ name ], $ sleep [$ n ]);
169
155
}
170
- if (! \array_key_exists ( $ name , $ proto ) || $ proto [ $ name ] !== $ v || "\x00Error \x00trace " === $ name || "\x00Exception \x00trace " === $ name ) {
156
+ if ("\x00Error \x00trace " === $ name || "\x00Exception \x00trace " === $ name ) {
171
157
$ properties [$ c ][$ n ] = $ v ;
158
+ } elseif (!\array_key_exists ($ name , $ proto ) || $ proto [$ name ] !== $ v ) {
159
+ $ properties [match ($ c ) {
160
+ 'Error ' => 'TypeError ' ,
161
+ 'Exception ' => 'ErrorException ' ,
162
+ default => $ c ,
163
+ }][$ n ] = $ v ;
172
164
}
173
165
}
174
166
if ($ sleep ) {
0 commit comments