@@ -308,6 +308,48 @@ private function replace_key( $key ) {
308308 return is_string ( $ new_key ) ? $ new_key : $ key ;
309309 }
310310
311+ /**
312+ * Work out the replacement for each key of a container.
313+ *
314+ * Log entries produced while replacing the keys are kept apart from the
315+ * value log, so that they can be committed only once the container
316+ * accepts the renames (see commit_key_logs()).
317+ *
318+ * @param array<int|string> $keys The keys or property names.
319+ * @return array{0: array<int|string, int|string>, 1: array<int|string, string[]>} Map of original key to
320+ * destination key, and the pending log entries for each key.
321+ */
322+ private function plan_key_renames ( $ keys ) {
323+ $ renames = array ();
324+ $ key_logs = array ();
325+ 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 );
329+ }
330+ return array ( $ renames , $ key_logs );
331+ }
332+
333+ /**
334+ * Add the log entries of the keys that were actually renamed.
335+ *
336+ * @param array<int|string, int|string> $renames Map of original key to destination key.
337+ * @param array<int|string, string[]> $key_logs Pending log entries for each key.
338+ * @param int $position Position in the log at which to insert the entries.
339+ * @return void
340+ */
341+ private function commit_key_logs ( $ renames , $ key_logs , $ position ) {
342+ $ accepted = array ();
343+ foreach ( $ renames as $ key => $ new_key ) {
344+ if ( $ new_key !== $ key ) {
345+ $ accepted = array_merge ( $ accepted , $ key_logs [ $ key ] );
346+ }
347+ }
348+ if ( $ accepted ) {
349+ array_splice ( $ this ->log_data , $ position , 0 , $ accepted );
350+ }
351+ }
352+
311353 /**
312354 * Recurse into an array, replacing string keys as well as values.
313355 *
@@ -322,20 +364,19 @@ private function replace_key( $key ) {
322364 * @return array The processed array.
323365 */
324366 private function replace_array_keys ( $ data , $ recursion_level , $ visited_data ) {
325- $ renames = array ();
326- foreach ( array_keys ( $ data ) as $ key ) {
327- $ renames [ $ key ] = $ this ->replace_key ( $ key );
328- }
367+ list ( $ renames , $ key_logs ) = $ this ->plan_key_renames ( array_keys ( $ data ) );
329368 if ( $ this ->has_destination_collision ( $ renames , false ) ) {
330369 \WP_CLI ::warning ( 'Skipping key replacement for an array: two keys would map onto the same key and a value would be lost. ' );
331370 $ renames = array_combine ( array_keys ( $ renames ), array_keys ( $ renames ) );
332371 }
333- $ replaced = array ();
372+ $ log_position = count ( $ this ->log_data );
373+ $ replaced = array ();
334374 foreach ( $ data as $ key => &$ value ) {
335375 $ value = $ this ->run_recursively ( $ value , false , $ recursion_level + 1 , $ visited_data );
336376 $ replaced [ $ renames [ $ key ] ] =& $ value ;
337377 }
338378 unset( $ value );
379+ $ this ->commit_key_logs ( $ renames , $ key_logs , $ log_position );
339380 return $ replaced ;
340381 }
341382
@@ -348,6 +389,12 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
348389 * the property names are left unchanged (values are still replaced) and a
349390 * warning is emitted instead of silently discarding a value.
350391 *
392+ * Readonly properties (PHP 8.1+) are never renamed, and no property is
393+ * renamed onto a readonly property. Properties that share a PHP reference
394+ * keep sharing it after a rename. If modifying the object fails part way,
395+ * it is restored to its original property names and values before the
396+ * error is passed on.
397+ *
351398 * ArrayAccess implementations may normalize offsets on write. Offsets are
352399 * only renamed for implementations whose offset semantics are known
353400 * (ArrayObject, ArrayIterator and the Requests case-insensitive
@@ -361,10 +408,11 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
361408 * @return object The processed object.
362409 */
363410 private function replace_object_property_names ( $ data , $ is_array_access , $ recursion_level , $ visited_data ) {
364- $ renames = array ();
411+ $ keys = array ();
365412 foreach ( $ data as $ key => $ value ) {
366- $ renames [ $ key ] = $ this -> replace_key ( $ key ) ;
413+ $ keys [ ] = $ key ;
367414 }
415+ list ( $ renames , $ key_logs ) = $ this ->plan_key_renames ( $ keys );
368416
369417 $ case_insensitive = false ;
370418 $ keep_offsets = false ;
@@ -376,6 +424,20 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
376424 }
377425 }
378426
427+ $ readonly = $ is_array_access ? array () : $ this ->get_readonly_property_names ( $ data );
428+ foreach ( $ renames as $ key => $ new_key ) {
429+ if ( $ new_key !== $ key && ( isset ( $ readonly [ $ key ] ) || isset ( $ readonly [ $ new_key ] ) ) ) {
430+ \WP_CLI ::warning (
431+ sprintf (
432+ 'Skipping key replacement for property "%s" of an object of class "%s": a readonly property cannot be renamed or overwritten. ' ,
433+ $ key ,
434+ get_class ( $ data )
435+ )
436+ );
437+ $ renames [ $ key ] = $ key ;
438+ }
439+ }
440+
379441 $ renamed = false ;
380442 foreach ( $ renames as $ key => $ new_key ) {
381443 if ( $ new_key !== $ key ) {
@@ -397,20 +459,108 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
397459 $ renames = array_combine ( array_keys ( $ renames ), array_keys ( $ renames ) );
398460 }
399461
400- // Stage all processed values before touching the object.
401- $ values = array ();
462+ // Take a reference to each writable property, so that renaming a
463+ // property keeps any reference it shares with other properties.
464+ $ refs = array ();
465+ $ originals = array ();
402466 foreach ( $ renames as $ key => $ new_key ) {
403- $ values [ $ key ] = $ this ->run_recursively ( $ this ->read_property ( $ data , $ is_array_access , $ key ), false , $ recursion_level + 1 , $ visited_data );
467+ if ( $ is_array_access || isset ( $ readonly [ $ key ] ) ) {
468+ $ originals [ $ key ] = $ this ->read_property ( $ data , $ is_array_access , $ key );
469+ } else {
470+ $ refs [ $ key ] = &$ data ->$ key ;
471+ $ originals [ $ key ] = $ refs [ $ key ];
472+ }
404473 }
474+
475+ // Stage all processed values before touching the object.
476+ $ log_position = count ( $ this ->log_data );
477+ $ values = array ();
405478 foreach ( $ renames as $ key => $ new_key ) {
406- if ( $ new_key !== $ key ) {
407- $ this ->remove_property ( $ data , $ is_array_access , $ key );
479+ $ values [ $ key ] = $ this ->run_recursively ( $ originals [ $ key ], false , $ recursion_level + 1 , $ visited_data );
480+ }
481+
482+ $ removed = array ();
483+ $ written = array ();
484+ try {
485+ foreach ( $ renames as $ key => $ new_key ) {
486+ if ( $ new_key !== $ key ) {
487+ $ this ->remove_property ( $ data , $ is_array_access , $ key );
488+ $ removed [ $ key ] = true ;
489+ }
490+ }
491+ foreach ( $ renames as $ key => $ new_key ) {
492+ if ( isset ( $ readonly [ $ key ] ) ) {
493+ if ( $ values [ $ key ] !== $ originals [ $ key ] ) {
494+ throw new \Error ( sprintf ( 'Cannot modify readonly property %s::$%s ' , get_class ( $ data ), $ key ) ); // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
495+ }
496+ continue ;
497+ }
498+ if ( $ is_array_access ) {
499+ $ this ->write_property ( $ data , true , $ new_key , $ values [ $ key ] );
500+ if ( $ new_key !== $ key ) {
501+ $ written [] = $ new_key ;
502+ }
503+ continue ;
504+ }
505+ if ( $ new_key !== $ key ) {
506+ $ data ->$ new_key = &$ refs [ $ key ];
507+ $ written [] = $ new_key ;
508+ }
509+ $ refs [ $ key ] = $ values [ $ key ];
408510 }
511+ } catch ( \Error $ exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
512+ $ this ->restore_object ( $ data , $ is_array_access , $ renames , $ originals , $ refs , $ removed , $ written );
513+ throw $ exception ;
514+ }
515+
516+ $ this ->commit_key_logs ( $ renames , $ key_logs , $ log_position );
517+ return $ data ;
518+ }
519+
520+ /**
521+ * Restore an object to its original property names and values after a failed modification.
522+ *
523+ * @param object $data The object to restore.
524+ * @param bool $is_array_access Whether the object implements ArrayAccess.
525+ * @param array<int|string, int|string> $renames Map of original key to destination key.
526+ * @param array<int|string, mixed> $originals Original values, by original key.
527+ * @param array<int|string, mixed> $refs References to the original properties, by original key.
528+ * @param array<int|string, bool> $removed Original keys that were removed.
529+ * @param array<int|string> $written Destination keys that were written.
530+ * @return void
531+ */
532+ private function restore_object ( $ data , $ is_array_access , $ renames , $ originals , &$ refs , $ removed , $ written ) {
533+ foreach ( $ written as $ name ) {
534+ $ this ->remove_property ( $ data , $ is_array_access , $ name );
409535 }
410536 foreach ( $ renames as $ key => $ new_key ) {
411- $ this ->write_property ( $ data , $ is_array_access , $ new_key , $ values [ $ key ] );
537+ 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 ];
542+ }
543+ $ refs [ $ key ] = $ originals [ $ key ];
544+ }
412545 }
413- return $ data ;
546+ }
547+
548+ /**
549+ * Get the names of the readonly properties of an object.
550+ *
551+ * Readonly properties exist as of PHP 8.1; on older versions there are none.
552+ *
553+ * @param object $data The object to inspect.
554+ * @return array<string, bool> Readonly property names as keys.
555+ */
556+ private function get_readonly_property_names ( $ data ) {
557+ $ names = array ();
558+ foreach ( ( new \ReflectionObject ( $ data ) )->getProperties () as $ property ) {
559+ if ( method_exists ( $ property , 'isReadOnly ' ) && $ property ->isReadOnly () ) {
560+ $ names [ $ property ->getName () ] = true ;
561+ }
562+ }
563+ return $ names ;
414564 }
415565
416566 /**
0 commit comments