@@ -32,28 +32,33 @@ class Printer
32
32
/** @var string */
33
33
protected $ returnTypeColon = ': ' ;
34
34
35
+ /** @var ?PhpNamespace */
36
+ protected $ namespace ;
37
+
35
38
/** @var bool */
36
39
private $ resolveTypes = true ;
37
40
38
41
39
42
public function printFunction (GlobalFunction $ function , PhpNamespace $ namespace = null ): string
40
43
{
44
+ $ this ->namespace = $ this ->resolveTypes ? $ namespace : null ;
41
45
$ line = 'function '
42
46
. ($ function ->getReturnReference () ? '& ' : '' )
43
47
. $ function ->getName ();
44
- $ returnType = $ this ->printReturnType ($ function, $ namespace );
48
+ $ returnType = $ this ->printReturnType ($ function );
45
49
46
50
return Helpers::formatDocComment ($ function ->getComment () . "\n" )
47
- . self ::printAttributes ($ function ->getAttributes (), $ namespace )
51
+ . self ::printAttributes ($ function ->getAttributes ())
48
52
. $ line
49
- . $ this ->printParameters ($ function , $ namespace , strlen ($ line ) + strlen ($ returnType ) + 2 ) // 2 = parentheses
53
+ . $ this ->printParameters ($ function , strlen ($ line ) + strlen ($ returnType ) + 2 ) // 2 = parentheses
50
54
. $ returnType
51
55
. "\n{ \n" . $ this ->indent (ltrim (rtrim ($ function ->getBody ()) . "\n" )) . "} \n" ;
52
56
}
53
57
54
58
55
- public function printClosure (Closure $ closure ): string
59
+ public function printClosure (Closure $ closure, PhpNamespace $ namespace = null ): string
56
60
{
61
+ $ this ->namespace = $ this ->resolveTypes ? $ namespace : null ;
57
62
$ uses = [];
58
63
foreach ($ closure ->getUses () as $ param ) {
59
64
$ uses [] = ($ param ->isReference () ? '& ' : '' ) . '$ ' . $ param ->getName ();
@@ -62,35 +67,37 @@ public function printClosure(Closure $closure): string
62
67
? "\n" . $ this ->indentation . implode (", \n" . $ this ->indentation , $ uses ) . "\n"
63
68
: $ tmp ;
64
69
65
- return self ::printAttributes ($ closure ->getAttributes (), null , true )
70
+ return self ::printAttributes ($ closure ->getAttributes (), true )
66
71
. 'function '
67
72
. ($ closure ->getReturnReference () ? '& ' : '' )
68
- . $ this ->printParameters ($ closure, null )
73
+ . $ this ->printParameters ($ closure )
69
74
. ($ uses ? " use ( $ useStr) " : '' )
70
- . $ this ->printReturnType ($ closure, null )
75
+ . $ this ->printReturnType ($ closure )
71
76
. " { \n" . $ this ->indent (ltrim (rtrim ($ closure ->getBody ()) . "\n" )) . '} ' ;
72
77
}
73
78
74
79
75
- public function printArrowFunction (Closure $ closure ): string
80
+ public function printArrowFunction (Closure $ closure, PhpNamespace $ namespace = null ): string
76
81
{
82
+ $ this ->namespace = $ this ->resolveTypes ? $ namespace : null ;
77
83
foreach ($ closure ->getUses () as $ use ) {
78
84
if ($ use ->isReference ()) {
79
85
throw new Nette \InvalidArgumentException ('Arrow function cannot bind variables by-reference. ' );
80
86
}
81
87
}
82
88
83
- return self ::printAttributes ($ closure ->getAttributes (), null )
89
+ return self ::printAttributes ($ closure ->getAttributes ())
84
90
. 'fn '
85
91
. ($ closure ->getReturnReference () ? '& ' : '' )
86
- . $ this ->printParameters ($ closure, null )
87
- . $ this ->printReturnType ($ closure, null )
92
+ . $ this ->printParameters ($ closure )
93
+ . $ this ->printReturnType ($ closure )
88
94
. ' => ' . trim ($ closure ->getBody ()) . '; ' ;
89
95
}
90
96
91
97
92
98
public function printMethod (Method $ method , PhpNamespace $ namespace = null ): string
93
99
{
100
+ $ this ->namespace = $ this ->resolveTypes ? $ namespace : null ;
94
101
$ method ->validate ();
95
102
$ line = ($ method ->isAbstract () ? 'abstract ' : '' )
96
103
. ($ method ->isFinal () ? 'final ' : '' )
@@ -99,12 +106,12 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
99
106
. 'function '
100
107
. ($ method ->getReturnReference () ? '& ' : '' )
101
108
. $ method ->getName ();
102
- $ returnType = $ this ->printReturnType ($ method, $ namespace );
109
+ $ returnType = $ this ->printReturnType ($ method );
103
110
104
111
return Helpers::formatDocComment ($ method ->getComment () . "\n" )
105
- . self ::printAttributes ($ method ->getAttributes (), $ namespace )
112
+ . self ::printAttributes ($ method ->getAttributes ())
106
113
. $ line
107
- . ($ params = $ this ->printParameters ($ method , $ namespace , strlen ($ line ) + strlen ($ returnType ) + strlen ($ this ->indentation ) + 2 )) // 2 = parentheses
114
+ . ($ params = $ this ->printParameters ($ method , strlen ($ line ) + strlen ($ returnType ) + strlen ($ this ->indentation ) + 2 )) // 2 = parentheses
108
115
. $ returnType
109
116
. ($ method ->isAbstract () || $ method ->getBody () === null
110
117
? "; \n"
@@ -117,8 +124,9 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
117
124
118
125
public function printClass (ClassType $ class , PhpNamespace $ namespace = null ): string
119
126
{
127
+ $ this ->namespace = $ this ->resolveTypes ? $ namespace : null ;
120
128
$ class ->validate ();
121
- $ resolver = $ this ->resolveTypes && $ namespace
129
+ $ resolver = $ this ->namespace
122
130
? [$ namespace , 'unresolveType ' ]
123
131
: function ($ s ) { return $ s ; };
124
132
@@ -131,7 +139,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
131
139
$ cases = [];
132
140
foreach ($ class ->getCases () as $ case ) {
133
141
$ cases [] = Helpers::formatDocComment ((string ) $ case ->getComment ())
134
- . self ::printAttributes ($ case ->getAttributes (), $ namespace )
142
+ . self ::printAttributes ($ case ->getAttributes ())
135
143
. 'case ' . $ case ->getName ()
136
144
. ($ case ->getValue () === null ? '' : ' = ' . $ this ->dump ($ case ->getValue ()))
137
145
. "; \n" ;
@@ -147,7 +155,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
147
155
. 'const ' . $ const ->getName () . ' = ' ;
148
156
149
157
$ consts [] = Helpers::formatDocComment ((string ) $ const ->getComment ())
150
- . self ::printAttributes ($ const ->getAttributes (), $ namespace )
158
+ . self ::printAttributes ($ const ->getAttributes ())
151
159
. $ def
152
160
. $ this ->dump ($ const ->getValue (), strlen ($ def )) . "; \n" ;
153
161
}
@@ -160,11 +168,11 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
160
168
. ($ property ->isStatic () ? ' static ' : '' )
161
169
. ($ property ->isReadOnly () && $ type ? ' readonly ' : '' )
162
170
. ' '
163
- . ltrim ($ this ->printType ($ type , $ property ->isNullable (), $ namespace ) . ' ' )
171
+ . ltrim ($ this ->printType ($ type , $ property ->isNullable ()) . ' ' )
164
172
. '$ ' . $ property ->getName ());
165
173
166
174
$ properties [] = Helpers::formatDocComment ((string ) $ property ->getComment ())
167
- . self ::printAttributes ($ property ->getAttributes (), $ namespace )
175
+ . self ::printAttributes ($ property ->getAttributes ())
168
176
. $ def
169
177
. ($ property ->getValue () === null && !$ property ->isInitialized () ? '' : ' = ' . $ this ->dump ($ property ->getValue (), strlen ($ def ) + 3 )) // 3 = ' = '
170
178
. "; \n" ;
@@ -186,7 +194,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
186
194
187
195
return Strings::normalize (
188
196
Helpers::formatDocComment ($ class ->getComment () . "\n" )
189
- . self ::printAttributes ($ class ->getAttributes (), $ namespace )
197
+ . self ::printAttributes ($ class ->getAttributes ())
190
198
. ($ class ->isAbstract () ? 'abstract ' : '' )
191
199
. ($ class ->isFinal () ? 'final ' : '' )
192
200
. ($ class ->getName () ? $ class ->getType () . ' ' . $ class ->getName () . $ enumType . ' ' : '' )
@@ -201,6 +209,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
201
209
202
210
public function printNamespace (PhpNamespace $ namespace ): string
203
211
{
212
+ $ this ->namespace = $ this ->resolveTypes ? $ namespace : null ;
204
213
$ name = $ namespace ->getName ();
205
214
$ uses = $ this ->printUses ($ namespace );
206
215
@@ -283,7 +292,7 @@ protected function printUses(PhpNamespace $namespace): string
283
292
/**
284
293
* @param Closure|GlobalFunction|Method $function
285
294
*/
286
- public function printParameters ($ function , PhpNamespace $ namespace = null , int $ column = 0 ): string
295
+ public function printParameters ($ function , int $ column = 0 ): string
287
296
{
288
297
$ params = [];
289
298
$ list = $ function ->getParameters ();
@@ -296,12 +305,12 @@ public function printParameters($function, PhpNamespace $namespace = null, int $
296
305
$ promoted = $ param instanceof PromotedParameter ? $ param : null ;
297
306
$ params [] =
298
307
($ promoted ? Helpers::formatDocComment ((string ) $ promoted ->getComment ()) : '' )
299
- . ($ attrs = self ::printAttributes ($ param ->getAttributes (), $ namespace , true ))
308
+ . ($ attrs = self ::printAttributes ($ param ->getAttributes (), true ))
300
309
. ($ promoted ?
301
310
($ promoted ->getVisibility () ?: 'public ' )
302
311
. ($ promoted ->isReadOnly () && $ type ? ' readonly ' : '' )
303
312
. ' ' : '' )
304
- . ltrim ($ this ->printType ($ type , $ param ->isNullable (), $ namespace ) . ' ' )
313
+ . ltrim ($ this ->printType ($ type , $ param ->isNullable ()) . ' ' )
305
314
. ($ param ->isReference () ? '& ' : '' )
306
315
. ($ variadic ? '... ' : '' )
307
316
. '$ ' . $ param ->getName ()
@@ -318,13 +327,13 @@ public function printParameters($function, PhpNamespace $namespace = null, int $
318
327
}
319
328
320
329
321
- public function printType (?string $ type , bool $ nullable = false , PhpNamespace $ namespace = null ): string
330
+ public function printType (?string $ type , bool $ nullable ): string
322
331
{
323
332
if ($ type === null ) {
324
333
return '' ;
325
334
}
326
- if ($ this ->resolveTypes && $ namespace ) {
327
- $ type = $ namespace ->unresolveType ($ type );
335
+ if ($ this ->namespace ) {
336
+ $ type = $ this -> namespace ->unresolveType ($ type );
328
337
}
329
338
if ($ nullable && strcasecmp ($ type , 'mixed ' )) {
330
339
$ type = strpos ($ type , '| ' ) === false
@@ -338,23 +347,23 @@ public function printType(?string $type, bool $nullable = false, PhpNamespace $n
338
347
/**
339
348
* @param Closure|GlobalFunction|Method $function
340
349
*/
341
- private function printReturnType ($ function, ? PhpNamespace $ namespace ): string
350
+ private function printReturnType ($ function ): string
342
351
{
343
- return ($ tmp = $ this ->printType ($ function ->getReturnType (), $ function ->isReturnNullable (), $ namespace ))
352
+ return ($ tmp = $ this ->printType ($ function ->getReturnType (), $ function ->isReturnNullable ()))
344
353
? $ this ->returnTypeColon . $ tmp
345
354
: '' ;
346
355
}
347
356
348
357
349
- private function printAttributes (array $ attrs , ? PhpNamespace $ namespace , bool $ inline = false ): string
358
+ private function printAttributes (array $ attrs , bool $ inline = false ): string
350
359
{
351
360
if (!$ attrs ) {
352
361
return '' ;
353
362
}
354
363
$ items = [];
355
364
foreach ($ attrs as $ attr ) {
356
365
$ args = (new Dumper )->format ('...?: ' , $ attr ->getArguments ());
357
- $ items [] = $ this ->printType ($ attr ->getName (), false , $ namespace ) . ($ args ? "( $ args) " : '' );
366
+ $ items [] = $ this ->printType ($ attr ->getName (), false ) . ($ args ? "( $ args) " : '' );
358
367
}
359
368
return $ inline
360
369
? '#[ ' . implode (', ' , $ items ) . '] '
0 commit comments