Skip to content

Commit

Permalink
Fix copying objects extending ArrayObject (#152) (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Jun 29, 2020
1 parent 5796d12 commit fcbffe2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
16 changes: 16 additions & 0 deletions fixtures/f011/ArrayObjectExtended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace DeepCopy\f011;

use ArrayObject;

class ArrayObjectExtended extends ArrayObject
{
public $x;

public function __construct($x)
{
parent::__construct();
$this->x = $x;
}
}
12 changes: 6 additions & 6 deletions src/DeepCopy/TypeFilter/Spl/ArrayObjectFilter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace DeepCopy\TypeFilter\Spl;

use ArrayObject;
use DeepCopy\DeepCopy;
use DeepCopy\TypeFilter\TypeFilter;

Expand All @@ -26,11 +25,12 @@ public function __construct(DeepCopy $copier)
*/
public function apply($arrayObject)
{
return new ArrayObject(
$this->copier->copy($arrayObject->getArrayCopy()),
$arrayObject->getFlags(),
$arrayObject->getIteratorClass()
);
$clone = clone $arrayObject;
foreach ($arrayObject->getArrayCopy() as $k => $v) {
$clone->offsetSet($k, $this->copier->copy($v));
}

return $clone;
}
}

19 changes: 18 additions & 1 deletion tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use DeepCopy\f007;
use DeepCopy\f008;
use DeepCopy\f009;
use DeepCopy\f011;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\PropertyNameMatcher;
Expand Down Expand Up @@ -391,14 +392,30 @@ public function test_it_can_deep_copy_an_array_object()
{
$foo = new f003\Foo('foo');
$foo->setProp('bar');
$object = new ArrayObject(['foo' => $foo, \ArrayObject::ARRAY_AS_PROPS, \RecursiveArrayIterator::class]);
$object = new ArrayObject(['foo' => $foo, ArrayObject::ARRAY_AS_PROPS, \RecursiveArrayIterator::class]);

$copy = deep_copy($object);

$this->assertEqualButNotSame($object, $copy);
$this->assertEqualButNotSame($foo, $copy['foo']);
}

/**
* @ticket https://github.com/myclabs/DeepCopy/issues/152
*/
public function test_it_clones_objects_extending_array_object()
{
$object = new f011\ArrayObjectExtended('foo');
$object->setFlags(ArrayObject::ARRAY_AS_PROPS);
$object->setIteratorClass(\RecursiveArrayIterator::class);
$object['a'] = new f011\ArrayObjectExtended('bar');

$copy = deep_copy($object);

$this->assertEqualButNotSame($object, $copy);
$this->assertEqualButNotSame($object['a'], $copy['a']);
}

/**
* @ticket https://github.com/myclabs/DeepCopy/pull/49
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/DeepCopyTest/TypeFilter/Spl/ArrayObjectFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ protected function setUp(): void
public function test_it_deep_copies_an_array_object(): void
{
$arrayObject = new ArrayObject(['foo' => 'bar'], ArrayObject::ARRAY_AS_PROPS, RecursiveArrayIterator::class);
$this->copierProphecy->copy(['foo' => 'bar'])->willReturn(['copy' => 'bar']);
$this->copierProphecy->copy('bar')->willReturn('baz');

/** @var \ArrayObject $newArrayObject */
$newArrayObject = $this->arrayObjectFilter->apply($arrayObject);
$this->assertSame(['copy' => 'bar'], $newArrayObject->getArrayCopy());
$this->assertSame(['foo' => 'baz'], $newArrayObject->getArrayCopy());
$this->assertSame(ArrayObject::ARRAY_AS_PROPS, $newArrayObject->getFlags());
$this->assertSame(RecursiveArrayIterator::class, $newArrayObject->getIteratorClass());
}
Expand Down

0 comments on commit fcbffe2

Please sign in to comment.