@@ -323,9 +323,16 @@ private function plan_key_renames( $keys ) {
323323 $ renames = array ();
324324 $ key_logs = array ();
325325 foreach ( $ keys as $ key ) {
326- $ mark = count ( $ this ->log_data );
327- $ renames [ $ key ] = $ this ->replace_key ( $ key );
328- $ key_logs [ $ key ] = array_splice ( $ this ->log_data , $ mark );
326+ $ mark = count ( $ this ->log_data );
327+ $ renames [ $ key ] = $ this ->replace_key ( $ key );
328+ // Move only the entries appended for this key out of the value log,
329+ // instead of rebuilding the whole log for every key.
330+ $ pending = array ();
331+ $ appended = count ( $ this ->log_data ) - $ mark ;
332+ for ( $ i = 0 ; $ i < $ appended ; $ i ++ ) {
333+ $ pending [] = array_pop ( $ this ->log_data );
334+ }
335+ $ key_logs [ $ key ] = array_reverse ( $ pending );
329336 }
330337 return array ( $ renames , $ key_logs );
331338 }
@@ -342,7 +349,9 @@ private function commit_key_logs( $renames, $key_logs, $position ) {
342349 $ accepted = array ();
343350 foreach ( $ renames as $ key => $ new_key ) {
344351 if ( $ new_key !== $ key ) {
345- $ accepted = array_merge ( $ accepted , $ key_logs [ $ key ] );
352+ foreach ( $ key_logs [ $ key ] as $ entry ) {
353+ $ accepted [] = $ entry ;
354+ }
346355 }
347356 }
348357 if ( $ accepted ) {
@@ -414,6 +423,17 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
414423 }
415424 list ( $ renames , $ key_logs ) = $ this ->plan_key_renames ( $ keys );
416425
426+ if ( ! $ is_array_access ) {
427+ // Property names are always strings, but as keys of $renames numeric
428+ // names such as "8" become integers. Compare them as strings, so that
429+ // an unchanged numeric property name is not treated as renamed.
430+ foreach ( $ renames as $ key => $ new_key ) {
431+ if ( (string ) $ new_key === (string ) $ key ) {
432+ $ renames [ $ key ] = $ key ;
433+ }
434+ }
435+ }
436+
417437 $ case_insensitive = false ;
418438 $ keep_offsets = false ;
419439 if ( $ is_array_access ) {
@@ -459,16 +479,18 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
459479 $ renames = array_combine ( array_keys ( $ renames ), array_keys ( $ renames ) );
460480 }
461481
462- // Take a reference to each writable property, so that renaming a
463- // property keeps any reference it shares with other properties.
482+ // Take a reference to each property that is renamed, so that it keeps
483+ // any reference it shares with other properties. Properties that keep
484+ // their name are read and assigned normally, so that, for example,
485+ // PHP 8.4 `set` hooks still apply to them.
464486 $ refs = array ();
465487 $ originals = array ();
466488 foreach ( $ renames as $ key => $ new_key ) {
467- if ( $ is_array_access || isset ( $ readonly [ $ key ] ) ) {
468- $ originals [ $ key ] = $ this ->read_property ( $ data , $ is_array_access , $ key );
469- } else {
489+ if ( ! $ is_array_access && $ new_key !== $ key ) {
470490 $ refs [ $ key ] = &$ data ->$ key ;
471491 $ originals [ $ key ] = $ refs [ $ key ];
492+ } else {
493+ $ originals [ $ key ] = $ this ->read_property ( $ data , $ is_array_access , $ key );
472494 }
473495 }
474496
@@ -479,8 +501,9 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
479501 $ values [ $ key ] = $ this ->run_recursively ( $ originals [ $ key ], false , $ recursion_level + 1 , $ visited_data );
480502 }
481503
482- $ removed = array ();
483- $ written = array ();
504+ $ removed = array ();
505+ $ written = array ();
506+ $ assigned = array ();
484507 try {
485508 foreach ( $ renames as $ key => $ new_key ) {
486509 if ( $ new_key !== $ key ) {
@@ -505,11 +528,19 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
505528 if ( $ new_key !== $ key ) {
506529 $ data ->$ new_key = &$ refs [ $ key ];
507530 $ written [] = $ new_key ;
531+ $ refs [ $ key ] = $ values [ $ key ];
532+ } else {
533+ $ data ->$ key = $ values [ $ key ];
534+ $ assigned [ $ key ] = true ;
508535 }
509- $ refs [ $ key ] = $ values [ $ key ];
510536 }
511537 } catch ( \Error $ exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
512- $ this ->restore_object ( $ data , $ is_array_access , $ renames , $ originals , $ refs , $ removed , $ written );
538+ $ this ->restore_object ( $ data , $ is_array_access , $ renames , $ originals , $ refs , $ removed , $ written , $ assigned );
539+ throw $ exception ;
540+ } catch ( Exception $ exception ) {
541+ // ArrayAccess implementations may throw any exception, for example
542+ // from offsetUnset() after an earlier offset was already removed.
543+ $ this ->restore_object ( $ data , $ is_array_access , $ renames , $ originals , $ refs , $ removed , $ written , $ assigned );
513544 throw $ exception ;
514545 }
515546
@@ -520,27 +551,50 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
520551 /**
521552 * Restore an object to its original property names and values after a failed modification.
522553 *
554+ * Removed properties and offsets are added back at the end, so the
555+ * properties and offsets that followed the first removed one are moved
556+ * to the end as well, to restore the original order. Declared properties
557+ * keep their position anyway and are not moved.
558+ *
523559 * @param object $data The object to restore.
524560 * @param bool $is_array_access Whether the object implements ArrayAccess.
525561 * @param array<int|string, int|string> $renames Map of original key to destination key.
526562 * @param array<int|string, mixed> $originals Original values, by original key.
527563 * @param array<int|string, mixed> $refs References to the original properties, by original key.
528564 * @param array<int|string, bool> $removed Original keys that were removed.
529565 * @param array<int|string> $written Destination keys that were written.
566+ * @param array<int|string, bool> $assigned Original keys that kept their name and were assigned.
530567 * @return void
531568 */
532- private function restore_object ( $ data , $ is_array_access , $ renames , $ originals , &$ refs , $ removed , $ written ) {
569+ private function restore_object ( $ data , $ is_array_access , $ renames , $ originals , &$ refs , $ removed , $ written, $ assigned ) {
533570 foreach ( $ written as $ name ) {
534571 $ this ->remove_property ( $ data , $ is_array_access , $ name );
535572 }
573+ $ class = get_class ( $ data );
574+ $ move = false ;
536575 foreach ( $ renames as $ key => $ new_key ) {
576+ if ( isset ( $ removed [ $ key ] ) ) {
577+ $ move = true ;
578+ }
537579 if ( $ is_array_access ) {
538- $ this ->write_property ( $ data , true , $ key , $ originals [ $ key ] );
539- } elseif ( array_key_exists ( $ key , $ refs ) ) {
540- if ( isset ( $ removed [ $ key ] ) ) {
541- $ data ->$ key = &$ refs [ $ key ];
580+ if ( $ move && ! isset ( $ removed [ $ key ] ) ) {
581+ $ this ->remove_property ( $ data , true , $ key );
542582 }
583+ $ this ->write_property ( $ data , true , $ key , $ originals [ $ key ] );
584+ } elseif ( isset ( $ removed [ $ key ] ) ) {
585+ $ data ->$ key = &$ refs [ $ key ];
543586 $ refs [ $ key ] = $ originals [ $ key ];
587+ } elseif ( $ move && ! property_exists ( $ class , (string ) $ key ) ) {
588+ // Move a dynamic property to the end, keeping any reference it shares.
589+ $ ref = &$ data ->$ key ;
590+ unset( $ data ->$ key );
591+ $ data ->$ key = &$ ref ;
592+ if ( isset ( $ assigned [ $ key ] ) ) {
593+ $ ref = $ originals [ $ key ];
594+ }
595+ unset( $ ref );
596+ } elseif ( isset ( $ assigned [ $ key ] ) ) {
597+ $ data ->$ key = $ originals [ $ key ];
544598 }
545599 }
546600 }
0 commit comments