Skip to content

Commit 2e533fb

Browse files
swissspidyclaude
andcommitted
Harden --recurse-keys object property renames
- Compare object property names as strings, so an unchanged numeric property name such as "8" is no longer removed and re-added (which reordered the object and changed the stored value even without a match). - Restore the object on Exception as well as Error, e.g. when an allowed ArrayObject subclass throws from offsetUnset() after an earlier removal, and restore the original order of the remaining properties/offsets. - Bind references only for renamed properties; read and assign properties that keep their name normally, so PHP 8.4 set hooks still apply. - Move only the log entries appended for each key instead of rebuilding the whole log with array_splice() per key. - Assert that the writable property is actually renamed in the readonly scenario, and add regression scenarios for the numeric property order and the exception restore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 7a81682 commit 2e533fb

2 files changed

Lines changed: 166 additions & 19 deletions

File tree

‎features/search-replace.feature‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2078,7 +2078,11 @@ Feature: Do global search/replace
20782078
"""
20792079
And STDOUT should contain:
20802080
"""
2081-
s:12:"keep-mutable";
2081+
s:14:"newtok_mutable";s:12:"keep-mutable";
2082+
"""
2083+
And STDOUT should not contain:
2084+
"""
2085+
"oldtok_mutable"
20822086
"""
20832087

20842088
@require-mysql
@@ -2127,3 +2131,92 @@ Feature: Do global search/replace
21272131
"""
21282132
changed
21292133
"""
2134+
2135+
@require-mysql
2136+
Scenario: Keep the order of numeric object property names that are not renamed with --recurse-keys
2137+
Given a WP install
2138+
And a setup-recurse-keys-numeric-property.php file:
2139+
"""
2140+
<?php
2141+
$object = new stdClass();
2142+
$object->{'8'} = 'top';
2143+
$object->label = 'keep';
2144+
update_option( 'recurse_keys_numeric_property_test', array( 'item' => $object, 'other' => 'oldtok' ), false );
2145+
"""
2146+
And a check-recurse-keys-numeric-property.php file:
2147+
"""
2148+
<?php
2149+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_numeric_property_test'" );
2150+
"""
2151+
And I run `wp eval-file setup-recurse-keys-numeric-property.php`
2152+
2153+
When I run `wp search-replace 'oldtok' 'newtok' wp_options --include-columns=option_value --recurse-keys`
2154+
Then STDOUT should contain:
2155+
"""
2156+
Success:
2157+
"""
2158+
2159+
When I run `wp eval-file check-recurse-keys-numeric-property.php`
2160+
Then STDOUT should be:
2161+
"""
2162+
a:2:{s:4:"item";O:8:"stdClass":2:{s:1:"8";s:3:"top";s:5:"label";s:4:"keep";}s:5:"other";s:6:"newtok";}
2163+
"""
2164+
2165+
When I run `wp search-replace 'stdClass' 'otherClass' wp_options --include-columns=option_value --recurse-keys --dry-run --report-changed-only`
2166+
Then STDOUT should not contain:
2167+
"""
2168+
wp_options
2169+
"""
2170+
2171+
@require-mysql
2172+
Scenario: Restore an object when removing a property throws an exception with --recurse-keys
2173+
Given a WP install
2174+
And a wp-content/mu-plugins/recurse-keys-throwing-unset.php file:
2175+
"""
2176+
<?php
2177+
class Recurse_Keys_Throwing_Unset_Test extends ArrayObject {
2178+
public static $unset_calls = 0;
2179+
#[ReturnTypeWillChange]
2180+
public function offsetUnset( $key ) {
2181+
if ( 2 === ++self::$unset_calls ) {
2182+
throw new Exception( 'second removal failed' );
2183+
}
2184+
parent::offsetUnset( $key );
2185+
}
2186+
}
2187+
if ( class_exists( 'WP_CLI' ) ) {
2188+
WP_CLI::add_hook(
2189+
'search_replace_unserialize_options',
2190+
function () {
2191+
return [ 'allowed_classes' => [ 'stdClass', 'Recurse_Keys_Throwing_Unset_Test' ] ];
2192+
}
2193+
);
2194+
}
2195+
"""
2196+
And a setup-recurse-keys-throwing-unset.php file:
2197+
"""
2198+
<?php
2199+
$object = new Recurse_Keys_Throwing_Unset_Test( array( 'oldtok_a' => 'value-a', 'keep' => 'value-keep', 'oldtok_b' => 'value-b' ) );
2200+
update_option( 'recurse_keys_throwing_unset_test', array( 'item' => $object ), false );
2201+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_throwing_unset_test'" );
2202+
"""
2203+
And a check-recurse-keys-throwing-unset.php file:
2204+
"""
2205+
<?php
2206+
echo $GLOBALS['wpdb']->get_var( "SELECT option_value FROM {$GLOBALS['wpdb']->options} WHERE option_name = 'recurse_keys_throwing_unset_test'" );
2207+
"""
2208+
And I run `wp eval-file setup-recurse-keys-throwing-unset.php`
2209+
And save STDOUT as {ORIGINAL}
2210+
2211+
When I try `wp search-replace 'oldtok' 'newtok' wp_options --include-columns=option_value --recurse-keys`
2212+
Then the return code should be 0
2213+
2214+
When I run `wp eval-file check-recurse-keys-throwing-unset.php`
2215+
Then STDOUT should be:
2216+
"""
2217+
{ORIGINAL}
2218+
"""
2219+
And STDOUT should contain:
2220+
"""
2221+
s:8:"oldtok_a";s:7:"value-a";s:4:"keep";s:10:"value-keep";s:8:"oldtok_b";s:7:"value-b";
2222+
"""

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)