@@ -25,25 +25,28 @@ public function __construct(
2525 */
2626 public function inheritProperty (string $ name , bool $ returnIfExists = false ): Property
2727 {
28- $ extends = $ this ->class ->getExtends ();
2928 if ($ this ->class ->hasProperty ($ name )) {
3029 return $ returnIfExists
3130 ? $ this ->class ->getProperty ($ name )
3231 : 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. " );
3632 }
3733
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 ;
4247 }
4348
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 ));
4750 }
4851
4952
@@ -52,16 +55,15 @@ public function inheritProperty(string $name, bool $returnIfExists = false): Pro
5255 */
5356 public function inheritMethod (string $ name , bool $ returnIfExists = false ): Method
5457 {
55- $ parents = [...(array ) $ this ->class ->getExtends (), ...$ this ->class ->getImplements ()];
5658 if ($ this ->class ->hasMethod ($ name )) {
5759 return $ returnIfExists
5860 ? $ this ->class ->getMethod ($ name )
5961 : 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. " );
6362 }
6463
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+
6567 foreach ($ parents as $ parent ) {
6668 try {
6769 $ rm = new \ReflectionMethod ($ parent , $ name );
@@ -91,5 +93,8 @@ public function implementInterface(string $interfaceName): void
9193 foreach ($ interface ->getMethods () as $ method ) {
9294 $ this ->inheritMethod ($ method ->getName (), returnIfExists: true );
9395 }
96+ foreach ($ interface ->getProperties () as $ property ) {
97+ $ this ->inheritProperty ($ property ->getName (), returnIfExists: true );
98+ }
9499 }
95100}
0 commit comments