@@ -20,6 +20,13 @@ class Printer
20
20
{
21
21
use Nette \SmartObject;
22
22
23
+ /** @var string */
24
+ protected $ indentation = "\t" ;
25
+
26
+ /** @var int */
27
+ protected $ linesBetweenMethods = 2 ;
28
+
29
+
23
30
public function printFunction (GlobalFunction $ function , PhpNamespace $ namespace = null ): string
24
31
{
25
32
return Helpers::formatDocComment ($ function ->getComment () . "\n" )
@@ -28,7 +35,7 @@ public function printFunction(GlobalFunction $function, PhpNamespace $namespace
28
35
. $ function ->getName ()
29
36
. $ this ->printParameters ($ function , $ namespace )
30
37
. $ this ->printReturnType ($ function , $ namespace )
31
- . "\n{ \n" . Strings:: indent (ltrim (rtrim ($ function ->getBody ()) . "\n" )) . '} ' ;
38
+ . "\n{ \n" . $ this -> indent (ltrim (rtrim ($ function ->getBody ()) . "\n" )) . '} ' ;
32
39
}
33
40
34
41
@@ -39,15 +46,15 @@ public function printClosure(Closure $closure): string
39
46
$ uses [] = ($ param ->isReference () ? '& ' : '' ) . '$ ' . $ param ->getName ();
40
47
}
41
48
$ useStr = strlen ($ tmp = implode (', ' , $ uses )) > Helpers::WRAP_LENGTH && count ($ uses ) > 1
42
- ? "\n\t " . implode (", \n\t" , $ uses ) . "\n"
49
+ ? "\n" . $ this -> indentation . implode (", \n" . $ this -> indentation , $ uses ) . "\n"
43
50
: $ tmp ;
44
51
45
52
return 'function '
46
53
. ($ closure ->getReturnReference () ? '& ' : '' )
47
54
. $ this ->printParameters ($ closure , null )
48
55
. ($ uses ? " use ( $ useStr) " : '' )
49
56
. $ this ->printReturnType ($ closure , null )
50
- . " { \n" . Strings:: indent (ltrim (rtrim ($ closure ->getBody ()) . "\n" )) . '} ' ;
57
+ . " { \n" . $ this -> indent (ltrim (rtrim ($ closure ->getBody ()) . "\n" )) . '} ' ;
51
58
}
52
59
53
60
@@ -67,7 +74,7 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
67
74
? '; '
68
75
: (strpos ($ params , "\n" ) === false ? "\n" : ' ' )
69
76
. "{ \n"
70
- . Strings:: indent (ltrim (rtrim ($ method ->getBody ()) . "\n" ))
77
+ . $ this -> indent (ltrim (rtrim ($ method ->getBody ()) . "\n" ))
71
78
. '} ' );
72
79
}
73
80
@@ -79,7 +86,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
79
86
$ traits = [];
80
87
foreach ($ class ->getTraitResolutions () as $ trait => $ resolutions ) {
81
88
$ traits [] = 'use ' . $ resolver ($ trait )
82
- . ($ resolutions ? " { \n\t " . implode ("; \n\t" , $ resolutions ) . "; \n} " : '; ' );
89
+ . ($ resolutions ? " { \n" . $ this -> indentation . implode ("; \n" . $ this -> indentation , $ resolutions ) . "; \n} " : '; ' );
83
90
}
84
91
85
92
$ consts = [];
@@ -102,6 +109,8 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
102
109
$ methods [] = $ this ->printMethod ($ method , $ namespace );
103
110
}
104
111
112
+ $ methodSpace = str_repeat ("\n" , $ this ->linesBetweenMethods + 1 );
113
+
105
114
return Strings::normalize (
106
115
Helpers::formatDocComment ($ class ->getComment () . "\n" )
107
116
. ($ class ->isAbstract () ? 'abstract ' : '' )
@@ -110,11 +119,11 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
110
119
. ($ class ->getExtends () ? 'extends ' . implode (', ' , array_map ($ resolver , (array ) $ class ->getExtends ())) . ' ' : '' )
111
120
. ($ class ->getImplements () ? 'implements ' . implode (', ' , array_map ($ resolver , $ class ->getImplements ())) . ' ' : '' )
112
121
. ($ class ->getName () ? "\n" : '' ) . "{ \n"
113
- . Strings:: indent (
122
+ . $ this -> indent (
114
123
($ traits ? implode ("\n" , $ traits ) . "\n\n" : '' )
115
124
. ($ consts ? implode ("\n" , $ consts ) . "\n\n" : '' )
116
- . ($ properties ? implode ("\n\n" , $ properties ) . "\n\n\n" : '' )
117
- . ($ methods ? implode ("\n\n\n" , $ methods ) . "\n" : '' ))
125
+ . ($ properties ? implode ("\n\n" , $ properties ) . $ methodSpace : '' )
126
+ . ($ methods ? implode ($ methodSpace , $ methods ) . "\n" : '' ))
118
127
. '} '
119
128
) . ($ class ->getName () ? "\n" : '' );
120
129
}
@@ -147,7 +156,7 @@ public function printNamespace(PhpNamespace $namespace): string
147
156
148
157
if ($ namespace ->getBracketedSyntax ()) {
149
158
return 'namespace ' . ($ name ? " $ name " : '' ) . " { \n\n"
150
- . Strings:: indent ($ body )
159
+ . $ this -> indent ($ body )
151
160
. "\n} \n" ;
152
161
153
162
} else {
@@ -172,6 +181,12 @@ public function printFile(PhpFile $file): string
172
181
}
173
182
174
183
184
+ protected function indent (string $ s ): string
185
+ {
186
+ return Strings::indent ($ s , 1 , $ this ->indentation );
187
+ }
188
+
189
+
175
190
/**
176
191
* @param Nette\PhpGenerator\Traits\FunctionLike $function
177
192
*/
@@ -190,7 +205,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
190
205
}
191
206
192
207
return strlen ($ tmp = implode (', ' , $ params )) > Helpers::WRAP_LENGTH && count ($ params ) > 1
193
- ? "( \n\t " . implode (", \n\t" , $ params ) . "\n) "
208
+ ? "( \n" . $ this -> indentation . implode (", \n" . $ this -> indentation , $ params ) . "\n) "
194
209
: "( $ tmp) " ;
195
210
}
196
211
0 commit comments