Skip to content

Commit

Permalink
Merge pull request #76 from wp-cli/68-handle-incomplete-class-seriali…
Browse files Browse the repository at this point in the history
…zation

Handle incomplete class (un)serialization gracefully
  • Loading branch information
schlessera authored Apr 21, 2018
2 parents b45eeb4 + 17c0a88 commit df1092f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
22 changes: 22 additions & 0 deletions features/search-replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1032,3 +1032,25 @@ Feature: Do global search/replace
"""
Success: 1 replacement to be made.
"""

# Regression test for https://github.com/wp-cli/search-replace-command/issues/68
Scenario: Incomplete classes are handled gracefully during (un)serialization

Given a WP install
And I run `wp option add cereal_isation 'a:1:{i:0;O:10:"CornFlakes":0:{}}'`

When I try `wp search-replace CornFlakes Smacks`
Then STDERR should contain:
"""
Warning: Skipping an uninitialized class "CornFlakes", replacements might not be complete.
"""
And STDOUT should contain:
"""
Success: Made 0 replacements.
"""

When I run `wp option get cereal_isation`
Then STDOUT should contain:
"""
a:1:{i:0;O:10:"CornFlakes":0:{}}
"""
33 changes: 25 additions & 8 deletions src/WP_CLI/SearchReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WP_CLI;

use ArrayObject;
use Exception;

class SearchReplacer {
Expand Down Expand Up @@ -89,9 +90,19 @@ private function _run( $data, $serialised, $recursion_level = 0, $visited_data =
}
}

elseif ( $this->recurse_objects && is_object( $data ) ) {
foreach ( $data as $key => $value ) {
$data->$key = $this->_run( $value, false, $recursion_level + 1, $visited_data );
elseif ( $this->recurse_objects && ( is_object( $data ) || $data instanceof \__PHP_Incomplete_Class ) ) {
if ( $data instanceof \__PHP_Incomplete_Class ) {
$array = new ArrayObject( $data );
\WP_CLI::warning(
sprintf(
'Skipping an uninitialized class "%s", replacements might not be complete.',
$array['__PHP_Incomplete_Class_Name']
)
);
} else {
foreach ( $data as $key => $value ) {
$data->$key = $this->_run( $value, false, $recursion_level + 1, $visited_data );
}
}
}

Expand Down Expand Up @@ -155,13 +166,19 @@ public function clear_log_data() {
* @return string Error constant name.
*/
private function preg_error_message( $error ) {
$constants = get_defined_constants( true );
if ( ! array_key_exists( 'pcre', $constants ) ) {
return '<unknown error>';
static $error_names = null;

if ( null === $error_names ) {
$definitions = get_defined_constants( true );
$pcre_constants = array_key_exists( 'pcre', $definitions )
? $definitions['pcre']
: array();
$error_names = array_flip( $pcre_constants );
}

$names = array_flip( $constants['pcre'] );
return isset( $names[ $error ] ) ? $names[ $error ] : '<unknown error>';
return isset( $error_names[ $error ] )
? $error_names[ $error ]
: '<unknown error>';
}
}

0 comments on commit df1092f

Please sign in to comment.