Skip to content

Commit 0194e2d

Browse files
committed
Printer: refactoring, added property $namespace
1 parent 0976f79 commit 0194e2d

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

src/PhpGenerator/Printer.php

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,33 @@ class Printer
3232
/** @var string */
3333
protected $returnTypeColon = ': ';
3434

35+
/** @var ?PhpNamespace */
36+
protected $namespace;
37+
3538
/** @var bool */
3639
private $resolveTypes = true;
3740

3841

3942
public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
4043
{
44+
$this->namespace = $this->resolveTypes ? $namespace : null;
4145
$line = 'function '
4246
. ($function->getReturnReference() ? '&' : '')
4347
. $function->getName();
44-
$returnType = $this->printReturnType($function, $namespace);
48+
$returnType = $this->printReturnType($function);
4549

4650
return Helpers::formatDocComment($function->getComment() . "\n")
47-
. self::printAttributes($function->getAttributes(), $namespace)
51+
. self::printAttributes($function->getAttributes())
4852
. $line
49-
. $this->printParameters($function, $namespace, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
53+
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
5054
. $returnType
5155
. "\n{\n" . $this->indent(ltrim(rtrim($function->getBody()) . "\n")) . "}\n";
5256
}
5357

5458

55-
public function printClosure(Closure $closure): string
59+
public function printClosure(Closure $closure, PhpNamespace $namespace = null): string
5660
{
61+
$this->namespace = $this->resolveTypes ? $namespace : null;
5762
$uses = [];
5863
foreach ($closure->getUses() as $param) {
5964
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
@@ -62,35 +67,37 @@ public function printClosure(Closure $closure): string
6267
? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . "\n"
6368
: $tmp;
6469

65-
return self::printAttributes($closure->getAttributes(), null, true)
70+
return self::printAttributes($closure->getAttributes(), true)
6671
. 'function '
6772
. ($closure->getReturnReference() ? '&' : '')
68-
. $this->printParameters($closure, null)
73+
. $this->printParameters($closure)
6974
. ($uses ? " use ($useStr)" : '')
70-
. $this->printReturnType($closure, null)
75+
. $this->printReturnType($closure)
7176
. " {\n" . $this->indent(ltrim(rtrim($closure->getBody()) . "\n")) . '}';
7277
}
7378

7479

75-
public function printArrowFunction(Closure $closure): string
80+
public function printArrowFunction(Closure $closure, PhpNamespace $namespace = null): string
7681
{
82+
$this->namespace = $this->resolveTypes ? $namespace : null;
7783
foreach ($closure->getUses() as $use) {
7884
if ($use->isReference()) {
7985
throw new Nette\InvalidArgumentException('Arrow function cannot bind variables by-reference.');
8086
}
8187
}
8288

83-
return self::printAttributes($closure->getAttributes(), null)
89+
return self::printAttributes($closure->getAttributes())
8490
. 'fn'
8591
. ($closure->getReturnReference() ? '&' : '')
86-
. $this->printParameters($closure, null)
87-
. $this->printReturnType($closure, null)
92+
. $this->printParameters($closure)
93+
. $this->printReturnType($closure)
8894
. ' => ' . trim($closure->getBody()) . ';';
8995
}
9096

9197

9298
public function printMethod(Method $method, PhpNamespace $namespace = null): string
9399
{
100+
$this->namespace = $this->resolveTypes ? $namespace : null;
94101
$method->validate();
95102
$line = ($method->isAbstract() ? 'abstract ' : '')
96103
. ($method->isFinal() ? 'final ' : '')
@@ -99,12 +106,12 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
99106
. 'function '
100107
. ($method->getReturnReference() ? '&' : '')
101108
. $method->getName();
102-
$returnType = $this->printReturnType($method, $namespace);
109+
$returnType = $this->printReturnType($method);
103110

104111
return Helpers::formatDocComment($method->getComment() . "\n")
105-
. self::printAttributes($method->getAttributes(), $namespace)
112+
. self::printAttributes($method->getAttributes())
106113
. $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
108115
. $returnType
109116
. ($method->isAbstract() || $method->getBody() === null
110117
? ";\n"
@@ -117,8 +124,9 @@ public function printMethod(Method $method, PhpNamespace $namespace = null): str
117124

118125
public function printClass(ClassType $class, PhpNamespace $namespace = null): string
119126
{
127+
$this->namespace = $this->resolveTypes ? $namespace : null;
120128
$class->validate();
121-
$resolver = $this->resolveTypes && $namespace
129+
$resolver = $this->namespace
122130
? [$namespace, 'unresolveType']
123131
: function ($s) { return $s; };
124132

@@ -131,7 +139,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
131139
$cases = [];
132140
foreach ($class->getCases() as $case) {
133141
$cases[] = Helpers::formatDocComment((string) $case->getComment())
134-
. self::printAttributes($case->getAttributes(), $namespace)
142+
. self::printAttributes($case->getAttributes())
135143
. 'case ' . $case->getName()
136144
. ($case->getValue() === null ? '' : ' = ' . $this->dump($case->getValue()))
137145
. ";\n";
@@ -147,7 +155,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
147155
. 'const ' . $const->getName() . ' = ';
148156

149157
$consts[] = Helpers::formatDocComment((string) $const->getComment())
150-
. self::printAttributes($const->getAttributes(), $namespace)
158+
. self::printAttributes($const->getAttributes())
151159
. $def
152160
. $this->dump($const->getValue(), strlen($def)) . ";\n";
153161
}
@@ -160,11 +168,11 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
160168
. ($property->isStatic() ? ' static' : '')
161169
. ($property->isReadOnly() && $type ? ' readonly' : '')
162170
. ' '
163-
. ltrim($this->printType($type, $property->isNullable(), $namespace) . ' ')
171+
. ltrim($this->printType($type, $property->isNullable()) . ' ')
164172
. '$' . $property->getName());
165173

166174
$properties[] = Helpers::formatDocComment((string) $property->getComment())
167-
. self::printAttributes($property->getAttributes(), $namespace)
175+
. self::printAttributes($property->getAttributes())
168176
. $def
169177
. ($property->getValue() === null && !$property->isInitialized() ? '' : ' = ' . $this->dump($property->getValue(), strlen($def) + 3)) // 3 = ' = '
170178
. ";\n";
@@ -186,7 +194,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
186194

187195
return Strings::normalize(
188196
Helpers::formatDocComment($class->getComment() . "\n")
189-
. self::printAttributes($class->getAttributes(), $namespace)
197+
. self::printAttributes($class->getAttributes())
190198
. ($class->isAbstract() ? 'abstract ' : '')
191199
. ($class->isFinal() ? 'final ' : '')
192200
. ($class->getName() ? $class->getType() . ' ' . $class->getName() . $enumType . ' ' : '')
@@ -201,6 +209,7 @@ public function printClass(ClassType $class, PhpNamespace $namespace = null): st
201209

202210
public function printNamespace(PhpNamespace $namespace): string
203211
{
212+
$this->namespace = $this->resolveTypes ? $namespace : null;
204213
$name = $namespace->getName();
205214
$uses = $this->printUses($namespace);
206215

@@ -283,7 +292,7 @@ protected function printUses(PhpNamespace $namespace): string
283292
/**
284293
* @param Closure|GlobalFunction|Method $function
285294
*/
286-
public function printParameters($function, PhpNamespace $namespace = null, int $column = 0): string
295+
public function printParameters($function, int $column = 0): string
287296
{
288297
$params = [];
289298
$list = $function->getParameters();
@@ -296,12 +305,12 @@ public function printParameters($function, PhpNamespace $namespace = null, int $
296305
$promoted = $param instanceof PromotedParameter ? $param : null;
297306
$params[] =
298307
($promoted ? Helpers::formatDocComment((string) $promoted->getComment()) : '')
299-
. ($attrs = self::printAttributes($param->getAttributes(), $namespace, true))
308+
. ($attrs = self::printAttributes($param->getAttributes(), true))
300309
. ($promoted ?
301310
($promoted->getVisibility() ?: 'public')
302311
. ($promoted->isReadOnly() && $type ? ' readonly' : '')
303312
. ' ' : '')
304-
. ltrim($this->printType($type, $param->isNullable(), $namespace) . ' ')
313+
. ltrim($this->printType($type, $param->isNullable()) . ' ')
305314
. ($param->isReference() ? '&' : '')
306315
. ($variadic ? '...' : '')
307316
. '$' . $param->getName()
@@ -318,13 +327,13 @@ public function printParameters($function, PhpNamespace $namespace = null, int $
318327
}
319328

320329

321-
public function printType(?string $type, bool $nullable = false, PhpNamespace $namespace = null): string
330+
public function printType(?string $type, bool $nullable): string
322331
{
323332
if ($type === null) {
324333
return '';
325334
}
326-
if ($this->resolveTypes && $namespace) {
327-
$type = $namespace->unresolveType($type);
335+
if ($this->namespace) {
336+
$type = $this->namespace->unresolveType($type);
328337
}
329338
if ($nullable && strcasecmp($type, 'mixed')) {
330339
$type = strpos($type, '|') === false
@@ -338,23 +347,23 @@ public function printType(?string $type, bool $nullable = false, PhpNamespace $n
338347
/**
339348
* @param Closure|GlobalFunction|Method $function
340349
*/
341-
private function printReturnType($function, ?PhpNamespace $namespace): string
350+
private function printReturnType($function): string
342351
{
343-
return ($tmp = $this->printType($function->getReturnType(), $function->isReturnNullable(), $namespace))
352+
return ($tmp = $this->printType($function->getReturnType(), $function->isReturnNullable()))
344353
? $this->returnTypeColon . $tmp
345354
: '';
346355
}
347356

348357

349-
private function printAttributes(array $attrs, ?PhpNamespace $namespace, bool $inline = false): string
358+
private function printAttributes(array $attrs, bool $inline = false): string
350359
{
351360
if (!$attrs) {
352361
return '';
353362
}
354363
$items = [];
355364
foreach ($attrs as $attr) {
356365
$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)" : '');
358367
}
359368
return $inline
360369
? '#[' . implode(', ', $items) . '] '

0 commit comments

Comments
 (0)