Skip to content

Commit 31779e2

Browse files
committed
Printer: refactoring, added property $dumper
1 parent 0194e2d commit 31779e2

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/PhpGenerator/Printer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,19 @@ class Printer
3535
/** @var ?PhpNamespace */
3636
protected $namespace;
3737

38+
/** @var ?Dumper */
39+
protected $dumper;
40+
3841
/** @var bool */
3942
private $resolveTypes = true;
4043

4144

45+
public function __construct()
46+
{
47+
$this->dumper = new Dumper;
48+
}
49+
50+
4251
public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
4352
{
4453
$this->namespace = $this->resolveTypes ? $namespace : null;
@@ -63,7 +72,7 @@ public function printClosure(Closure $closure, PhpNamespace $namespace = null):
6372
foreach ($closure->getUses() as $param) {
6473
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
6574
}
66-
$useStr = strlen($tmp = implode(', ', $uses)) > (new Dumper)->wrapLength && count($uses) > 1
75+
$useStr = strlen($tmp = implode(', ', $uses)) > $this->dumper->wrapLength && count($uses) > 1
6776
? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . "\n"
6877
: $tmp;
6978

@@ -270,7 +279,7 @@ protected function indent(string $s): string
270279

271280
protected function dump($var, int $column = 0): string
272281
{
273-
return (new Dumper)->dump($var, $column);
282+
return $this->dumper->dump($var, $column);
274283
}
275284

276285

@@ -321,7 +330,7 @@ public function printParameters($function, int $column = 0): string
321330

322331
$line = implode(', ', $params);
323332

324-
return count($params) > 1 && ($special || strlen($line) + $column > (new Dumper)->wrapLength)
333+
return count($params) > 1 && ($special || strlen($line) + $column > $this->dumper->wrapLength)
325334
? "(\n" . $this->indent(implode(",\n", $params)) . ($special ? ',' : '') . "\n)"
326335
: "($line)";
327336
}
@@ -362,7 +371,7 @@ private function printAttributes(array $attrs, bool $inline = false): string
362371
}
363372
$items = [];
364373
foreach ($attrs as $attr) {
365-
$args = (new Dumper)->format('...?:', $attr->getArguments());
374+
$args = $this->dumper->format('...?:', $attr->getArguments());
366375
$items[] = $this->printType($attr->getName(), false) . ($args ? "($args)" : '');
367376
}
368377
return $inline

0 commit comments

Comments
 (0)