@@ -25,25 +25,25 @@ 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
+ return $ this ->implementProperty ($ rp );
42
44
}
43
45
44
- $ property = (new Factory )->fromPropertyReflection ($ rp );
45
- $ this ->class ->addMember ($ property );
46
- return $ property ;
46
+ throw new Nette \InvalidStateException ("Property ' $ name' has not been found in any ancestor: " . implode (', ' , $ parents ));
47
47
}
48
48
49
49
@@ -52,16 +52,15 @@ public function inheritProperty(string $name, bool $returnIfExists = false): Pro
52
52
*/
53
53
public function inheritMethod (string $ name , bool $ returnIfExists = false ): Method
54
54
{
55
- $ parents = [...(array ) $ this ->class ->getExtends (), ...$ this ->class ->getImplements ()];
56
55
if ($ this ->class ->hasMethod ($ name )) {
57
56
return $ returnIfExists
58
57
? $ this ->class ->getMethod ($ name )
59
58
: 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
59
}
64
60
61
+ $ parents = [...(array ) $ this ->class ->getExtends (), ...$ this ->class ->getImplements ()]
62
+ ?: throw new Nette \InvalidStateException ("Class ' {$ this ->class ->getName ()}' has neither setExtends() nor setImplements() set. " );
63
+
65
64
foreach ($ parents as $ parent ) {
66
65
try {
67
66
$ rm = new \ReflectionMethod ($ parent , $ name );
@@ -94,6 +93,14 @@ public function implement(string $name): void
94
93
$ this ->implementMethod ($ method );
95
94
}
96
95
}
96
+
97
+ if (PHP_VERSION_ID >= 80400 ) {
98
+ foreach ($ definition ->getProperties () as $ property ) {
99
+ if (!$ this ->class ->hasProperty ($ property ->getName ()) && $ property ->isAbstract ()) {
100
+ $ this ->implementProperty ($ property );
101
+ }
102
+ }
103
+ }
97
104
}
98
105
99
106
@@ -106,6 +113,15 @@ private function implementMethod(\ReflectionMethod $rm): Method
106
113
}
107
114
108
115
116
+ private function implementProperty (\ReflectionProperty $ rp ): Property
117
+ {
118
+ $ property = (new Factory )->fromPropertyReflection ($ rp );
119
+ $ property ->setHooks ([]);
120
+ $ this ->class ->addMember ($ property );
121
+ return $ property ;
122
+ }
123
+
124
+
109
125
/** @deprecated use implement() */
110
126
public function implementInterface (string $ interfaceName ): void
111
127
{
0 commit comments