Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matchers & Filters: use the right ReflectionProperty #35

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/DeepCopy/DeepCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ private function copyObjectProperty($object, ReflectionProperty $property)
/** @var Filter $filter */
$filter = $item['filter'];

if ($matcher->matches($object, $property->getName())) {
if ($matcher->matches($object, $property)) {
$filter->apply(
$object,
$property->getName(),
$property,
function ($object) {
return $this->recursiveCopy($object);
}
Expand Down
4 changes: 1 addition & 3 deletions src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ class DoctrineCollectionFilter implements Filter
/**
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = new ReflectionProperty($object, $property);

$reflectionProperty->setAccessible(true);
$oldCollection = $reflectionProperty->getValue($object);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DeepCopy\Filter\Filter;
use Doctrine\Common\Collections\ArrayCollection;
use ReflectionProperty;

class DoctrineEmptyCollectionFilter implements Filter
{
Expand All @@ -14,9 +15,8 @@ class DoctrineEmptyCollectionFilter implements Filter
* @param string $property
* @param callable $objectCopier
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue($object, new ArrayCollection());
Expand Down
10 changes: 6 additions & 4 deletions src/DeepCopy/Filter/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

namespace DeepCopy\Filter;

use ReflectionProperty;

/**
* Filter to apply to a property while copying an object
*/
interface Filter
{
/**
* Apply the filter to the object.
* @param object $object
* @param string $property
* @param callable $objectCopier
* @param object $object
* @param ReflectionProperty $reflectionProperty
* @param callable $objectCopier
*/
public function apply($object, $property, $objectCopier);
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier);
}
4 changes: 3 additions & 1 deletion src/DeepCopy/Filter/KeepFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeepCopy\Filter;

use ReflectionProperty;

/**
* Keep the value of a property
*/
Expand All @@ -10,7 +12,7 @@ class KeepFilter implements Filter
/**
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
// Nothing to do
}
Expand Down
5 changes: 3 additions & 2 deletions src/DeepCopy/Filter/ReplaceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeepCopy\Filter;

use ReflectionProperty;

/**
* Replace the value of a property
*/
Expand All @@ -23,9 +25,8 @@ public function __construct(callable $callable)
/**
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);

$value = call_user_func($this->callback, $reflectionProperty->getValue($object));
Expand Down
4 changes: 1 addition & 3 deletions src/DeepCopy/Filter/SetNullFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class SetNullFilter implements Filter
/**
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = new ReflectionProperty($object, $property);

$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, null);
}
Expand Down
4 changes: 3 additions & 1 deletion src/DeepCopy/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeepCopy\Matcher;

use ReflectionProperty;

/**
* Matcher interface
*/
Expand All @@ -12,5 +14,5 @@ interface Matcher
* @param string $property
* @return boolean
*/
public function matches($object, $property);
public function matches($object, ReflectionProperty $reflectionProperty);
}
6 changes: 4 additions & 2 deletions src/DeepCopy/Matcher/PropertyMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeepCopy\Matcher;

use ReflectionProperty;

/**
* Match a specific property of a specific class
*/
Expand Down Expand Up @@ -30,8 +32,8 @@ public function __construct($class, $property)
/**
* {@inheritdoc}
*/
public function matches($object, $property)
public function matches($object, ReflectionProperty $reflectionProperty)
{
return ($object instanceof $this->class) && ($property == $this->property);
return ($object instanceof $this->class) && ($reflectionProperty->getName() == $this->property);
}
}
6 changes: 4 additions & 2 deletions src/DeepCopy/Matcher/PropertyNameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace DeepCopy\Matcher;

use ReflectionProperty;

/**
* Match a property by its name
*/
Expand All @@ -23,8 +25,8 @@ public function __construct($property)
/**
* {@inheritdoc}
*/
public function matches($object, $property)
public function matches($object, ReflectionProperty $reflectionProperty)
{
return $property == $this->property;
return $reflectionProperty->getName() == $this->property;
}
}
3 changes: 1 addition & 2 deletions src/DeepCopy/Matcher/PropertyTypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public function __construct($propertyType)
/**
* {@inheritdoc}
*/
public function matches($object, $property)
public function matches($object, ReflectionProperty $reflectionProperty)
{
$reflectionProperty = new ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);

return $reflectionProperty->getValue($object) instanceof $this->propertyType;
Expand Down
24 changes: 21 additions & 3 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use DeepCopy\DeepCopy;
use DeepCopy\Filter\Filter;
use DeepCopy\Filter\ReplaceFilter;
use DeepCopy\Matcher\PropertyMatcher;
use DeepCopy\Matcher\PropertyTypeMatcher;
use DeepCopy\TypeFilter\TypeFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use ReflectionProperty;

