mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
Reflection > getPropertyValue() method > look for the property in parent classes
This commit is contained in:
@@ -7,6 +7,7 @@ Common and useful classes, methods, exceptions etc.
|
||||
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
|
||||
missing
|
||||
3. Reflection > getPropertyValue() method > look for the property in parent classes
|
||||
|
||||
# 1.0.2
|
||||
|
||||
|
||||
@@ -217,54 +217,76 @@ class Reflection
|
||||
} catch (\ReflectionException $exception) {
|
||||
/*
|
||||
* 2nd try:
|
||||
* Look for the get / has / is methods
|
||||
* Look for the property in parent classes
|
||||
*/
|
||||
$class = new \ReflectionObject($object);
|
||||
$valueFound = false;
|
||||
|
||||
if ($force || $class->hasProperty($property)) {
|
||||
$property = Inflector::classify($property);
|
||||
|
||||
$getterPrefixes = [
|
||||
'get',
|
||||
'has',
|
||||
'is',
|
||||
];
|
||||
|
||||
foreach ($getterPrefixes as $prefix) {
|
||||
$getterName = sprintf('%s%s', $prefix, $property);
|
||||
|
||||
if ($class->hasMethod($getterName)) {
|
||||
$method = new \ReflectionMethod($object, $getterName);
|
||||
|
||||
/*
|
||||
* Getter is not accessible publicly?
|
||||
* I have to skip it, to avoid an error like this:
|
||||
*
|
||||
* Call to protected method My\ExtraClass::getExtraProperty() from context 'My\ExtraClass'
|
||||
*/
|
||||
if ($method->isProtected() || $method->isPrivate()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $object->{$getterName}();
|
||||
$valueFound = true;
|
||||
if (null === $reflectionProperty) {
|
||||
$propertyFound = false;
|
||||
$reflectionProperties = self::getProperties($object, null, true);
|
||||
|
||||
foreach ($reflectionProperties as $reflectionProperty) {
|
||||
if ($reflectionProperty->getName() === $property) {
|
||||
$propertyFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$valueFound && null !== $reflectionProperty) {
|
||||
if ($propertyFound && null !== $reflectionProperty) {
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
$reflectionProperty->setAccessible(false);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* Oops, value of the property is still unknown
|
||||
*
|
||||
* 3rd try:
|
||||
* Let's modify accessibility of the property and try again to get value
|
||||
* Look for the get / has / is methods
|
||||
*/
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
$reflectionProperty->setAccessible(false);
|
||||
$class = new \ReflectionObject($object);
|
||||
$valueFound = false;
|
||||
|
||||
if ($force || $class->hasProperty($property)) {
|
||||
$property = Inflector::classify($property);
|
||||
|
||||
$getterPrefixes = [
|
||||
'get',
|
||||
'has',
|
||||
'is',
|
||||
];
|
||||
|
||||
foreach ($getterPrefixes as $prefix) {
|
||||
$getterName = sprintf('%s%s', $prefix, $property);
|
||||
|
||||
if ($class->hasMethod($getterName)) {
|
||||
$method = new \ReflectionMethod($object, $getterName);
|
||||
|
||||
/*
|
||||
* Getter is not accessible publicly?
|
||||
* I have to skip it, to avoid an error like this:
|
||||
*
|
||||
* Call to protected method My\ExtraClass::getExtraProperty() from context 'My\ExtraClass'
|
||||
*/
|
||||
if ($method->isProtected() || $method->isPrivate()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = $object->{$getterName}();
|
||||
$valueFound = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$valueFound) {
|
||||
/*
|
||||
* Oops, value of the property is still unknown
|
||||
*
|
||||
* 4th try:
|
||||
* Let's modify accessibility of the property and try again to get value
|
||||
*/
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
$reflectionProperty->setAccessible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,6 +309,12 @@ class ReflectionTest extends BaseTestCase
|
||||
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()
|
||||
{
|
||||
self::assertEquals([], Reflection::getPropertyValues([], 'something'));
|
||||
|
||||
Reference in New Issue
Block a user