@@ -308,7 +308,7 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
308308 foreach ( array_keys ( $ data ) as $ key ) {
309309 $ renames [ $ key ] = is_string ( $ key ) ? $ this ->replace_string ( $ key ) : $ key ;
310310 }
311- if ( count ( array_unique ( $ renames , SORT_REGULAR ) ) !== count ( $ renames ) ) {
311+ if ( $ this -> has_destination_collision ( $ renames , false ) ) {
312312 \WP_CLI ::warning ( 'Skipping key replacement for an array: two keys would map onto the same key and a value would be lost. ' );
313313 $ renames = array_combine ( array_keys ( $ renames ), array_keys ( $ renames ) );
314314 }
@@ -324,9 +324,17 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
324324 /**
325325 * Recurse into an object, replacing string property names as well as values.
326326 *
327- * When two property names would map onto the same name, the property
328- * names are left unchanged (values are still replaced) and a warning is
329- * emitted instead of silently discarding a value.
327+ * All values are read and processed before the object is modified, so a
328+ * renamed property can never overwrite a source property that has not
329+ * been read yet. When two property names would map onto the same name,
330+ * the property names are left unchanged (values are still replaced) and a
331+ * warning is emitted instead of silently discarding a value.
332+ *
333+ * ArrayAccess implementations may normalize offsets on write. Offsets are
334+ * only renamed for implementations whose offset semantics are known
335+ * (ArrayObject, ArrayIterator and the Requests case-insensitive
336+ * dictionary); for any other ArrayAccess implementation the original
337+ * offsets are kept.
330338 *
331339 * @param object $data The object to process.
332340 * @param bool $is_array_access Whether the object implements ArrayAccess.
@@ -339,20 +347,79 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
339347 foreach ( $ data as $ key => $ value ) {
340348 $ renames [ $ key ] = is_string ( $ key ) ? $ this ->replace_string ( $ key ) : $ key ;
341349 }
342- if ( count ( array_unique ( $ renames , SORT_REGULAR ) ) !== count ( $ renames ) ) {
350+
351+ $ case_insensitive = false ;
352+ $ keep_offsets = false ;
353+ if ( $ is_array_access ) {
354+ if ( $ data instanceof \WpOrg \Requests \Utility \CaseInsensitiveDictionary || is_a ( $ data , 'Requests_Utility_CaseInsensitiveDictionary ' ) ) {
355+ $ case_insensitive = true ;
356+ } elseif ( ! $ data instanceof \ArrayObject && ! $ data instanceof \ArrayIterator ) {
357+ $ keep_offsets = true ;
358+ }
359+ }
360+
361+ $ renamed = false ;
362+ foreach ( $ renames as $ key => $ new_key ) {
363+ if ( $ new_key !== $ key ) {
364+ $ renamed = true ;
365+ break ;
366+ }
367+ }
368+
369+ if ( $ renamed && $ keep_offsets ) {
370+ \WP_CLI ::warning (
371+ sprintf (
372+ 'Skipping key replacement for an object of class "%s": its offset semantics are unknown. ' ,
373+ get_class ( $ data )
374+ )
375+ );
376+ $ renames = array_combine ( array_keys ( $ renames ), array_keys ( $ renames ) );
377+ } elseif ( $ renamed && $ this ->has_destination_collision ( $ renames , $ case_insensitive ) ) {
343378 \WP_CLI ::warning ( 'Skipping key replacement for an object: two property names would map onto the same name and a value would be lost. ' );
344379 $ renames = array_combine ( array_keys ( $ renames ), array_keys ( $ renames ) );
345380 }
381+
382+ // Stage all processed values before touching the object.
383+ $ values = array ();
384+ foreach ( $ renames as $ key => $ new_key ) {
385+ $ values [ $ key ] = $ this ->run_recursively ( $ this ->read_property ( $ data , $ is_array_access , $ key ), false , $ recursion_level + 1 , $ visited_data );
386+ }
346387 foreach ( $ renames as $ key => $ new_key ) {
347- $ value = $ this ->run_recursively ( $ this ->read_property ( $ data , $ is_array_access , $ key ), false , $ recursion_level + 1 , $ visited_data );
348388 if ( $ new_key !== $ key ) {
349389 $ this ->remove_property ( $ data , $ is_array_access , $ key );
350390 }
351- $ this ->write_property ( $ data , $ is_array_access , $ new_key , $ value );
391+ }
392+ foreach ( $ renames as $ key => $ new_key ) {
393+ $ this ->write_property ( $ data , $ is_array_access , $ new_key , $ values [ $ key ] );
352394 }
353395 return $ data ;
354396 }
355397
398+ /**
399+ * Check whether two renamed keys would end up as the same stored key.
400+ *
401+ * Destination keys are compared by how PHP actually stores them as array
402+ * keys (so `8` and `'08'` stay distinct, while `'8'` and `8` collide), and
403+ * optionally case-insensitively for containers that normalize offsets.
404+ *
405+ * @param array $renames Map of original key to destination key.
406+ * @param bool $case_insensitive Whether destination keys are compared case-insensitively.
407+ * @return bool Whether at least two destination keys collide.
408+ */
409+ private function has_destination_collision ( $ renames , $ case_insensitive ) {
410+ $ seen = array ();
411+ foreach ( $ renames as $ new_key ) {
412+ if ( $ case_insensitive && is_string ( $ new_key ) ) {
413+ $ new_key = strtolower ( $ new_key );
414+ }
415+ if ( isset ( $ seen [ $ new_key ] ) ) {
416+ return true ;
417+ }
418+ $ seen [ $ new_key ] = true ;
419+ }
420+ return false ;
421+ }
422+
356423 /**
357424 * Read one property or offset from an object or ArrayAccess instance.
358425 *
0 commit comments