Skip to content

Commit

Permalink
FunctionLike: chops parameters when are longer than WRAP_LENGTH [Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 26, 2018
1 parent 01ce200 commit 8c19457
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/PhpGenerator/Method.php
Expand Up @@ -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)
. '}');
}


Expand Down
5 changes: 4 additions & 1 deletion src/PhpGenerator/Traits/FunctionLike.php
Expand Up @@ -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)";
}


Expand Down
33 changes: 33 additions & 0 deletions tests/PhpGenerator/Method.longParams.phpt
@@ -0,0 +1,33 @@
<?php

use Nette\PhpGenerator\Method;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$method = (new Method('create'))
->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);

0 comments on commit 8c19457

Please sign in to comment.