Skip to content

Commit

Permalink
Printer: parameter wrapping counts with function name length [Closes #77
Browse files Browse the repository at this point in the history
]
  • Loading branch information
dg committed Feb 8, 2021
1 parent 710e6c7 commit f662444
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
32 changes: 19 additions & 13 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ class Printer

public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
{
$line = 'function '
. ($function->getReturnReference() ? '&' : '')
. $function->getName();
$returnType = $this->printReturnType($function, $namespace);

return Helpers::formatDocComment($function->getComment() . "\n")
. self::printAttributes($function->getAttributes(), $namespace)
. 'function '
. ($function->getReturnReference() ? '&' : '')
. $function->getName()
. $this->printParameters($function, $namespace)
. $this->printReturnType($function, $namespace)
. $line
. $this->printParameters($function, $namespace, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
. $returnType
. "\n{\n" . $this->indent(ltrim(rtrim($function->getBody()) . "\n")) . "}\n";
}

Expand Down Expand Up @@ -89,17 +92,20 @@ public function printArrowFunction(Closure $closure): string
public function printMethod(Method $method, PhpNamespace $namespace = null): string
{
$method->validate();
return Helpers::formatDocComment($method->getComment() . "\n")
. self::printAttributes($method->getAttributes(), $namespace)
. ($method->isAbstract() ? 'abstract ' : '')
$line = ($method->isAbstract() ? 'abstract ' : '')
. ($method->isFinal() ? 'final ' : '')
. ($method->getVisibility() ? $method->getVisibility() . ' ' : '')
. ($method->isStatic() ? 'static ' : '')
. 'function '
. ($method->getReturnReference() ? '&' : '')
. $method->getName()
. ($params = $this->printParameters($method, $namespace))
. $this->printReturnType($method, $namespace)
. $method->getName();
$returnType = $this->printReturnType($method, $namespace);

return Helpers::formatDocComment($method->getComment() . "\n")
. self::printAttributes($method->getAttributes(), $namespace)
. $line
. ($params = $this->printParameters($method, $namespace, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2)) // 2 = parentheses
. $returnType
. ($method->isAbstract() || $method->getBody() === null
? ";\n"
: (strpos($params, "\n") === false ? "\n" : ' ')
Expand Down Expand Up @@ -254,7 +260,7 @@ protected function printUses(PhpNamespace $namespace): string
/**
* @param Closure|GlobalFunction|Method $function
*/
public function printParameters($function, PhpNamespace $namespace = null): string
public function printParameters($function, PhpNamespace $namespace = null, int $column = 0): string
{
$params = [];
$list = $function->getParameters();
Expand All @@ -279,7 +285,7 @@ public function printParameters($function, PhpNamespace $namespace = null): stri

$line = implode(', ', $params);

return count($params) > 1 && ($special || strlen($line) > (new Dumper)->wrapLength)
return count($params) > 1 && ($special || strlen($line) + $column > (new Dumper)->wrapLength)
? "(\n" . $this->indent(implode(",\n", $params)) . ($special ? ',' : '') . "\n)"
: "($line)";
}
Expand Down
11 changes: 9 additions & 2 deletions tests/PhpGenerator/expected/ClassType.from.expect
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,15 @@ class Class2 extends Class1 implements Interface2
* Func3
* @return Class1
*/
private function &func3(array $a = [], Class2 $b = null, Unknown $c, \Xyz\Unknown $d, callable $e, $f = Abc\Unknown::ABC, $g)
{
private function &func3(
array $a = [],
Class2 $b = null,
Unknown $c,
\Xyz\Unknown $d,
callable $e,
$f = Abc\Unknown::ABC,
$g
) {
}


Expand Down

0 comments on commit f662444

Please sign in to comment.