OSDN Git Service

clone object instead of calling constructor; fixes #592
authorscribu <mail@scribu.net>
Tue, 23 Jul 2013 19:03:53 +0000 (22:03 +0300)
committerscribu <mail@scribu.net>
Tue, 23 Jul 2013 19:08:41 +0000 (22:08 +0300)
php/utils.php
tests/test-unserialize-replace.php [new file with mode: 0644]

index 8b8c463..0bb98f1 100644 (file)
@@ -217,19 +217,15 @@ function recursive_unserialize_replace( $from = '', $to = '', $data = '', $seria
                        }
 
                        $data = $_tmp;
-                       unset( $_tmp );
                }
 
-               // Submitted by Tina Matter
                elseif ( is_object( $data ) ) {
-                       $dataClass = get_class( $data );
-                       $_tmp = new $dataClass( );
+                       $_tmp = clone( $data );
                        foreach ( $data as $key => $value ) {
                                $_tmp->$key = recursive_unserialize_replace( $from, $to, $value, false );
                        }
 
                        $data = $_tmp;
-                       unset( $_tmp );
                }
 
                else {
@@ -255,7 +251,7 @@ function recursive_unserialize_replace( $from = '', $to = '', $data = '', $seria
  * @param array|string  $fields     Named fields for each item of data. Can be array or comma-separated list
  */
 function format_items( $format, $items, $fields ) {
-       
+
        if ( 'ids' == $format ) {
                echo implode( ' ', $items );
                return;
diff --git a/tests/test-unserialize-replace.php b/tests/test-unserialize-replace.php
new file mode 100644 (file)
index 0000000..3389336
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+class UnserializeReplaceTest extends PHPUnit_Framework_TestCase {
+
+       function testPrivateConstructor() {
+               $old_obj = ClassWithPrivateConstructor::get_instance();
+
+               $new_obj = WP_CLI\Utils\recursive_unserialize_replace( 'foo', 'bar', $old_obj );
+               $this->assertEquals( 'bar', $new_obj->prop );
+       }
+}
+
+
+class ClassWithPrivateConstructor {
+
+       public $prop = 'foo';
+
+       private function __construct() {}
+
+       public static function get_instance() {
+               return new self;
+       }
+}
+