Skip to content

Commit 343883a

Browse files
committed
fix
1 parent ea193d4 commit 343883a

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ DeepCopy helps you create deep copies (clones) of your objects. It is designed t
2727
1. [`DoctrineProxyFilter`](#doctrineproxyfilter-filter)
2828
1. [`ReplaceFilter`](#replacefilter-type-filter)
2929
1. [`ShallowCopyFilter`](#doctrinecollectionfilter-type-filter)
30+
1. [Edge cases](#edge-cases)
3031
1. [Contributing](#contributing)
3132
1. [Tests](#tests)
3233

@@ -297,6 +298,15 @@ $myServiceWithMocks = new MyService(m::mock(MyDependency1::class), m::mock(MyDep
297298
```
298299

299300

301+
## Edge cases
302+
303+
The following structures cannot be deep-copied with PHP Reflection. As a result they are shallow cloned and filters are
304+
not applied. There is two ways for you to handle them:
305+
306+
- Implement your own `__clone()` method
307+
- Use a filter with a type matcher
308+
309+
300310
## Contributing
301311

302312
DeepCopy is distributed under the MIT license.

fixtures/f007/FooDateInterval.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
class FooDateInterval extends DateInterval
88
{
9+
public $cloned = false;
10+
911
public function __clone()
1012
{
13+
$this->cloned = true;
1114
}
1215
}

fixtures/f007/FooDateTimeZone.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
class FooDateTimeZone extends DateTimeZone
88
{
9+
public $cloned = false;
10+
911
public function __clone()
1012
{
13+
$this->cloned = true;
1114
}
1215
}

src/DeepCopy/DeepCopy.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ private function copyObject($object)
187187
return $newObject;
188188
}
189189

190-
$newObjectClass = get_class($newObject);
191-
192190
if ($newObject instanceof DateTimeInterface
193-
|| $newObjectClass === DateTimeZone::class
194-
|| $newObjectClass === DateInterval::class
191+
|| $newObject instanceof DateTimeZone
192+
|| $newObject instanceof DateInterval
195193
) {
196194
return $newObject;
197195
}
@@ -231,7 +229,6 @@ function ($object) {
231229
}
232230
}
233231

234-
// No filter has been applied: simply copy the value
235232
$property->setAccessible(true);
236233
$propertyValue = $property->getValue($object);
237234

tests/DeepCopyTest/DeepCopyTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,29 +158,41 @@ public function test_it_can_copy_an_object_with_a_date_object_property()
158158
/**
159159
* @ticket https://github.com/myclabs/DeepCopy/pull/70
160160
*/
161-
public function test_it_does_not_skip_the_copy_for_userland_datetimezone()
161+
public function test_it_skips_the_copy_for_userland_datetimezone()
162162
{
163+
$deepCopy = new DeepCopy();
164+
$deepCopy->addFilter(
165+
new SetNullFilter(),
166+
new PropertyNameMatcher('cloned')
167+
);
168+
163169
$object = new stdClass();
164170

165171
$object->dtz = new f007\FooDateTimeZone('UTC');
166172

167-
$copy = deep_copy($object);
173+
$copy = $deepCopy->copy($object);
168174

169-
$this->assertEqualButNotSame($object->dtz, $copy->dtz);
175+
$this->assertTrue($copy->dtz->cloned);
170176
}
171177

172178
/**
173179
* @ticket https://github.com/myclabs/DeepCopy/pull/76
174180
*/
175-
public function test_it_does_not_skip_the_copy_for_userland_dateinterval()
181+
public function test_it_skips_the_copy_for_userland_dateinterval()
176182
{
183+
$deepCopy = new DeepCopy();
184+
$deepCopy->addFilter(
185+
new SetNullFilter(),
186+
new PropertyNameMatcher('cloned')
187+
);
188+
177189
$object = new stdClass();
178190

179191
$object->di = new f007\FooDateInterval('P2D');
180192

181-
$copy = deep_copy($object);
193+
$copy = $deepCopy->copy($object);
182194

183-
$this->assertEqualButNotSame($object->di, $copy->di);
195+
$this->assertTrue($copy->di->cloned);
184196
}
185197

186198
public function test_it_copies_the_private_properties_of_the_parent_class()

0 commit comments

Comments
 (0)