Skip to content

Commit db3b0b3

Browse files
committed
Printer: configurable indentation chars and lines between methods
1 parent 493145f commit db3b0b3

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/PhpGenerator/Printer.php

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ class Printer
2020
{
2121
use Nette\SmartObject;
2222

23+
/** @var string */
24+
protected $indentation = "\t";
25+
26+
/** @var int */
27+
protected $linesBetweenMethods = 2;
28+
29+
2330
public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
2431
{
2532
return Helpers::formatDocComment($function->getComment() . "\n")
@@ -28,7 +35,7 @@ public function printFunction(GlobalFunction $function, PhpNamespace $namespace
2835
. $function->getName()
2936
. $this->printParameters($function, $namespace)
3037
. $this->printReturnType($function, $namespace)
31-
. "\n{\n" . Strings::indent(ltrim(rtrim($function->getBody()) . "\n")) . '}';
38+
. "\n{\n" . $this->indent(ltrim(rtrim($function->getBody()) . "\n")) . '}';
3239
}
3340

3441

@@ -39,15 +46,15 @@ public function printClosure(Closure $closure): string
3946
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
4047
}
4148
$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"
4350
: $tmp;
4451

4552
return 'function '
4653
. ($closure->getReturnReference() ? '&' : '')
4754
. $this->printParameters($closure, null)
4855
. ($uses ? " use ($useStr)" : '')
4956
. $this->printReturnType($closure, null)
50-
. " {\n" . Strings::indent(ltrim(rtrim($closure->getBody()) . "\n")) . '}';
57+
. " {\n" . $this->indent(ltrim(rtrim($closure->getBody()) . "\n")) . '}';
5158
}
5259

5360

@@ -67,7 +74,7 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
6774
? ';'
6875
: (strpos($params, "\n") === false ? "\n" : ' ')
6976
. "{\n"
70-
. Strings::indent(ltrim(rtrim($method->getBody()) . "\n"))
77+
. $this->indent(ltrim(rtrim($method->getBody()) . "\n"))
7178
. '}');
7279
}
7380

@@ -79,7 +86,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
7986
$traits = [];
8087
foreach ($class->getTraitResolutions() as $trait => $resolutions) {
8188
$traits[] = 'use ' . $resolver($trait)
82-
. ($resolutions ? " {\n\t" . implode(";\n\t", $resolutions) . ";\n}" : ';');
89+
. ($resolutions ? " {\n" . $this->indentation . implode(";\n" . $this->indentation, $resolutions) . ";\n}" : ';');
8390
}
8491

8592
$consts = [];
@@ -102,6 +109,8 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
102109
$methods[] = $this->printMethod($method, $namespace);
103110
}
104111

112+
$methodSpace = str_repeat("\n", $this->linesBetweenMethods + 1);
113+
105114
return Strings::normalize(
106115
Helpers::formatDocComment($class->getComment() . "\n")
107116
. ($class->isAbstract() ? 'abstract ' : '')
@@ -110,11 +119,11 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
110119
. ($class->getExtends() ? 'extends ' . implode(', ', array_map($resolver, (array) $class->getExtends())) . ' ' : '')
111120
. ($class->getImplements() ? 'implements ' . implode(', ', array_map($resolver, $class->getImplements())) . ' ' : '')
112121
. ($class->getName() ? "\n" : '') . "{\n"
113-
. Strings::indent(
122+
. $this->indent(
114123
($traits ? implode("\n", $traits) . "\n\n" : '')
115124
. ($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" : ''))
118127
. '}'
119128
) . ($class->getName() ? "\n" : '');
120129
}
@@ -147,7 +156,7 @@ public function printNamespace(PhpNamespace $namespace): string
147156

148157
if ($namespace->getBracketedSyntax()) {
149158
return 'namespace' . ($name ? " $name" : '') . " {\n\n"
150-
. Strings::indent($body)
159+
. $this->indent($body)
151160
. "\n}\n";
152161

153162
} else {
@@ -172,6 +181,12 @@ public function printFile(PhpFile $file): string
172181
}
173182

174183

184+
protected function indent(string $s): string
185+
{
186+
return Strings::indent($s, 1, $this->indentation);
187+
}
188+
189+
175190
/**
176191
* @param Nette\PhpGenerator\Traits\FunctionLike $function
177192
*/
@@ -190,7 +205,7 @@ protected function printParameters($function, ?PhpNamespace $namespace): string
190205
}
191206

192207
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)"
194209
: "($tmp)";
195210
}
196211

0 commit comments

Comments
 (0)