Conditions | 6 |
Paths | 4 |
Total Lines | 26 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
33 | public static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty |
||
34 | { |
||
35 | $reflectionObject = new ReflectionObject($object); |
||
36 | |||
37 | $reflectionProperty = null; |
||
38 | |||
39 | if ($reflectionObject->hasProperty($propertyName)) { |
||
40 | $reflectionProperty = $reflectionObject->getProperty($propertyName); |
||
41 | } else { |
||
42 | |||
43 | // This is needed for private parent properties only. |
||
44 | $parent = $reflectionObject->getParentClass(); |
||
45 | while ($reflectionProperty === null && $parent !== false) { |
||
46 | if ($parent->hasProperty($propertyName)) { |
||
47 | $reflectionProperty = $parent->getProperty($propertyName); |
||
48 | } |
||
49 | |||
50 | $parent = $parent->getParentClass(); |
||
51 | } |
||
52 | } |
||
53 | |||
54 | if (!$reflectionProperty) { |
||
55 | throw new ReflectionException("Property not found: " . $propertyName); |
||
56 | } |
||
57 | |||
58 | return $reflectionProperty; |
||
59 | } |
||
61 |