From bce6abcd4090ab5eec24b78f26c753c6525a425c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 23 Sep 2021 19:17:03 +0200 Subject: [PATCH] Printer: added property $wrapLength [Closes #55][Closes #56] --- src/PhpGenerator/Printer.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 5ea6bc01..ed03a98e 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -20,6 +20,9 @@ class Printer { use Nette\SmartObject; + /** @var int */ + public $wrapLength = 120; + /** @var string */ protected $indentation = "\t"; @@ -73,7 +76,7 @@ public function printClosure(Closure $closure, PhpNamespace $namespace = null): foreach ($closure->getUses() as $param) { $uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName(); } - $useStr = strlen($tmp = implode(', ', $uses)) > $this->dumper->wrapLength && count($uses) > 1 + $useStr = strlen($tmp = implode(', ', $uses)) > $this->wrapLength && count($uses) > 1 ? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . "\n" : $tmp; $body = Helpers::simplifyTaggedNames($closure->getBody(), $this->namespace); @@ -320,7 +323,7 @@ protected function printParameters($function, int $column = 0): string $line = implode(', ', $params); - return count($params) > 1 && ($special || strlen($line) + $column > $this->dumper->wrapLength) + return count($params) > 1 && ($special || strlen($line) + $column > $this->wrapLength) ? "(\n" . $this->indent(implode(",\n", $params)) . ($special ? ',' : '') . "\n)" : "($line)"; } @@ -390,6 +393,7 @@ protected function indent(string $s): string protected function dump($var, int $column = 0): string { $this->dumper->indentation = $this->indentation; + $this->dumper->wrapLength = $this->wrapLength; $s = $this->dumper->dump($var, $column); $s = Helpers::simplifyTaggedNames($s, $this->namespace); return $s;