@@ -34,9 +34,12 @@ public function serialize(mixed $data): string
3434 try {
3535 // get boxed structure
3636 $ data = $ this ->handle ($ data );
37- if ($ this ->hasClosures && $ this ->priority ->count ()) {
37+ if ($ this ->hasClosures && ( $ count = $ this ->priority ->count () )) {
3838 // we only need priority when we have closures
39- $ data = new PriorityWrapper (iterator_to_array ($ this ->priority ), $ data );
39+ // do not try to wrap a single object
40+ if ($ count !== 1 || !is_object ($ data ) || !$ this ->priority ->offsetExists ($ data )) {
41+ $ data = new PriorityWrapper (iterator_to_array ($ this ->priority ), $ data );
42+ }
4043 }
4144 return serialize ($ data );
4245 } finally {
@@ -136,26 +139,26 @@ private function handleObject(object $data): object
136139 return $ this ->handleClosure ($ data );
137140 }
138141
139- $ info = ReflectionClass::get (get_class ( $ data) );
140- if (!$ this ->shouldBox ($ info )) {
142+ $ reflector = ReflectionClass::get ($ data );
143+ if (!$ this ->shouldBox ($ reflector )) {
141144 // skip boxing
142145 return $ this ->objectMap [$ data ] = $ data ;
143146 }
144147
145- if ($ info ->isAnonymousLike ()) {
146- $ anonInfo = AnonymousClassParser:: parse ( $ info );
148+ if ($ reflector ->isAnonymousLike ()) {
149+ $ anonInfo = $ reflector -> info ( );
147150 $ box = new Box (Box::TYPE_ANONYMOUS_CLASS , [null , null ]);
148151 $ box ->data [0 ] = &$ this ->getCachedInfo ($ anonInfo );
149152 unset($ anonInfo );
150153 } else {
151- $ box = new Box (Box::TYPE_OBJECT , [$ info ->name , null ]);
154+ $ box = new Box (Box::TYPE_OBJECT , [$ reflector ->name , null ]);
152155 }
153156
154157 // Set mapping (before vars!)
155158 $ this ->objectMap [$ data ] = $ box ;
156159
157160 // Set vars
158- $ box ->data [1 ] = $ this ->getObjectVars ($ data , $ info );
161+ $ box ->data [1 ] = $ this ->getObjectVars ($ data , $ reflector );
159162
160163 // Add to priority
161164 $ this ->priority ->offsetSet ($ box );
0 commit comments