Skip to content

Commit

Permalink
Copy property after applying doctrine filter (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
amatoenot authored and theofidry committed Jan 9, 2020
1 parent b638b62 commit 0ef5985
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
35 changes: 35 additions & 0 deletions fixtures/f010/A.php
@@ -0,0 +1,35 @@
<?php

namespace DeepCopy\f010;

use Doctrine\Common\Persistence\Proxy;

class A implements Proxy
{
/** @var object */
private $foo;

/**
* @inheritdoc
*/
public function __load()
{
}

/**
* @inheritdoc
*/
public function __isInitialized()
{
}

public function getFoo(): object
{
return $this->foo;
}

public function setFoo(object $foo): void
{
$this->foo = $foo;
}
}
13 changes: 13 additions & 0 deletions fixtures/f010/B.php
@@ -0,0 +1,13 @@
<?php

namespace DeepCopy\f010;

class B
{
public $foo = 1;

public function __clone()
{
$this->foo = null;
}
}
9 changes: 1 addition & 8 deletions src/DeepCopy/DeepCopy.php
Expand Up @@ -7,6 +7,7 @@
use DateTimeZone;
use DeepCopy\Exception\CloneException;
use DeepCopy\Filter\ChainableFilter;
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
use DeepCopy\Filter\Filter;
use DeepCopy\Matcher\Matcher;
use DeepCopy\Reflection\ReflectionHelper;
Expand Down Expand Up @@ -185,8 +186,6 @@ private function copyObjectProperty(object $object, ReflectionProperty $property
return;
}

$filterWasApplied = false;

// Apply the filters
foreach ($this->filters as [$matcher, $filter]) {
/** @var Matcher $matcher */
Expand All @@ -201,8 +200,6 @@ function ($object) {
}
);

$filterWasApplied = true;

if ($filter instanceof ChainableFilter) {
continue;
}
Expand All @@ -212,10 +209,6 @@ function ($object) {
}
}

if ($filterWasApplied) {
return;
}

$property->setAccessible(true);
$propertyValue = $property->getValue($object);

Expand Down
15 changes: 15 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Expand Up @@ -17,6 +17,7 @@
use DeepCopy\f007;
use DeepCopy\f008;
use DeepCopy\f009;
use DeepCopy\f010;
use DeepCopy\Filter\Doctrine\DoctrineProxyFilter;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\ReplaceFilter;
Expand Down Expand Up @@ -477,4 +478,18 @@ private function assertEqualButNotSame($expected, $val)
$this->assertEquals($expected, $val);
$this->assertNotSame($expected, $val);
}

public function test_it_can_copy_property_after_applying_doctrine_proxy_filter()
{
$object = new f010\A();
$object->setFoo(new f010\B());

$deepCopy = new DeepCopy();
$deepCopy->addFilter(new DoctrineProxyFilter(), new DoctrineProxyMatcher());

/** @var f010\A $copy */
$copy = $deepCopy->copy($object);

$this->assertNotEquals($copy->getFoo(), $object->getFoo());
}
}

0 comments on commit 0ef5985

Please sign in to comment.