Skip to content

Fix access to parent properties in matchers #88

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 1 commit into from
Oct 16, 2017
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
18 changes: 18 additions & 0 deletions fixtures/f008/A.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace DeepCopy\f008;

class A
{
private $foo;

public function __construct($foo)
{
$this->foo = $foo;
}

public function getFoo()
{
return $this->foo;
}
}
7 changes: 7 additions & 0 deletions fixtures/f008/B.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace DeepCopy\f008;

class B extends A
{
}
9 changes: 9 additions & 0 deletions src/DeepCopy/Exception/PropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace DeepCopy\Exception;

use ReflectionException;

class PropertyException extends ReflectionException
{
}
4 changes: 2 additions & 2 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 ReflectionProperty;
use DeepCopy\Reflection\ReflectionHelper;

/**
* @final
Expand All @@ -17,7 +17,7 @@ class DoctrineCollectionFilter implements Filter
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = new ReflectionProperty($object, $property);
$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 @@ -20,7 +20,7 @@ class DoctrineEmptyCollectionFilter implements Filter
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = new ReflectionProperty($object, $property);
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);

$reflectionProperty->setValue($object, new ArrayCollection());
Expand Down
4 changes: 2 additions & 2 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 ReflectionProperty;
use DeepCopy\Reflection\ReflectionHelper;

/**
* @final
Expand All @@ -29,7 +29,7 @@ public function __construct(callable $callable)
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = new ReflectionProperty($object, $property);
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
$reflectionProperty->setAccessible(true);

$value = call_user_func($this->callback, $reflectionProperty->getValue($object));
Expand Down
4 changes: 2 additions & 2 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 ReflectionProperty;
use DeepCopy\Reflection\ReflectionHelper;

/**
* @final
Expand All @@ -16,7 +16,7 @@ class SetNullFilter implements Filter
*/
public function apply($object, $property, $objectCopier)
{
$reflectionProperty = new ReflectionProperty($object, $property);
$reflectionProperty = ReflectionHelper::getProperty($object, $property);

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

namespace DeepCopy\Matcher;

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

/**
* Matches a property by its type.
Expand Down Expand Up @@ -34,7 +34,7 @@ public function __construct($propertyType)
public function matches($object, $property)
{
try {
$reflectionProperty = new ReflectionProperty($object, $property);
$reflectionProperty = ReflectionHelper::getProperty($object, $property);
} catch (ReflectionException $exception) {
return false;
}
Expand Down
35 changes: 35 additions & 0 deletions src/DeepCopy/Reflection/ReflectionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace DeepCopy\Reflection;

use DeepCopy\Exception\PropertyException;
use ReflectionClass;
use ReflectionException;
use ReflectionObject;
use ReflectionProperty;

class ReflectionHelper
Expand Down Expand Up @@ -40,4 +43,36 @@ 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
)
);
}
}
20 changes: 19 additions & 1 deletion tests/DeepCopyTest/DeepCopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use DeepCopy\f004;
use DeepCopy\f005;
use DeepCopy\f006;
use DeepCopy\f008;
use DeepCopy\Filter\KeepFilter;
use DeepCopy\Filter\SetNullFilter;
use DeepCopy\Matcher\PropertyNameMatcher;
use DeepCopy\Matcher\PropertyTypeMatcher;
use DeepCopy\TypeFilter\ShallowCopyFilter;
use DeepCopy\TypeMatcher\TypeMatcher;
use PHPUnit_Framework_TestCase;
Expand Down Expand Up @@ -118,7 +120,7 @@ public function test_it_can_copy_an_object_with_an_object_property()
$this->assertEqualButNotSame($foo->bar, $copy->bar);
}

public function test_dynamic_properties_are_copied()
public function test_it_copies_dynamic_properties()
{
$foo = new stdClass();
$bar = new stdClass();
Expand Down Expand Up @@ -375,6 +377,22 @@ public function test_it_can_copy_a_SplDoublyLinkedList()
$this->assertEqualButNotSame($b, $aCopy->b);
}

/**
* @ticket https://github.com/myclabs/DeepCopy/issues/62
*/
public function test_matchers_can_access_to_parent_private_properties()
{
$deepCopy = new DeepCopy();
$deepCopy->addFilter(new SetNullFilter(), new PropertyTypeMatcher(stdClass::class));

$object = new f008\B(new stdClass());

/** @var f008\B $copy */
$copy = $deepCopy->copy($object);

$this->assertNull($copy->getFoo());
}

private function assertEqualButNotSame($expected, $val)
{
$this->assertEquals($expected, $val);
Expand Down
34 changes: 34 additions & 0 deletions tests/DeepCopyTest/Reflection/ReflectionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DeepCopy\Reflection\ReflectionHelper;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
use ReflectionProperty;

/**
* @covers \DeepCopy\Reflection\ReflectionHelper
Expand Down Expand Up @@ -35,6 +36,39 @@ public function test_it_retrieves_the_object_properties()

$this->assertSame($expectedProps, array_keys($actualProps));
}

/**
* @dataProvider provideProperties
*/
public function test_it_can_retrieve_an_object_property($name)
{
$object = new ReflectionHelperTestChild();

$property = ReflectionHelper::getProperty($object, $name);

$this->assertInstanceOf(ReflectionProperty::class, $property);

$this->assertSame($name, $property->getName());
}

public function provideProperties()
{
return [
'public property' => ['a10'],
'private property' => ['a102'],
'private property of ancestor' => ['a3']
];
}

/**
* @expectedException \DeepCopy\Exception\PropertyException
*/
public function test_it_cannot_retrieve_a_non_existent_prperty()
{
$object = new ReflectionHelperTestChild();

ReflectionHelper::getProperty($object, 'non existent property');
}
}

class ReflectionHelperTestParent
Expand Down