File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33namespace Enqueue \Test ;
44
5+ use ReflectionProperty ;
6+
57trait 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 ));
You can’t perform that action at this time.
0 commit comments