Skip to content

Commit f662444

Browse files
committed
Printer: parameter wrapping counts with function name length [Closes #77]
1 parent 710e6c7 commit f662444

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/PhpGenerator/Printer.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ class Printer
3838

3939
public function printFunction(GlobalFunction $function, PhpNamespace $namespace = null): string
4040
{
41+
$line = 'function '
42+
. ($function->getReturnReference() ? '&' : '')
43+
. $function->getName();
44+
$returnType = $this->printReturnType($function, $namespace);
45+
4146
return Helpers::formatDocComment($function->getComment() . "\n")
4247
. self::printAttributes($function->getAttributes(), $namespace)
43-
. 'function '
44-
. ($function->getReturnReference() ? '&' : '')
45-
. $function->getName()
46-
. $this->printParameters($function, $namespace)
47-
. $this->printReturnType($function, $namespace)
48+
. $line
49+
. $this->printParameters($function, $namespace, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
50+
. $returnType
4851
. "\n{\n" . $this->indent(ltrim(rtrim($function->getBody()) . "\n")) . "}\n";
4952
}
5053

@@ -89,17 +92,20 @@ public function printArrowFunction(Closure $closure): string
8992
public function printMethod(Method $method, PhpNamespace $namespace = null): string
9093
{
9194
$method->validate();
92-
return Helpers::formatDocComment($method->getComment() . "\n")
93-
. self::printAttributes($method->getAttributes(), $namespace)
94-
. ($method->isAbstract() ? 'abstract ' : '')
95+
$line = ($method->isAbstract() ? 'abstract ' : '')
9596
. ($method->isFinal() ? 'final ' : '')
9697
. ($method->getVisibility() ? $method->getVisibility() . ' ' : '')
9798
. ($method->isStatic() ? 'static ' : '')
9899
. 'function '
99100
. ($method->getReturnReference() ? '&' : '')
100-
. $method->getName()
101-
. ($params = $this->printParameters($method, $namespace))
102-
. $this->printReturnType($method, $namespace)
101+
. $method->getName();
102+
$returnType = $this->printReturnType($method, $namespace);
103+
104+
return Helpers::formatDocComment($method->getComment() . "\n")
105+
. self::printAttributes($method->getAttributes(), $namespace)
106+
. $line
107+
. ($params = $this->printParameters($method, $namespace, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2)) // 2 = parentheses
108+
. $returnType
103109
. ($method->isAbstract() || $method->getBody() === null
104110
? ";\n"
105111
: (strpos($params, "\n") === false ? "\n" : ' ')
@@ -254,7 +260,7 @@ protected function printUses(PhpNamespace $namespace): string
254260
/**
255261
* @param Closure|GlobalFunction|Method $function
256262
*/
257-
public function printParameters($function, PhpNamespace $namespace = null): string
263+
public function printParameters($function, PhpNamespace $namespace = null, int $column = 0): string
258264
{
259265
$params = [];
260266
$list = $function->getParameters();
@@ -279,7 +285,7 @@ public function printParameters($function, PhpNamespace $namespace = null): stri
279285

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

282-
return count($params) > 1 && ($special || strlen($line) > (new Dumper)->wrapLength)
288+
return count($params) > 1 && ($special || strlen($line) + $column > (new Dumper)->wrapLength)
283289
? "(\n" . $this->indent(implode(",\n", $params)) . ($special ? ',' : '') . "\n)"
284290
: "($line)";
285291
}

tests/PhpGenerator/expected/ClassType.from.expect

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ class Class2 extends Class1 implements Interface2
4242
* Func3
4343
* @return Class1
4444
*/
45-
private function &func3(array $a = [], Class2 $b = null, Unknown $c, \Xyz\Unknown $d, callable $e, $f = Abc\Unknown::ABC, $g)
46-
{
45+
private function &func3(
46+
array $a = [],
47+
Class2 $b = null,
48+
Unknown $c,
49+
\Xyz\Unknown $d,
50+
callable $e,
51+
$f = Abc\Unknown::ABC,
52+
$g
53+
) {
4754
}
4855

4956

0 commit comments

Comments
 (0)