@@ -37,24 +37,38 @@ 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 ;
41- if ($ from ->isEnum ()) {
42- $ class = new EnumType ($ from ->getShortName (), new PhpNamespace ($ from ->getNamespaceName ()));
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+ {
49+ if ($ from ->isAnonymous ()) {
50+ return new ClassType ;
51+ } elseif ($ from ->isEnum ()) {
4352 $ from = new \ReflectionEnum ($ from ->getName ());
44- $ enumIface = $ from ->isBacked () ? \BackedEnum::class : \UnitEnum::class;
45- } elseif ($ from ->isAnonymous ()) {
46- $ class = new ClassType ;
53+ $ class = new EnumType ($ from ->getName ());
4754 } elseif ($ from ->isInterface ()) {
48- $ class = new InterfaceType ($ from ->getShortName (), new PhpNamespace ( $ from -> getNamespaceName () ));
55+ $ class = new InterfaceType ($ from ->getName ( ));
4956 } elseif ($ from ->isTrait ()) {
50- $ class = new TraitType ($ from ->getShortName (), new PhpNamespace ( $ from -> getNamespaceName () ));
57+ $ class = new TraitType ($ from ->getName ( ));
5158 } else {
52- $ class = new ClassType ($ from ->getShortName (), new PhpNamespace ( $ from -> getNamespaceName ()) );
59+ $ class = new ClassType ($ from ->getShortName ());
5360 $ class ->setFinal ($ from ->isFinal () && $ class ->isClass ());
5461 $ class ->setAbstract ($ from ->isAbstract () && $ class ->isClass ());
5562 $ class ->setReadOnly (PHP_VERSION_ID >= 80200 && $ from ->isReadOnly ());
5663 }
5764
65+ $ class ->setNamespace (new PhpNamespace ($ from ->getNamespaceName ()));
66+ return $ class ;
67+ }
68+
69+
70+ private function setupInheritance (ClassLike $ class , \ReflectionClass $ from ): void
71+ {
5872 $ ifaces = $ from ->getInterfaceNames ();
5973 foreach ($ ifaces as $ iface ) {
6074 $ ifaces = array_filter ($ ifaces , fn (string $ item ): bool => !is_subclass_of ($ iface , $ item ));
@@ -63,7 +77,7 @@ public function fromClassReflection(
6377 if ($ from ->isInterface ()) {
6478 $ class ->setExtends ($ ifaces );
6579 } elseif ($ ifaces ) {
66- $ ifaces = array_diff ($ ifaces , [$ enumIface ]);
80+ $ ifaces = array_diff ($ ifaces , [\BackedEnum::class, \UnitEnum::class ]);
6781 $ class ->setImplements ($ ifaces );
6882 }
6983
@@ -73,7 +87,12 @@ public function fromClassReflection(
7387 $ class ->setExtends ($ from ->getParentClass ()->name );
7488 $ class ->setImplements (array_diff ($ class ->getImplements (), $ from ->getParentClass ()->getInterfaceNames ()));
7589 }
90+ }
91+
7692
93+ private function populateMembers (ClassLike $ class , \ReflectionClass $ from , bool $ withBodies ): void
94+ {
95+ // Properties
7796 $ props = [];
7897 foreach ($ from ->getProperties () as $ prop ) {
7998 $ declaringClass = Reflection::getPropertyDeclaringClass ($ prop );
@@ -97,14 +116,15 @@ public function fromClassReflection(
97116 $ class ->setProperties ($ props );
98117 }
99118
119+ // Methods and trait resolutions
100120 $ methods = $ resolutions = [];
101121 foreach ($ from ->getMethods () as $ method ) {
102122 $ declaringMethod = Reflection::getMethodDeclaringMethod ($ method );
103123 $ declaringClass = $ declaringMethod ->getDeclaringClass ();
104124
105125 if (
106126 $ declaringClass ->name === $ from ->name
107- && (!$ enumIface || !method_exists ($ enumIface , $ method ->name ))
127+ && (!$ from -> isEnum () || !method_exists ($ from -> isBacked () ? \BackedEnum::class : \UnitEnum::class , $ method ->name ))
108128 ) {
109129 $ methods [] = $ m = $ this ->fromMethodReflection ($ method );
110130 if ($ withBodies ) {
@@ -127,6 +147,7 @@ public function fromClassReflection(
127147
128148 $ class ->setMethods ($ methods );
129149
150+ // Traits
130151 foreach ($ from ->getTraitNames () as $ trait ) {
131152 $ trait = $ class ->addTrait ($ trait );
132153 foreach ($ resolutions as $ resolution ) {
@@ -135,6 +156,7 @@ public function fromClassReflection(
135156 $ resolutions = [];
136157 }
137158
159+ // Constants and enum cases
138160 $ consts = $ cases = [];
139161 foreach ($ from ->getReflectionConstants () as $ const ) {
140162 if ($ class ->isEnum () && $ from ->hasCase ($ const ->name )) {
@@ -150,8 +172,6 @@ public function fromClassReflection(
150172 if ($ cases ) {
151173 $ class ->setCases ($ cases );
152174 }
153-
154- return $ class ;
155175 }
156176
157177
0 commit comments