Skip to content

Matchers and Filters: use the provided ReflectionProperty #107

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

Merged
merged 2 commits into from
May 31, 2018
Merged
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 @@ -214,10 +214,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
6 changes: 2 additions & 4 deletions src/DeepCopy/Filter/Doctrine/DoctrineCollectionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DeepCopy\Filter\Doctrine;

use DeepCopy\Filter\Filter;
use DeepCopy\Reflection\ReflectionHelper;
use ReflectionProperty;

/**
* @final
Expand All @@ -15,10 +15,8 @@ class DoctrineCollectionFilter implements Filter
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace DeepCopy\Filter\Doctrine;

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

/**
* @final
Expand All @@ -14,13 +14,10 @@ class DoctrineEmptyCollectionFilter implements Filter
/**
* Sets the object property to an empty doctrine collection.
*
* @param object $object
* @param string $property
* @param callable $objectCopier
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue($object, new ArrayCollection());
Expand Down
3 changes: 2 additions & 1 deletion src/DeepCopy/Filter/Doctrine/DoctrineProxyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DeepCopy\Filter\Doctrine;

use DeepCopy\Filter\Filter;
use ReflectionProperty;

/**
* @final
Expand All @@ -15,7 +16,7 @@ class DoctrineProxyFilter implements Filter
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$object->__load();
}
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,6 +2,8 @@

namespace DeepCopy\Filter;

use ReflectionProperty;

/**
* Filter to apply to a property while copying an object
*/
Expand All @@ -10,9 +12,9 @@ interface Filter
/**
* Applies 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,14 +2,16 @@

namespace DeepCopy\Filter;

use ReflectionProperty;

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

namespace DeepCopy\Filter;

use DeepCopy\Reflection\ReflectionHelper;
use ReflectionProperty;

/**
* @final
Expand All @@ -27,9 +27,8 @@ public function __construct(callable $callable)
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);

$value = call_user_func($this->callback, $reflectionProperty->getValue($object));
Expand Down
6 changes: 2 additions & 4 deletions src/DeepCopy/Filter/SetNullFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DeepCopy\Filter;

use DeepCopy\Reflection\ReflectionHelper;
use ReflectionProperty;

/**
* @final
Expand All @@ -14,10 +14,8 @@ class SetNullFilter implements Filter
*
* {@inheritdoc}
*/
public function apply($object, $property, $objectCopier)
public function apply($object, ReflectionProperty $reflectionProperty, $objectCopier)
{
$reflectionProperty = ReflectionHelper::getProperty($object, $property);

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

use DeepCopy\Matcher\Matcher;
use Doctrine\Common\Persistence\Proxy;
use ReflectionProperty;

/**
* @final
Expand All @@ -15,7 +16,7 @@ class DoctrineProxyMatcher implements Matcher
*
* {@inheritdoc}
*/
public function matches($object, $property)
public function matches($object, ReflectionProperty $reflectionProperty)
{
return $object instanceof Proxy;
}
Expand Down
8 changes: 5 additions & 3 deletions src/DeepCopy/Matcher/Matcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace DeepCopy\Matcher;

use ReflectionProperty;

interface Matcher
{
/**
* @param object $object
* @param string $property
* @param object $object
* @param ReflectionProperty $reflectionProperty
*
* @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;

/**
* @final
*/
Expand Down Expand Up @@ -32,8 +34,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;

/**
* @final
*/
Expand All @@ -25,8 +27,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;
}
}
11 changes: 2 additions & 9 deletions src/DeepCopy/Matcher/PropertyTypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace DeepCopy\Matcher;

use DeepCopy\Reflection\ReflectionHelper;
use ReflectionException;
use ReflectionProperty;

/**
* Matches a property by its type.
Expand Down Expand Up @@ -31,14 +30,8 @@ public function __construct($propertyType)
/**
* {@inheritdoc}
*/
public function matches($object, $property)
public function matches($object, ReflectionProperty $reflectionProperty)
{
try {
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
} catch (ReflectionException $exception) {
return false;
}

$reflectionProperty->setAccessible(true);

return $reflectionProperty->getValue($object) instanceof $this->propertyType;
Expand Down
32 changes: 0 additions & 32 deletions src/DeepCopy/Reflection/ReflectionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,4 @@ public static function getProperties(ReflectionClass $ref)

return $propsArr;
}

/**
* Retrieves property by name from object and all its ancestors.
*
* @param object|string $object
* @param string $name
*
* @throws PropertyException
* @throws ReflectionException
*
* @return ReflectionProperty
*/
public static function getProperty($object, $name)
{
$reflection = is_object($object) ? new ReflectionObject($object) : new ReflectionClass($object);

if ($reflection->hasProperty($name)) {
return $reflection->getProperty($name);
}

if ($parentClass = $reflection->getParentClass()) {
return self::getProperty($parentClass->getName(), $name);
}

throw new PropertyException(
sprintf(
'The class "%s" doesn\'t have a property with the given name: "%s".',
is_object($object) ? get_class($object) : $object,
$name
)
);
}
}
16 changes: 16 additions & 0 deletions tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use DeepCopy\f007;
use DeepCopy\f008;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\ReplaceFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\PropertyNameMatcher;
use DeepCopy\Matcher\PropertyTypeMatcher;
Expand Down Expand Up @@ -424,6 +425,21 @@ public function test_matchers_can_access_to_parent_private_properties()
$this->assertNull($copy->getFoo());
}

public function test_private_property_of_parent_object_copy_with_filters_and_matchers()
{
$object = new f001\B();
$object->setAProp(new stdClass());
$object->setBProp(new stdClass());

$deepCopy = new DeepCopy();
$deepCopy->addFilter(new ReplaceFilter(function() {return 'foo';}), new PropertyTypeMatcher(stdClass::class));

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

$this->assertSame('foo', $new->getAProp());
$this->assertSame('foo', $new->getBProp());
}

private function assertEqualButNotSame($expected, $val)
{
$this->assertEquals($expected, $val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use stdClass;

/**
Expand All @@ -15,7 +16,9 @@ class DoctrineCollectionFilterTest extends TestCase
{
public function test_it_copies_the_object_property_array_collection()
{
$object = new stdClass();
$object = new class {
public $foo;
};
$oldCollection = new ArrayCollection();
$oldCollection->add($stdClass = new stdClass());
$object->foo = $oldCollection;
Expand All @@ -24,7 +27,7 @@ public function test_it_copies_the_object_property_array_collection()

$filter->apply(
$object,
'foo',
new ReflectionProperty($object, 'foo'),
function($item) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use stdClass;

/**
Expand All @@ -15,8 +16,9 @@ class DoctrineEmptyCollectionFilterTest extends TestCase
{
public function test_it_sets_the_object_property_to_an_empty_doctrine_collection()
{
$object = new stdClass();

$object = new class {
public $foo;
};
$collection = new ArrayCollection();
$collection->add(new stdClass());

Expand All @@ -26,7 +28,7 @@ public function test_it_sets_the_object_property_to_an_empty_doctrine_collection

$filter->apply(
$object,
'foo',
new ReflectionProperty($object, 'foo'),
function($item) {
return null;
}
Expand Down
Loading