Skip to content

Commit 5891f37

Browse files
committed
FunctionLike: chops parameters when are longer than WRAP_LENGTH [Closes #28]
1 parent acb64bc commit 5891f37

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/PhpGenerator/Method.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ public function __toString(): string
7272
. 'function '
7373
. ($this->returnReference ? '&' : '')
7474
. $this->name
75-
. $this->parametersToString()
75+
. ($params = $this->parametersToString())
7676
. $this->returnTypeToString()
7777
. ($this->abstract || $this->body === null
7878
? ';'
79-
: "\n{\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}');
79+
: (strpos($params, "\n") === false ? "\n" : ' ')
80+
. "{\n"
81+
. Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1)
82+
. '}');
8083
}
8184

8285

src/PhpGenerator/Traits/FunctionLike.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ protected function parametersToString(): string
197197
. '$' . $param->getName()
198198
. ($param->hasDefaultValue() && !$variadic ? ' = ' . Helpers::dump($param->getDefaultValue()) : '');
199199
}
200-
return '(' . implode(', ', $params) . ')';
200+
201+
return strlen($tmp = implode(', ', $params)) > Helpers::WRAP_LENGTH && count($params) > 1
202+
? "(\n\t" . implode(",\n\t", $params) . "\n)"
203+
: "($tmp)";
201204
}
202205

203206

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

0 commit comments

Comments
 (0)