File tree 1 file changed +24
-1
lines changed
1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Enqueue \Test ;
4
4
5
+ use ReflectionProperty ;
6
+
5
7
trait ReadAttributeTrait
6
8
{
7
9
public function readAttribute (object $ object , string $ attribute )
8
10
{
9
- $ refProperty = new \ ReflectionProperty ( get_class ( $ object) , $ attribute );
11
+ $ refProperty = $ this -> getClassAttribute ( $ object , $ attribute );
10
12
$ refProperty ->setAccessible (true );
11
13
$ value = $ refProperty ->getValue ($ object );
12
14
$ refProperty ->setAccessible (false );
13
15
14
16
return $ value ;
15
17
}
16
18
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
+
17
40
private function assertAttributeSame ($ expected , string $ attribute , object $ object ): void
18
41
{
19
42
static ::assertSame ($ expected , $ this ->readAttribute ($ object , $ attribute ));
You can’t perform that action at this time.
0 commit comments