Skip to content

Commit 770e509

Browse files
authored
Fix copying of DateTimeZone objects (#86)
Closes #70.
1 parent 7a84e10 commit 770e509

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

fixtures/f007/FooDateTimeZone.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace DeepCopy\f007;
4+
5+
use DateTimeZone;
6+
7+
class FooDateTimeZone extends DateTimeZone
8+
{
9+
public $cloned = false;
10+
11+
public function __clone()
12+
{
13+
$this->cloned = true;
14+
}
15+
}

src/DeepCopy/DeepCopy.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
namespace DeepCopy;
44

55
use DateTimeInterface;
6+
use DateTimeZone;
67
use DeepCopy\Exception\CloneException;
78
use DeepCopy\Filter\Filter;
89
use DeepCopy\Matcher\Matcher;
9-
use DeepCopy\TypeFilter\Spl\SplDoublyLinkedList;
10+
use DeepCopy\TypeFilter\Spl\SplDoublyLinkedListFilter;
1011
use DeepCopy\TypeFilter\TypeFilter;
1112
use DeepCopy\TypeMatcher\TypeMatcher;
1213
use ReflectionObject;
1314
use ReflectionProperty;
1415
use DeepCopy\Reflection\ReflectionHelper;
16+
use SplDoublyLinkedList;
1517

1618
/**
1719
* @final
@@ -55,7 +57,7 @@ public function __construct($useCloneMethod = false)
5557
{
5658
$this->useCloneMethod = $useCloneMethod;
5759

58-
$this->addTypeFilter(new SplDoublyLinkedList($this), new TypeMatcher('SplDoublyLinkedList'));
60+
$this->addTypeFilter(new SplDoublyLinkedListFilter($this), new TypeMatcher(SplDoublyLinkedList::class));
5961
}
6062

6163
/**
@@ -184,7 +186,7 @@ private function copyObject($object)
184186
return $newObject;
185187
}
186188

187-
if ($newObject instanceof DateTimeInterface) {
189+
if ($newObject instanceof DateTimeInterface || get_class($newObject) === DateTimeZone::class) {
188190
return $newObject;
189191
}
190192

tests/DeepCopyTest/DeepCopyTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use DateTimeImmutable;
7+
use DateTimeZone;
78
use DeepCopy\DeepCopy;
89
use DeepCopy\Exception\CloneException;
910
use DeepCopy\f001;
@@ -132,18 +133,35 @@ public function test_dynamic_properties_are_copied()
132133

133134
/**
134135
* @ticket https://github.com/myclabs/DeepCopy/issues/38
136+
* @ticket https://github.com/myclabs/DeepCopy/pull/70
135137
*/
136138
public function test_it_can_copy_an_object_with_a_date_object_property()
137139
{
138140
$object = new stdClass();
139141

140142
$object->d1 = new DateTime();
141143
$object->d2 = new DateTimeImmutable();
144+
$object->dtz = new DateTimeZone('UTC');
142145

143146
$copy = deep_copy($object);
144147

145148
$this->assertEqualButNotSame($object->d1, $copy->d1);
146149
$this->assertEqualButNotSame($object->d2, $copy->d2);
150+
$this->assertEqualButNotSame($object->dtz, $copy->dtz);
151+
}
152+
153+
/**
154+
* @ticket https://github.com/myclabs/DeepCopy/pull/70g
155+
*/
156+
public function test_it_does_not_skip_the_copy_for_userland_datetimezone()
157+
{
158+
$object = new stdClass();
159+
160+
$object->dtz = new DateTimeZone('UTC');
161+
162+
$copy = deep_copy($object);
163+
164+
$this->assertEqualButNotSame($object->dtz, $copy->dtz);
147165
}
148166

149167
public function test_it_copies_the_private_properties_of_the_parent_class()

0 commit comments

Comments
 (0)