55namespace AsyncAws \CodeGenerator \Generator \PhpGenerator ;
66
77use Nette \PhpGenerator \ClassType ;
8- use Nette \PhpGenerator \Closure ;
9- use Nette \PhpGenerator \GlobalFunction ;
10- use Nette \PhpGenerator \Helpers ;
11- use Nette \PhpGenerator \Literal ;
12- use Nette \PhpGenerator \Method ;
13- use Nette \PhpGenerator \Parameter ;
8+ use Nette \PhpGenerator \Factory ;
149use Nette \PhpGenerator \PhpNamespace ;
15- use Nette \PhpGenerator \Property ;
1610
1711/**
1812 * Generate Nette PhpNamespace from existing source class.
2519 */
2620class ClassFactory
2721{
22+ /**
23+ * @var Factory
24+ */
25+ private $ factory ;
26+
27+ public function __construct ()
28+ {
29+ $ this ->factory = new Factory ();
30+ }
31+
2832 /**
2933 * @param \ReflectionClass<object> $from
3034 */
3135 public function fromClassReflection (\ReflectionClass $ from ): PhpNamespace
3236 {
37+ /** @var ClassType $class */
38+ $ class = $ this ->factory ->fromClassReflection ($ from , true );
39+
3340 $ namespace = new PhpNamespace ($ from ->getNamespaceName ());
3441 $ filename = $ from ->getFileName ();
3542 $ rows = file ($ filename );
@@ -46,136 +53,8 @@ public function fromClassReflection(\ReflectionClass $from): PhpNamespace
4653 }
4754 }
4855
49- $ class = $ from ->isAnonymous ()
50- ? new ClassType ()
51- : new ClassType ($ from ->getShortName (), $ namespace );
52-
5356 $ namespace ->add ($ class );
54- $ class ->setType ($ from ->isInterface () ? $ class ::TYPE_INTERFACE : ($ from ->isTrait () ? $ class ::TYPE_TRAIT : $ class ::TYPE_CLASS ));
55- $ class ->setFinal ($ from ->isFinal () && $ class ->isClass ());
56- $ class ->setAbstract ($ from ->isAbstract () && $ class ->isClass ());
57-
58- $ ifaces = $ from ->getInterfaceNames ();
59- foreach ($ ifaces as $ iface ) {
60- $ ifaces = array_filter ($ ifaces , function (string $ item ) use ($ iface ): bool {
61- return !is_subclass_of ($ iface , $ item );
62- });
63- }
64- $ class ->setImplements ($ ifaces );
65-
66- $ class ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
67- if ($ from ->getParentClass ()) {
68- $ class ->setExtends ($ from ->getParentClass ()->getName ());
69- $ class ->setImplements (array_diff ($ class ->getImplements (), $ from ->getParentClass ()->getInterfaceNames ()));
70- }
71- $ props = $ methods = [];
72- foreach ($ from ->getProperties () as $ prop ) {
73- if ($ prop ->isDefault () && $ prop ->getDeclaringClass ()->getName () === $ from ->getName ()) {
74- $ props [] = $ this ->fromPropertyReflection ($ prop );
75- }
76- }
77- $ class ->setProperties ($ props );
78- foreach ($ from ->getMethods () as $ method ) {
79- if ($ method ->getDeclaringClass ()->getName () === $ from ->getName ()) {
80- $ methods [] = $ this ->fromMethodReflection ($ method );
81- }
82- }
83- $ class ->setMethods ($ methods );
84- $ class ->setConstants ($ from ->getConstants ());
8557
8658 return $ namespace ;
8759 }
88-
89- public function fromMethodReflection (\ReflectionMethod $ from ): Method
90- {
91- $ method = new Method ($ from ->getName ());
92- $ method ->setParameters (array_map ([$ this , 'fromParameterReflection ' ], $ from ->getParameters ()));
93- $ method ->setStatic ($ from ->isStatic ());
94- $ isInterface = $ from ->getDeclaringClass ()->isInterface ();
95- $ method ->setVisibility (
96- $ from ->isPrivate ()
97- ? ClassType::VISIBILITY_PRIVATE
98- : ($ from ->isProtected () ? ClassType::VISIBILITY_PROTECTED : ($ isInterface ? null : ClassType::VISIBILITY_PUBLIC ))
99- );
100- $ method ->setFinal ($ from ->isFinal ());
101- $ method ->setAbstract ($ from ->isAbstract () && !$ isInterface );
102-
103- $ filename = $ from ->getFileName ();
104- $ rows = file ($ filename );
105- $ body = implode ('' , array_map (function ($ r ) {
106- return trim ($ r , ' ' );
107- }, \array_slice ($ rows , $ from ->getStartLine () + 1 , $ from ->getEndLine () - $ from ->getStartLine () - 2 )));
108-
109- $ method ->setBody ($ from ->isAbstract () ? null : print_r ($ body , true ));
110- $ method ->setReturnReference ($ from ->returnsReference ());
111- $ method ->setVariadic ($ from ->isVariadic ());
112- $ method ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
113- if ($ from ->hasReturnType () && ($ type = $ from ->getReturnType ()) instanceof \ReflectionNamedType) {
114- $ method ->setReturnType ($ type ->getName ());
115- $ method ->setReturnNullable ($ type ->allowsNull ());
116- }
117-
118- return $ method ;
119- }
120-
121- /**
122- * @return GlobalFunction|Closure
123- */
124- public function fromFunctionReflection (\ReflectionFunction $ from )
125- {
126- $ function = $ from ->isClosure () ? new Closure () : new GlobalFunction ($ from ->getName ());
127- $ function ->setParameters (array_map ([$ this , 'fromParameterReflection ' ], $ from ->getParameters ()));
128- $ function ->setReturnReference ($ from ->returnsReference ());
129- $ function ->setVariadic ($ from ->isVariadic ());
130- if (!$ from ->isClosure ()) {
131- $ function ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
132- }
133- if ($ from ->hasReturnType () && ($ type = $ from ->getReturnType ()) instanceof \ReflectionNamedType) {
134- $ function ->setReturnType ($ type ->getName ());
135- $ function ->setReturnNullable ($ type ->allowsNull ());
136- }
137-
138- return $ function ;
139- }
140-
141- public function fromParameterReflection (\ReflectionParameter $ from ): Parameter
142- {
143- $ param = new Parameter ($ from ->getName ());
144- $ param ->setReference ($ from ->isPassedByReference ());
145- if ($ from ->hasType () && ($ type = $ from ->getType ()) instanceof \ReflectionNamedType) {
146- $ param ->setType ($ type ->getName ());
147- $ param ->setNullable ($ type ->allowsNull ());
148- }
149- if ($ from ->isDefaultValueAvailable ()) {
150- $ param ->setDefaultValue ($ from ->isDefaultValueConstant ()
151- ? new Literal ($ from ->getDefaultValueConstantName ())
152- : $ from ->getDefaultValue ());
153- $ param ->setNullable ($ param ->isNullable () && null !== $ param ->getDefaultValue ());
154- }
155-
156- return $ param ;
157- }
158-
159- public function fromPropertyReflection (\ReflectionProperty $ from ): Property
160- {
161- $ defaults = $ from ->getDeclaringClass ()->getDefaultProperties ();
162- $ prop = new Property ($ from ->getName ());
163- if (isset ($ defaults [$ prop ->getName ()])) {
164- $ prop ->setValue ($ defaults [$ prop ->getName ()]);
165- }
166- $ prop ->setStatic ($ from ->isStatic ());
167- $ prop ->setVisibility (
168- $ from ->isPrivate ()
169- ? ClassType::VISIBILITY_PRIVATE
170- : ($ from ->isProtected () ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC )
171- );
172- if (\PHP_VERSION_ID >= 70400 && ($ type = $ from ->getType ()) instanceof \ReflectionNamedType) {
173- $ prop ->setType ($ type ->getName ());
174- $ prop ->setNullable ($ type ->allowsNull ());
175- $ prop ->setInitialized (\array_key_exists ($ prop ->getName (), $ defaults ));
176- }
177- $ prop ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
178-
179- return $ prop ;
180- }
18160}
0 commit comments