@@ -187,17 +187,14 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
187187 if ( false !== $ unserialized ) {
188188 $ data = $ this ->run_recursively ( $ unserialized , true , $ recursion_level + 1 );
189189 } elseif ( is_array ( $ data ) ) {
190- $ keys = array_keys ( $ data );
191- $ replaced = array ();
192- foreach ( $ keys as $ key ) {
193- $ value = $ this ->run_recursively ( $ data [ $ key ], false , $ recursion_level + 1 , $ visited_data );
194- $ new_key = $ key ;
195- if ( $ this ->recurse_keys && is_string ( $ key ) ) {
196- $ new_key = $ this ->replace_string ( $ key );
190+ if ( $ this ->recurse_keys ) {
191+ $ data = $ this ->replace_array_keys ( $ data , $ recursion_level , $ visited_data );
192+ } else {
193+ $ keys = array_keys ( $ data );
194+ foreach ( $ keys as $ key ) {
195+ $ data [ $ key ] = $ this ->run_recursively ( $ data [ $ key ], false , $ recursion_level + 1 , $ visited_data );
197196 }
198- $ replaced [ $ new_key ] = $ value ;
199197 }
200- $ data = $ replaced ;
201198 } elseif ( $ this ->recurse_objects && ( is_object ( $ data ) || $ data instanceof \__PHP_Incomplete_Class ) ) {
202199 if ( $ data instanceof \__PHP_Incomplete_Class ) {
203200 $ array = new ArrayObject ( $ data );
@@ -210,26 +207,16 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
210207 } else {
211208 try {
212209 $ is_array_access = $ data instanceof \ArrayAccess;
213- $ properties = array ();
214- foreach ( $ data as $ key => $ value ) {
215- $ properties [ $ key ] = $ value ;
216- }
217- foreach ( $ properties as $ key => $ value ) {
218- $ value = $ this ->run_recursively ( $ value , false , $ recursion_level + 1 , $ visited_data );
219- $ new_key = $ key ;
220- if ( $ this ->recurse_keys && is_string ( $ key ) ) {
221- $ new_key = $ this ->replace_string ( $ key );
222- }
223- if ( $ is_array_access ) {
224- if ( $ new_key !== $ key ) {
225- unset( $ data [ $ key ] );
210+ if ( $ this ->recurse_keys ) {
211+ $ data = $ this ->replace_object_property_names ( $ data , $ is_array_access , $ recursion_level , $ visited_data );
212+ } else {
213+ foreach ( $ data as $ key => $ value ) {
214+ $ value = $ this ->run_recursively ( $ value , false , $ recursion_level + 1 , $ visited_data );
215+ if ( $ is_array_access ) {
216+ $ data [ $ key ] = $ value ;
217+ } else {
218+ $ data ->$ key = $ value ;
226219 }
227- $ data [ $ new_key ] = $ value ;
228- } else {
229- if ( $ new_key !== $ key ) {
230- unset( $ data ->$ key );
231- }
232- $ data ->$ new_key = $ value ;
233220 }
234221 }
235222 } catch ( \Error $ exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
@@ -303,6 +290,114 @@ private function replace_string( $data ) {
303290 return $ data ;
304291 }
305292
293+ /**
294+ * Recurse into an array, replacing string keys as well as values.
295+ *
296+ * Renamed keys keep their original position. Shared references between
297+ * elements are preserved by iterating by reference. When two keys would
298+ * map onto the same key, the array is left unchanged and a warning is
299+ * emitted instead of silently discarding a value.
300+ *
301+ * @param array $data The array to process.
302+ * @param int $recursion_level Current recursion depth.
303+ * @param array $visited_data Previously seen arrays and objects.
304+ * @return array The processed array.
305+ */
306+ private function replace_array_keys ( $ data , $ recursion_level , $ visited_data ) {
307+ $ renames = array ();
308+ foreach ( array_keys ( $ data ) as $ key ) {
309+ $ renames [ $ key ] = is_string ( $ key ) ? $ this ->replace_string ( $ key ) : $ key ;
310+ }
311+ if ( count ( array_unique ( $ renames , SORT_REGULAR ) ) !== count ( $ renames ) ) {
312+ \WP_CLI ::warning ( 'Skipping key replacement for an array: two keys would map onto the same key and a value would be lost. ' );
313+ return $ data ;
314+ }
315+ $ replaced = array ();
316+ foreach ( $ data as $ key => &$ value ) {
317+ $ value = $ this ->run_recursively ( $ value , false , $ recursion_level + 1 , $ visited_data );
318+ $ replaced [ $ renames [ $ key ] ] =& $ value ;
319+ }
320+ unset( $ value );
321+ return $ replaced ;
322+ }
323+
324+ /**
325+ * Recurse into an object, replacing string property names as well as values.
326+ *
327+ * When two property names would map onto the same name, the object is
328+ * left unchanged and a warning is emitted instead of silently
329+ * discarding a value.
330+ *
331+ * @param object $data The object to process.
332+ * @param bool $is_array_access Whether the object implements ArrayAccess.
333+ * @param int $recursion_level Current recursion depth.
334+ * @param array $visited_data Previously seen arrays and objects.
335+ * @return object The processed object.
336+ */
337+ private function replace_object_property_names ( $ data , $ is_array_access , $ recursion_level , $ visited_data ) {
338+ $ renames = array ();
339+ foreach ( $ data as $ key => $ value ) {
340+ $ renames [ $ key ] = is_string ( $ key ) ? $ this ->replace_string ( $ key ) : $ key ;
341+ }
342+ if ( count ( array_unique ( $ renames , SORT_REGULAR ) ) !== count ( $ renames ) ) {
343+ \WP_CLI ::warning ( 'Skipping key replacement for an object: two property names would map onto the same name and a value would be lost. ' );
344+ return $ data ;
345+ }
346+ 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 );
348+ if ( $ new_key !== $ key ) {
349+ $ this ->remove_property ( $ data , $ is_array_access , $ key );
350+ }
351+ $ this ->write_property ( $ data , $ is_array_access , $ new_key , $ value );
352+ }
353+ return $ data ;
354+ }
355+
356+ /**
357+ * Read one property or offset from an object or ArrayAccess instance.
358+ *
359+ * @param object $data The object to read from.
360+ * @param bool $is_array_access Whether the object implements ArrayAccess.
361+ * @param string $key The property name or offset.
362+ * @return mixed The property value.
363+ */
364+ private function read_property ( $ data , $ is_array_access , $ key ) {
365+ return $ is_array_access ? $ data [ $ key ] : $ data ->$ key ;
366+ }
367+
368+ /**
369+ * Remove one property or offset from an object or ArrayAccess instance.
370+ *
371+ * @param object $data The object to modify, by reference.
372+ * @param bool $is_array_access Whether the object implements ArrayAccess.
373+ * @param string $key The property name or offset.
374+ * @return void
375+ */
376+ private function remove_property ( &$ data , $ is_array_access , $ key ) {
377+ if ( $ is_array_access ) {
378+ unset( $ data [ $ key ] );
379+ } else {
380+ unset( $ data ->$ key );
381+ }
382+ }
383+
384+ /**
385+ * Write one property or offset on an object or ArrayAccess instance.
386+ *
387+ * @param object $data The object to modify, by reference.
388+ * @param bool $is_array_access Whether the object implements ArrayAccess.
389+ * @param string $key The property name or offset.
390+ * @param mixed $value The value to write.
391+ * @return void
392+ */
393+ private function write_property ( &$ data , $ is_array_access , $ key , $ value ) {
394+ if ( $ is_array_access ) {
395+ $ data [ $ key ] = $ value ;
396+ } else {
397+ $ data ->$ key = $ value ;
398+ }
399+ }
400+
306401 /**
307402 * Gets existing data saved for this run when logging.
308403 * @return string[] Array of data strings, prior to replacements.
0 commit comments