-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deep copying of ArrayObject in PHP 7.4 (#145)
- Loading branch information
Showing
4 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace DeepCopy\TypeFilter\Spl; | ||
|
||
use ArrayObject; | ||
use DeepCopy\DeepCopy; | ||
use DeepCopy\TypeFilter\TypeFilter; | ||
|
||
/** | ||
* In PHP 7.4 the storage of an ArrayObject isn't returned as | ||
* ReflectionProperty. So we deep copy its array copy. | ||
*/ | ||
final class ArrayObjectFilter implements TypeFilter | ||
{ | ||
/** | ||
* @var DeepCopy | ||
*/ | ||
private $copier; | ||
|
||
public function __construct(DeepCopy $copier) | ||
{ | ||
$this->copier = $copier; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function apply($arrayObject) | ||
{ | ||
return new ArrayObject( | ||
$this->copier->copy($arrayObject->getArrayCopy()), | ||
$arrayObject->getFlags(), | ||
$arrayObject->getIteratorClass() | ||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/DeepCopyTest/TypeFilter/Spl/ArrayObjectFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace DeepCopyTest\TypeFilter\Spl; | ||
|
||
use ArrayObject; | ||
use DeepCopy\DeepCopy; | ||
use DeepCopy\TypeFilter\Spl\ArrayObjectFilter; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use RecursiveArrayIterator; | ||
|
||
/** | ||
* @author Dominic Tubach <[email protected]> | ||
* | ||
* @covers \DeepCopy\TypeFilter\Spl\ArrayObjectFilter | ||
*/ | ||
final class ArrayObjectFilterTest extends TestCase | ||
{ | ||
/** | ||
* @var ArrayObjectFilter | ||
*/ | ||
private $arrayObjectFilter; | ||
|
||
/** | ||
* @var DeepCopy|ObjectProphecy | ||
*/ | ||
private $copierProphecy; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->copierProphecy = $this->prophesize(DeepCopy::class); | ||
$this->arrayObjectFilter = new ArrayObjectFilter( | ||
$this->copierProphecy->reveal() | ||
); | ||
} | ||
|
||
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']); | ||
|
||
/** @var \ArrayObject $newArrayObject */ | ||
$newArrayObject = $this->arrayObjectFilter->apply($arrayObject); | ||
$this->assertSame(['copy' => 'bar'], $newArrayObject->getArrayCopy()); | ||
$this->assertSame(ArrayObject::ARRAY_AS_PROPS, $newArrayObject->getFlags()); | ||
$this->assertSame(RecursiveArrayIterator::class, $newArrayObject->getIteratorClass()); | ||
} | ||
} |