Skip to content

Commit 8c19457

Browse files
committed
FunctionLike: chops parameters when are longer than WRAP_LENGTH [Closes #28]
1 parent 01ce200 commit 8c19457

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/PhpGenerator/Method.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ public function __toString()
7575
. 'function '
7676
. ($this->returnReference ? '&' : '')
7777
. $this->name
78-
. $this->parametersToString()
78+
. ($params = $this->parametersToString())
7979
. $this->returnTypeToString()
8080
. ($this->abstract || $this->body === false
8181
? ';'
82-
: "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
82+
: (strpos($params, "\n") === false ? "\n" : ' ')
83+
. "{\n"
84+
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1)
85+
. '}');
8386
}
8487

8588

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ protected function parametersToString()
216216
. '$' . $param->getName()
217217
. ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->defaultValue) : '');
218218
}
219-
return '(' . implode(', ', $params) . ')';
219+
220+
return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH
221+
? "(\n\t" . implode(",\n\t", $params) . "\n)"
222+
: "($tmp)";
220223
}
221224

222225

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Nette\PhpGenerator\Method;
4+
use Tester\Assert;
5+
6+
require __DIR__ . '/../bootstrap.php';
7+
8+
9+
$method = (new Method('create'))
10+
->setBody('return null;');
11+
12+
for ($name = 'a'; $name < 'm'; $name++) {
13+
$method->addParameter($name)->setTypeHint('string');
14+
}
15+
16+
Assert::match(
17+
'function create(
18+
string $a,
19+
string $b,
20+
string $c,
21+
string $d,
22+
string $e,
23+
string $f,
24+
string $g,
25+
string $h,
26+
string $i,
27+
string $j,
28+
string $k,
29+
string $l
30+
) {
31+
return null;
32+
}
33+
', (string) $method);

0 commit comments

Comments
 (0)