@@ -37,33 +37,45 @@ public function fromClassReflection(
3737 throw new Nette \NotSupportedException ('The $withBodies parameter cannot be used for anonymous or internal classes or interfaces. ' );
3838 }
3939
40- $ enumIface = null ;
40+ $ class = $ this ->createClassObject ($ from );
41+ $ this ->setupInheritance ($ class , $ from );
42+ $ this ->populateMembers ($ class , $ from , $ withBodies );
43+ return $ class ;
44+ }
45+
46+
47+ private function createClassObject (\ReflectionClass &$ from ): ClassLike
48+ {
4149 if ($ from ->isEnum ()) {
42- $ class = new EnumType ($ from ->getName ());
4350 $ from = new \ReflectionEnum ($ from ->getName ());
44- $ enumIface = $ from ->isBacked () ? \BackedEnum::class : \UnitEnum::class ;
51+ return new EnumType ( $ from ->getName ()) ;
4552 } elseif ($ from ->isAnonymous ()) {
46- $ class = new ClassType ;
53+ return new ClassType ;
4754 } elseif ($ from ->isInterface ()) {
48- $ class = new InterfaceType ($ from ->getName ());
55+ return new InterfaceType ($ from ->getName ());
4956 } elseif ($ from ->isTrait ()) {
50- $ class = new TraitType ($ from ->getName ());
57+ return new TraitType ($ from ->getName ());
5158 } else {
5259 $ class = new ClassType ($ from ->getName ());
5360 $ class ->setFinal ($ from ->isFinal () && $ class ->isClass ());
5461 $ class ->setAbstract ($ from ->isAbstract () && $ class ->isClass ());
5562 $ class ->setReadOnly (PHP_VERSION_ID >= 80200 && $ from ->isReadOnly ());
63+ return $ class ;
5664 }
65+ }
66+
5767
68+ private function setupInheritance (ClassLike $ class , \ReflectionClass $ from ): void
69+ {
5870 $ ifaces = $ from ->getInterfaceNames ();
5971 foreach ($ ifaces as $ iface ) {
6072 $ ifaces = array_filter ($ ifaces , fn (string $ item ): bool => !is_subclass_of ($ iface , $ item ));
6173 }
74+ $ ifaces = array_diff ($ ifaces , [\BackedEnum::class, \UnitEnum::class]);
6275
6376 if ($ from ->isInterface ()) {
6477 $ class ->setExtends ($ ifaces );
6578 } elseif ($ ifaces ) {
66- $ ifaces = array_diff ($ ifaces , [$ enumIface ]);
6779 $ class ->setImplements ($ ifaces );
6880 }
6981
@@ -73,7 +85,12 @@ public function fromClassReflection(
7385 $ class ->setExtends ($ from ->getParentClass ()->name );
7486 $ class ->setImplements (array_diff ($ class ->getImplements (), $ from ->getParentClass ()->getInterfaceNames ()));
7587 }
88+ }
89+
7690
91+ private function populateMembers (ClassLike $ class , \ReflectionClass $ from , bool $ withBodies ): void
92+ {
93+ // Properties
7794 $ props = [];
7895 foreach ($ from ->getProperties () as $ prop ) {
7996 $ declaringClass = Reflection::getPropertyDeclaringClass ($ prop );
@@ -97,14 +114,15 @@ public function fromClassReflection(
97114 $ class ->setProperties ($ props );
98115 }
99116
117+ // Methods and trait resolutions
100118 $ methods = $ resolutions = [];
101119 foreach ($ from ->getMethods () as $ method ) {
102120 $ declaringMethod = Reflection::getMethodDeclaringMethod ($ method );
103121 $ declaringClass = $ declaringMethod ->getDeclaringClass ();
104122
105123 if (
106124 $ declaringClass ->name === $ from ->name
107- && (!$ enumIface || !method_exists ($ enumIface , $ method ->name ))
125+ && (!$ from -> isEnum () || !method_exists ($ from -> isBacked () ? \BackedEnum::class : \UnitEnum::class , $ method ->name ))
108126 ) {
109127 $ methods [] = $ m = $ this ->fromMethodReflection ($ method );
110128 if ($ withBodies ) {
@@ -127,6 +145,7 @@ public function fromClassReflection(
127145
128146 $ class ->setMethods ($ methods );
129147
148+ // Traits
130149 foreach ($ from ->getTraitNames () as $ trait ) {
131150 $ trait = $ class ->addTrait ($ trait );
132151 foreach ($ resolutions as $ resolution ) {
@@ -135,6 +154,7 @@ public function fromClassReflection(
135154 $ resolutions = [];
136155 }
137156
157+ // Constants and enum cases
138158 $ consts = $ cases = [];
139159 foreach ($ from ->getReflectionConstants () as $ const ) {
140160 if ($ class ->isEnum () && $ from ->hasCase ($ const ->name )) {
@@ -150,8 +170,6 @@ public function fromClassReflection(
150170 if ($ cases ) {
151171 $ class ->setCases ($ cases );
152172 }
153-
154- return $ class ;
155173 }
156174
157175
0 commit comments