Reflection > getPropertyValue() method > look for the property in parent classes

This commit is contained in:
Meritoo
2019-04-13 18:28:53 +02:00
parent 6f90f5a249
commit eb8fa110ad
3 changed files with 68 additions and 39 deletions

View File

@@ -7,6 +7,7 @@ Common and useful classes, methods, exceptions etc.
1. Travis CI > run many tasks using Phing > update configuration 1. Travis CI > run many tasks using Phing > update configuration
2. Template with placeholders > verification of placeholders without values > make stronger and point out which are 2. Template with placeholders > verification of placeholders without values > make stronger and point out which are
missing missing
3. Reflection > getPropertyValue() method > look for the property in parent classes
# 1.0.2 # 1.0.2

View File

@@ -217,6 +217,27 @@ class Reflection
} catch (\ReflectionException $exception) { } catch (\ReflectionException $exception) {
/* /*
* 2nd try: * 2nd try:
* Look for the property in parent classes
*/
if (null === $reflectionProperty) {
$propertyFound = false;
$reflectionProperties = self::getProperties($object, null, true);
foreach ($reflectionProperties as $reflectionProperty) {
if ($reflectionProperty->getName() === $property) {
$propertyFound = true;
break;
}
}
if ($propertyFound && null !== $reflectionProperty) {
$reflectionProperty->setAccessible(true);
$value = $reflectionProperty->getValue($object);
$reflectionProperty->setAccessible(false);
}
} else {
/*
* 3rd try:
* Look for the get / has / is methods * Look for the get / has / is methods
*/ */
$class = new \ReflectionObject($object); $class = new \ReflectionObject($object);
@@ -255,11 +276,11 @@ class Reflection
} }
} }
if (!$valueFound && null !== $reflectionProperty) { if (!$valueFound) {
/* /*
* Oops, value of the property is still unknown * Oops, value of the property is still unknown
* *
* 3rd try: * 4th try:
* Let's modify accessibility of the property and try again to get value * Let's modify accessibility of the property and try again to get value
*/ */
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
@@ -268,6 +289,7 @@ class Reflection
} }
} }
} }
}
return $value; return $value;
} }

View File

@@ -309,6 +309,12 @@ class ReflectionTest extends BaseTestCase
self::assertEquals($username, Reflection::getPropertyValue($f, 'username')); self::assertEquals($username, Reflection::getPropertyValue($f, 'username'));
} }
public function testGetPropertyValueFromParentClass(): void
{
$c = new C();
self::assertEquals(1, Reflection::getPropertyValue($c, 'count', true));
}
public function testGetPropertyValuesFromEmptySource() public function testGetPropertyValuesFromEmptySource()
{ {
self::assertEquals([], Reflection::getPropertyValues([], 'something')); self::assertEquals([], Reflection::getPropertyValues([], 'something'));