From f639d82c67c8432bb7cb18d9a1be97b4ef3aea99 Mon Sep 17 00:00:00 2001 From: Davert Date: Tue, 25 Feb 2014 05:07:26 +0200 Subject: [PATCH 1/2] fixed cloning non-clonable items --- src/DeepCopy/DeepCopy.php | 11 ++++++++--- tests/DeepCopyTest/DeepCopyTest.php | 10 +++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index b7d54f6..b5665b5 100755 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -90,12 +90,17 @@ private function copyObject($object) return $this->hashMap[$objectHash]; } - $newObject = clone $object; + $reflectedObject = new \ReflectionObject($object); + + if (!$reflectedObject->isCloneable()) { + $this->hashMap[$objectHash] = $object; + return $object; + } + $newObject = clone $object; $this->hashMap[$objectHash] = $newObject; - $class = new \ReflectionObject($newObject); - foreach ($class->getProperties() as $property) { + foreach ($reflectedObject->getProperties() as $property) { $this->copyObjectProperty($newObject, $property); } diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index e84c431..ebb0b61 100755 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -91,6 +91,14 @@ public function testDynamicProperties() $this->assertDeepCopyOf($a, $a2); } + public function testNonClonableItems() + { + $a = new \ReflectionClass('DeepCopyTest\A'); + $deepCopy = new DeepCopy(); + $a2 = $deepCopy->copy($a); + $this->assertSame($a, $a2); + } + /** * @test */ @@ -135,7 +143,7 @@ public function filtersShouldBeAppliedAndBreakPropertyCopying() $new = $deepCopy->copy($o); $this->assertSame($o->property1, $new->property1); - } + } } class A From 6247d82c1b441fdbcf12f67f4f134ce68d7ee87c Mon Sep 17 00:00:00 2001 From: Davert Date: Fri, 21 Mar 2014 05:17:37 +0200 Subject: [PATCH 2/2] added "skipUncloneable" config --- src/DeepCopy/DeepCopy.php | 17 +++++++++++++++-- tests/DeepCopyTest/DeepCopyTest.php | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/DeepCopy/DeepCopy.php b/src/DeepCopy/DeepCopy.php index b5665b5..fbeaf9c 100755 --- a/src/DeepCopy/DeepCopy.php +++ b/src/DeepCopy/DeepCopy.php @@ -25,6 +25,19 @@ class DeepCopy */ private $filters = []; + private $skipUncloneable = false; + + /** + * Cloning uncloneable properties won't throw exception. + * @param $skipUncloneable + * @return $this + */ + public function skipUncloneable($skipUncloneable = true) + { + $this->skipUncloneable = $skipUncloneable; + return $this; + } + /** * Perform a deep copy of the object. * @param object $object @@ -92,9 +105,9 @@ private function copyObject($object) $reflectedObject = new \ReflectionObject($object); - if (!$reflectedObject->isCloneable()) { + if (!$reflectedObject->isCloneable() and $this->skipUncloneable) { $this->hashMap[$objectHash] = $object; - return $object; + return $object; } $newObject = clone $object; diff --git a/tests/DeepCopyTest/DeepCopyTest.php b/tests/DeepCopyTest/DeepCopyTest.php index ebb0b61..0d96156 100755 --- a/tests/DeepCopyTest/DeepCopyTest.php +++ b/tests/DeepCopyTest/DeepCopyTest.php @@ -95,7 +95,7 @@ public function testNonClonableItems() { $a = new \ReflectionClass('DeepCopyTest\A'); $deepCopy = new DeepCopy(); - $a2 = $deepCopy->copy($a); + $a2 = $deepCopy->skipUncloneable()->copy($a); $this->assertSame($a, $a2); }