/**
* DeepCopyTest
Expand Down Expand Up @@ -53,6 +56,21 @@ public function testPrivatePropertyOfParentObjectCopy()
$this->assertDeepCopyOf($o, $deepCopy->copy($o));
}

public function testPrivatePropertyOfParentObjectCopyWithFiltersAndMatchers()
{
$o = new E();
$o->setProperty1(new B);
$o->setProperty2(new B);

$deepCopy = new DeepCopy();
$deepCopy->addFilter(new ReplaceFilter(function() {return 'foo';}), new PropertyTypeMatcher('DeepCopyTest\B'));

$new = $deepCopy->copy($o);

$this->assertSame('foo', $new->getProperty1());
$this->assertSame('foo', $new->getProperty2());
}

public function testPropertyArrayCopy()
{
$o = new A();
Expand Down Expand Up @@ -139,8 +157,8 @@ public function filtersShouldBeApplied()
$filter = $this->getMockForAbstractClass('DeepCopy\Filter\Filter');
$filter->expects($this->once())
->method('apply')
->will($this->returnCallback(function($object, $property) {
$object->$property = null;
->will($this->returnCallback(function($object, ReflectionProperty $reflectionProperty) {
$reflectionProperty->setValue($object, null);
}));

$deepCopy = new DeepCopy();
Expand All @@ -164,7 +182,7 @@ public function filtersShouldBeAppliedAndBreakPropertyCopying()
$filter = $this->getMockForAbstractClass('DeepCopy\Filter\Filter');
$filter->expects($this->once())
->method('apply')
->will($this->returnCallback(function($object, $property, $objectCopier) {
->will($this->returnCallback(function($object, ReflectionProperty $reflectionProperty, $objectCopier) {
}));

$deepCopy = new DeepCopy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DeepCopy\Matcher\PropertyMatcher;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use ReflectionProperty;

/**
* Test Doctrine Collection filter
Expand All @@ -15,19 +16,19 @@ class DoctrineCollectionFilterTest extends \PHPUnit_Framework_TestCase
{
public function testApply()
{
$object = new \stdClass();
$object = new DoctrineCollectionFilterTestFixture();
$oldCollection = new ArrayCollection();
$oldCollection->add(new \stdClass());
$object->foo = $oldCollection;
$object->property1 = $oldCollection;

$filter = new DoctrineCollectionFilter();
$filter->apply($object, 'foo', function($item) {
$filter->apply($object, new ReflectionProperty('DeepCopyTest\Filter\Doctrine\DoctrineCollectionFilterTestFixture', 'property1'), function($item) {
return null;
});

$this->assertTrue($object->foo instanceof Collection);
$this->assertNotSame($oldCollection, $object->foo);
$this->assertCount(1, $object->foo);
$this->assertTrue($object->property1 instanceof Collection);
$this->assertNotSame($oldCollection, $object->property1);
$this->assertCount(1, $object->property1);
}

public function testIntegration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DeepCopy\Matcher\PropertyMatcher;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use ReflectionProperty;

/**
* Test Doctrine Collection filter
Expand All @@ -15,19 +16,19 @@ class DoctrineEmptyCollectionFilterTest extends \PHPUnit_Framework_TestCase
{
public function testApply()
{
$object = new \StdClass();
$object = new DoctrineEmptyCollectionFilterTestFixture();

$collection = new ArrayCollection();
$collection->add(new \StdClass());

$object->foo = $collection;
$object->property1 = $collection;

$filter = new DoctrineEmptyCollectionFilter();
$filter->apply($object, 'foo', function($item){ return null; });
$filter->apply($object, new ReflectionProperty('DeepCopyTest\Filter\Doctrine\DoctrineEmptyCollectionFilterTestFixture', 'property1'), function($item){ return null; });

$this->assertTrue($object->foo instanceof Collection);
$this->assertNotSame($collection, $object->foo);
$this->assertTrue($object->foo->isEmpty());
$this->assertTrue($object->property1 instanceof Collection);
$this->assertNotSame($collection, $object->property1);
$this->assertTrue($object->property1->isEmpty());
}

public function testIntegration()
Expand All @@ -36,16 +37,21 @@ public function testIntegration()
$doctrineEmptyCollectionFixture = new \StdClass();
$originalCollection = new ArrayCollection();
$originalCollection->add(new \StdClass());
$doctrineEmptyCollectionFixture->foo = $originalCollection;
$doctrineEmptyCollectionFixture->property1 = $originalCollection;

//Copy
$deepCopy = new DeepCopy();
$deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher(get_class($doctrineEmptyCollectionFixture), 'foo'));
$deepCopy->addFilter(new DoctrineEmptyCollectionFilter(), new PropertyMatcher(get_class($doctrineEmptyCollectionFixture), 'property1'));
$copied = $deepCopy->copy($doctrineEmptyCollectionFixture);

//Check result
$this->assertTrue($copied->foo instanceof Collection);
$this->assertNotSame($originalCollection, $copied->foo);
$this->assertTrue($copied->foo->isEmpty());
$this->assertTrue($copied->property1 instanceof Collection);
$this->assertNotSame($originalCollection, $copied->property1);
$this->assertTrue($copied->property1->isEmpty());
}
}

class DoctrineEmptyCollectionFilterTestFixture
{
public $property1;
}
9 changes: 5 additions & 4 deletions tests/DeepCopyTest/Filter/KeepFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DeepCopy\DeepCopy;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Matcher\PropertyMatcher;
use ReflectionProperty;

/**
* Test Keep filter
Expand All @@ -13,14 +14,14 @@ class KeepFilterTest extends \PHPUnit_Framework_TestCase
{
public function testApply()
{
$object = new \stdClass();
$object = new KeepFilterTestFixture();
$keepObject = new \stdClass();
$object->foo = $keepObject;
$object->property1 = $keepObject;

$filter = new KeepFilter();
$filter->apply($object, 'foo', null);
$filter->apply($object, new ReflectionProperty('DeepCopyTest\Filter\KeepFilterTestFixture', 'property1'), null);

$this->assertSame($keepObject, $object->foo);
$this->assertSame($keepObject, $object->property1);
}

public function testIntegration()
Expand Down
Loading