@@ -25,25 +25,28 @@ public function __construct(
25
25
*/
26
26
public function inheritProperty (string $ name , bool $ returnIfExists = false ): Property
27
27
{
28
- $ extends = $ this ->class ->getExtends ();
29
28
if ($ this ->class ->hasProperty ($ name )) {
30
29
return $ returnIfExists
31
30
? $ this ->class ->getProperty ($ name )
32
31
: throw new Nette \InvalidStateException ("Cannot inherit property ' $ name', because it already exists. " );
33
-
34
- } elseif (!$ extends ) {
35
- throw new Nette \InvalidStateException ("Class ' {$ this ->class ->getName ()}' has not setExtends() set. " );
36
32
}
37
33
38
- try {
39
- $ rp = new \ReflectionProperty ($ extends , $ name );
40
- } catch (\ReflectionException ) {
41
- throw new Nette \InvalidStateException ("Property ' $ name' has not been found in ancestor {$ extends }" );
34
+ $ parents = [...(array ) $ this ->class ->getExtends (), ...$ this ->class ->getImplements ()]
35
+ ?: throw new Nette \InvalidStateException ("Class ' {$ this ->class ->getName ()}' has neither setExtends() nor setImplements() set. " );
36
+
37
+ foreach ($ parents as $ parent ) {
38
+ try {
39
+ $ rp = new \ReflectionProperty ($ parent , $ name );
40
+ } catch (\ReflectionException ) {
41
+ continue ;
42
+ }
43
+ $ property = (new Factory )->fromPropertyReflection ($ rp );
44
+ $ this ->class ->addMember ($ property );
45
+ $ property ->setHooks ([]);
46
+ return $ property ;
42
47
}
43
48
44
- $ property = (new Factory )->fromPropertyReflection ($ rp );
45
- $ this ->class ->addMember ($ property );
46
- return $ property ;
49
+ throw new Nette \InvalidStateException ("Property ' $ name' has not been found in any ancestor: " . implode (', ' , $ parents ));
47
50
}
48
51
49
52
@@ -52,16 +55,15 @@ public function inheritProperty(string $name, bool $returnIfExists = false): Pro
52
55
*/
53
56
public function inheritMethod (string $ name , bool $ returnIfExists = false ): Method
54
57
{
55
- $ parents = [...(array ) $ this ->class ->getExtends (), ...$ this ->class ->getImplements ()];
56
58
if ($ this ->class ->hasMethod ($ name )) {
57
59
return $ returnIfExists
58
60
? $ this ->class ->getMethod ($ name )
59
61
: throw new Nette \InvalidStateException ("Cannot inherit method ' $ name', because it already exists. " );
60
-
61
- } elseif (!$ parents ) {
62
- throw new Nette \InvalidStateException ("Class ' {$ this ->class ->getName ()}' has neither setExtends() nor setImplements() set. " );
63
62
}
64
63
64
+ $ parents = [...(array ) $ this ->class ->getExtends (), ...$ this ->class ->getImplements ()]
65
+ ?: throw new Nette \InvalidStateException ("Class ' {$ this ->class ->getName ()}' has neither setExtends() nor setImplements() set. " );
66
+
65
67
foreach ($ parents as $ parent ) {
66
68
try {
67
69
$ rm = new \ReflectionMethod ($ parent , $ name );
@@ -91,5 +93,8 @@ public function implementInterface(string $interfaceName): void
91
93
foreach ($ interface ->getMethods () as $ method ) {
92
94
$ this ->inheritMethod ($ method ->getName (), returnIfExists: true );
93
95
}
96
+ foreach ($ interface ->getProperties () as $ property ) {
97
+ $ this ->inheritProperty ($ property ->getName (), returnIfExists: true );
98
+ }
94
99
}
95
100
}
0 commit comments