@@ -37,11 +37,18 @@ 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+ $ from = $ from ->isEnum () ? new \ReflectionEnum ($ from ->getName ()) : $ from ;
41+ $ class = $ this ->createClassObject ($ from );
42+ $ this ->setupInheritance ($ class , $ from );
43+ $ this ->populateMembers ($ class , $ from , $ withBodies );
44+ return $ class ;
45+ }
46+
47+
48+ private function createClassObject (\ReflectionClass $ from ): ClassLike
49+ {
4150 if ($ from ->isEnum ()) {
4251 $ class = new EnumType ($ from ->getShortName ());
43- $ from = new \ReflectionEnum ($ from ->getName ());
44- $ enumIface = $ from ->isBacked () ? \BackedEnum::class : \UnitEnum::class;
4552 } elseif ($ from ->isAnonymous ()) {
4653 $ class = new ClassType ;
4754 } elseif ($ from ->isInterface ()) {
@@ -57,16 +64,21 @@ public function fromClassReflection(
5764 if (!$ from ->isAnonymous ()) {
5865 (new PhpNamespace ($ from ->getNamespaceName ()))->add ($ class );
5966 }
67+ return $ class ;
68+ }
6069
70+
71+ private function setupInheritance (ClassLike $ class , \ReflectionClass $ from ): void
72+ {
6173 $ ifaces = $ from ->getInterfaceNames ();
6274 foreach ($ ifaces as $ iface ) {
6375 $ ifaces = array_filter ($ ifaces , fn (string $ item ): bool => !is_subclass_of ($ iface , $ item ));
6476 }
77+ $ ifaces = array_diff ($ ifaces , [\BackedEnum::class, \UnitEnum::class]);
6578
6679 if ($ from ->isInterface ()) {
6780 $ class ->setExtends ($ ifaces );
6881 } elseif ($ ifaces ) {
69- $ ifaces = array_diff ($ ifaces , [$ enumIface ]);
7082 $ class ->setImplements ($ ifaces );
7183 }
7284
@@ -76,7 +88,12 @@ public function fromClassReflection(
7688 $ class ->setExtends ($ from ->getParentClass ()->name );
7789 $ class ->setImplements (array_diff ($ class ->getImplements (), $ from ->getParentClass ()->getInterfaceNames ()));
7890 }
91+ }
92+
7993
94+ private function populateMembers (ClassLike $ class , \ReflectionClass $ from , bool $ withBodies ): void
95+ {
96+ // Properties
8097 $ props = [];
8198 foreach ($ from ->getProperties () as $ prop ) {
8299 $ declaringClass = Reflection::getPropertyDeclaringClass ($ prop );
@@ -100,14 +117,15 @@ public function fromClassReflection(
100117 $ class ->setProperties ($ props );
101118 }
102119
120+ // Methods and trait resolutions
103121 $ methods = $ resolutions = [];
104122 foreach ($ from ->getMethods () as $ method ) {
105123 $ declaringMethod = Reflection::getMethodDeclaringMethod ($ method );
106124 $ declaringClass = $ declaringMethod ->getDeclaringClass ();
107125
108126 if (
109127 $ declaringClass ->name === $ from ->name
110- && (!$ enumIface || !method_exists ($ enumIface , $ method ->name ))
128+ && (!$ from -> isEnum () || !method_exists ($ from -> isBacked () ? \BackedEnum::class : \UnitEnum::class , $ method ->name ))
111129 ) {
112130 $ methods [] = $ m = $ this ->fromMethodReflection ($ method );
113131 if ($ withBodies ) {
@@ -130,6 +148,7 @@ public function fromClassReflection(
130148
131149 $ class ->setMethods ($ methods );
132150
151+ // Traits
133152 foreach ($ from ->getTraitNames () as $ trait ) {
134153 $ trait = $ class ->addTrait ($ trait );
135154 foreach ($ resolutions as $ resolution ) {
@@ -138,6 +157,7 @@ public function fromClassReflection(
138157 $ resolutions = [];
139158 }
140159
160+ // Constants and enum cases
141161 $ consts = $ cases = [];
142162 foreach ($ from ->getReflectionConstants () as $ const ) {
143163 if ($ class ->isEnum () && $ from ->hasCase ($ const ->name )) {
@@ -153,8 +173,6 @@ public function fromClassReflection(
153173 if ($ cases ) {
154174 $ class ->setCases ($ cases );
155175 }
156-
157- return $ class ;
158176 }
159177
160178
0 commit comments