Skip to content

Commit 443fd77

Browse files
committed
Fix reading parent attribute
1 parent 9d34085 commit 443fd77

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

pkg/test/ReadAttributeTrait.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,41 @@
22

33
namespace Enqueue\Test;
44

5+
use ReflectionProperty;
6+
57
trait ReadAttributeTrait
68
{
79
public function readAttribute(object $object, string $attribute)
810
{
9-
$refProperty = new \ReflectionProperty(get_class($object), $attribute);
11+
$refProperty = $this->getClassAttribute($object, $attribute);
1012
$refProperty->setAccessible(true);
1113
$value = $refProperty->getValue($object);
1214
$refProperty->setAccessible(false);
1315

1416
return $value;
1517
}
1618

19+
private function getClassAttribute(
20+
object $object,
21+
string $attribute,
22+
?string $class = null
23+
): ReflectionProperty {
24+
if ($class === null) {
25+
$class = get_class($object);
26+
}
27+
28+
try {
29+
return new ReflectionProperty($class, $attribute);
30+
} catch (\ReflectionException $exception) {
31+
$parentClass = get_parent_class($object);
32+
if ($parentClass === false) {
33+
throw $exception;
34+
}
35+
36+
return $this->getClassAttribute($object, $attribute, $parentClass);
37+
}
38+
}
39+
1740
private function assertAttributeSame($expected, string $attribute, object $object): void
1841
{
1942
static::assertSame($expected, $this->readAttribute($object, $attribute));

0 commit comments

Comments
 (0)