From 8c19457832d21ea6b1bf41e9b123fea7fb5384a9 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 25 Apr 2018 14:29:06 +0200 Subject: [PATCH] FunctionLike: chops parameters when are longer than WRAP_LENGTH [Closes #28] --- src/PhpGenerator/Method.php | 7 +++-- src/PhpGenerator/Traits/FunctionLike.php | 5 +++- tests/PhpGenerator/Method.longParams.phpt | 33 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/PhpGenerator/Method.longParams.phpt diff --git a/src/PhpGenerator/Method.php b/src/PhpGenerator/Method.php index 7f3f75b5..c4e6ac3f 100644 --- a/src/PhpGenerator/Method.php +++ b/src/PhpGenerator/Method.php @@ -75,11 +75,14 @@ public function __toString() . 'function ' . ($this->returnReference ? '&' : '') . $this->name - . $this->parametersToString() + . ($params = $this->parametersToString()) . $this->returnTypeToString() . ($this->abstract || $this->body === false ? ';' - : "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}'); + : (strpos($params, "\n") === false ? "\n" : ' ') + . "{\n" + . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) + . '}'); } diff --git a/src/PhpGenerator/Traits/FunctionLike.php b/src/PhpGenerator/Traits/FunctionLike.php index 71c8e1c4..c6a4090a 100644 --- a/src/PhpGenerator/Traits/FunctionLike.php +++ b/src/PhpGenerator/Traits/FunctionLike.php @@ -216,7 +216,10 @@ protected function parametersToString() . '$' . $param->getName() . ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : ''); } - return '(' . implode(', ', $params) . ')'; + + return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH + ? "(\n\t" . implode(",\n\t", $params) . "\n)" + : "($tmp)"; } diff --git a/tests/PhpGenerator/Method.longParams.phpt b/tests/PhpGenerator/Method.longParams.phpt new file mode 100644 index 00000000..0f3e4c34 --- /dev/null +++ b/tests/PhpGenerator/Method.longParams.phpt @@ -0,0 +1,33 @@ +setBody('return null;'); + +for ($name = 'a'; $name < 'm'; $name++) { + $method->addParameter($name)->setTypeHint('string'); +} + +Assert::match( +'function create( + string $a, + string $b, + string $c, + string $d, + string $e, + string $f, + string $g, + string $h, + string $i, + string $j, + string $k, + string $l +) { + return null; +} +', (string) $method);