File tree Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Expand file tree Collapse file tree 2 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,19 @@ class DeepCopy
22
22
*/
23
23
private $ filters = [];
24
24
25
+ private $ skipUncloneable = false ;
26
+
27
+ /**
28
+ * Cloning uncloneable properties won't throw exception.
29
+ * @param $skipUncloneable
30
+ * @return $this
31
+ */
32
+ public function skipUncloneable ($ skipUncloneable = true )
33
+ {
34
+ $ this ->skipUncloneable = $ skipUncloneable ;
35
+ return $ this ;
36
+ }
37
+
25
38
/**
26
39
* Perform a deep copy of the object.
27
40
* @param object $object
@@ -87,12 +100,17 @@ private function copyObject($object)
87
100
return $ this ->hashMap [$ objectHash ];
88
101
}
89
102
90
- $ newObject = clone $ object ;
103
+ $ reflectedObject = new \ ReflectionObject ( $ object) ;
91
104
105
+ if (!$ reflectedObject ->isCloneable () and $ this ->skipUncloneable ) {
106
+ $ this ->hashMap [$ objectHash ] = $ object ;
107
+ return $ object ;
108
+ }
109
+
110
+ $ newObject = clone $ object ;
92
111
$ this ->hashMap [$ objectHash ] = $ newObject ;
93
112
94
- $ class = new \ReflectionObject ($ newObject );
95
- foreach ($ class ->getProperties () as $ property ) {
113
+ foreach ($ reflectedObject ->getProperties () as $ property ) {
96
114
$ this ->copyObjectProperty ($ newObject , $ property );
97
115
}
98
116
Original file line number Diff line number Diff line change @@ -95,6 +95,14 @@ public function testDynamicProperties()
95
95
$ this ->assertDeepCopyOf ($ a , $ a2 );
96
96
}
97
97
98
+ public function testNonClonableItems ()
99
+ {
100
+ $ a = new \ReflectionClass ('DeepCopyTest\A ' );
101
+ $ deepCopy = new DeepCopy ();
102
+ $ a2 = $ deepCopy ->skipUncloneable ()->copy ($ a );
103
+ $ this ->assertSame ($ a , $ a2 );
104
+ }
105
+
98
106
/**
99
107
* @test
100
108
*/
@@ -139,7 +147,7 @@ public function filtersShouldBeAppliedAndBreakPropertyCopying()
139
147
$ new = $ deepCopy ->copy ($ o );
140
148
141
149
$ this ->assertSame ($ o ->property1 , $ new ->property1 );
142
- }
150
+ }
143
151
}
144
152
145
153
class A
You can’t perform that action at this time.
0 commit comments