Skip to content

Commit 8797224

Browse files
swissspidyclaude
andcommitted
Keep replacing values when key renames would collide
When two keys (or property names) would map onto the same key, the array/object was returned untouched, so its values and nested data were skipped entirely, leaving more stale strings than running without --recurse-keys. Fall back to keeping the original keys while still recursing into the values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 244e895 commit 8797224

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ Feature: Do global search/replace
17211721
And a setup-recurse-keys-collision.php file:
17221722
"""
17231723
<?php
1724-
update_option( 'recurse_keys_collision_test', array( 'https://old.example.com/key' => 'v1', 'https://new.example.com/key' => 'v2' ), false );
1724+
update_option( 'recurse_keys_collision_test', array( 'https://old.example.com/key' => 'v1', 'https://new.example.com/key' => 'v2', 'other' => 'https://old.example.com/value' ), false );
17251725
"""
17261726
And a check-recurse-keys-collision.php file:
17271727
"""
@@ -1751,14 +1751,18 @@ Feature: Do global search/replace
17511751
"""
17521752
"https://new.example.com/key":"v2"
17531753
"""
1754+
And STDOUT should contain:
1755+
"""
1756+
"other":"https://new.example.com/value"
1757+
"""
17541758

17551759
@require-mysql
17561760
Scenario: Refuse to rename colliding object property names with --recurse-keys
17571761
Given a WP install
17581762
And a setup-recurse-keys-object-collision.php file:
17591763
"""
17601764
<?php
1761-
update_option( 'recurse_keys_object_collision_test', (object) array( 'https://old.example.com/prop' => 'v1', 'https://new.example.com/prop' => 'v2' ), false );
1765+
update_option( 'recurse_keys_object_collision_test', (object) array( 'https://old.example.com/prop' => 'v1', 'https://new.example.com/prop' => 'v2', 'other' => 'https://old.example.com/value' ), false );
17621766
"""
17631767
And a check-recurse-keys-object-collision.php file:
17641768
"""
@@ -1788,6 +1792,10 @@ Feature: Do global search/replace
17881792
"""
17891793
"https://new.example.com/prop":"v2"
17901794
"""
1795+
And STDOUT should contain:
1796+
"""
1797+
"other":"https://new.example.com/value"
1798+
"""
17911799

17921800
@require-mysql
17931801
Scenario: Preserve shared references when renaming keys with --recurse-keys

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ private function replace_string( $data ) {
295295
*
296296
* Renamed keys keep their original position. Shared references between
297297
* 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.
298+
* map onto the same key, the keys are left unchanged (values are still
299+
* replaced) and a warning is emitted instead of silently discarding a value.
300300
*
301301
* @param array $data The array to process.
302302
* @param int $recursion_level Current recursion depth.
@@ -310,7 +310,7 @@ private function replace_array_keys( $data, $recursion_level, $visited_data ) {
310310
}
311311
if ( count( array_unique( $renames, SORT_REGULAR ) ) !== count( $renames ) ) {
312312
\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;
313+
$renames = array_combine( array_keys( $renames ), array_keys( $renames ) );
314314
}
315315
$replaced = array();
316316
foreach ( $data as $key => &$value ) {
@@ -324,9 +324,9 @@ 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 object is
328-
* left unchanged and a warning is emitted instead of silently
329-
* discarding a value.
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.
330330
*
331331
* @param object $data The object to process.
332332
* @param bool $is_array_access Whether the object implements ArrayAccess.
@@ -341,7 +341,7 @@ private function replace_object_property_names( $data, $is_array_access, $recurs
341341
}
342342
if ( count( array_unique( $renames, SORT_REGULAR ) ) !== count( $renames ) ) {
343343
\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;
344+
$renames = array_combine( array_keys( $renames ), array_keys( $renames ) );
345345
}
346346
foreach ( $renames as $key => $new_key ) {
347347
$value = $this->run_recursively( $this->read_property( $data, $is_array_access, $key ), false, $recursion_level + 1, $visited_data );

0 commit comments

Comments
 (0)