Skip to content

Commit 43d6101

Browse files
committed
Printer: print brace on next line when method/function has typehint
1 parent cd40df5 commit 43d6101

File tree

7 files changed

+98
-20
lines changed

7 files changed

+98
-20
lines changed

src/PhpGenerator/Printer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
4545
. ($function->getReturnReference() ? '&' : '')
4646
. $function->getName();
4747
$returnType = $this->printReturnType($function);
48+
$params = $this->printParameters($function, strlen($line) + strlen($returnType) + 2); // 2 = parentheses
4849
$body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
4950
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
51+
$braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType);
5052

5153
return $this->printDocComment($function)
5254
. $this->printAttributes($function->getAttributes())
5355
. $line
54-
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
56+
. $params
5557
. $returnType
56-
. ($this->bracesOnNextLine ? "\n" : ' ')
58+
. ($braceOnNextLine ? "\n" : ' ')
5759
. "{\n" . $this->indent($body) . "}\n";
5860
}
5961

@@ -117,7 +119,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
117119
$params = $this->printParameters($method, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2);
118120
$body = Helpers::simplifyTaggedNames($method->getBody(), $this->namespace);
119121
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
120-
$braceOnNextLine = $this->bracesOnNextLine && !str_contains($params, "\n");
122+
$braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType);
121123

122124
return $this->printDocComment($method)
123125
. $this->printAttributes($method->getAttributes())
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\Printer;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
$printer = new Printer;
12+
$function = new Nette\PhpGenerator\GlobalFunction('func');
13+
$function
14+
->setReturnType('stdClass')
15+
->setBody("func(); \r\nreturn 123;")
16+
->addParameter('var')
17+
->setType('stdClass');
18+
19+
Assert::match(<<<'XX'
20+
function func(stdClass $var): stdClass
21+
{
22+
func();
23+
return 123;
24+
}
25+
26+
XX, $printer->printFunction($function));
27+
28+
29+
$function = new Nette\PhpGenerator\GlobalFunction('multi');
30+
$function->addParameter('foo')
31+
->addAttribute('Foo');
32+
33+
Assert::match(<<<'XX'
34+
function multi(
35+
#[Foo] $foo,
36+
) {
37+
}
38+
39+
XX, $printer->printFunction($function));
40+
41+
42+
$function = new Nette\PhpGenerator\GlobalFunction('multiType');
43+
$function
44+
->setReturnType('array')
45+
->addParameter('foo')
46+
->addAttribute('Foo');
47+
48+
Assert::match(<<<'XX'
49+
function multiType(
50+
#[Foo] $foo,
51+
): array
52+
{
53+
}
54+
55+
XX, $printer->printFunction($function));

tests/PhpGenerator/Printer.phpt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ $class->addMethod('first')
5151

5252
$class->addMethod('second');
5353

54+
$method = $class->addMethod('multi')
55+
->addParameter('foo')
56+
->addAttribute('Foo');
57+
58+
$method = $class->addMethod('multiType')
59+
->setReturnType('array')
60+
->addParameter('foo')
61+
->addAttribute('Foo');
62+
5463

5564
sameFile(__DIR__ . '/expected/Printer.class.expect', $printer->printClass($class));
5665
sameFile(__DIR__ . '/expected/Printer.method.expect', $printer->printMethod($class->getMethod('first')));
@@ -63,17 +72,6 @@ sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($c
6372

6473

6574

66-
$printer = new Printer;
67-
$function = new Nette\PhpGenerator\GlobalFunction('func');
68-
$function
69-
->setReturnType('stdClass')
70-
->setBody("func(); \r\nreturn 123;")
71-
->addParameter('var')
72-
->setType('stdClass');
73-
74-
sameFile(__DIR__ . '/expected/Printer.function.expect', $printer->printFunction($function));
75-
76-
7775
$closure = new Nette\PhpGenerator\Closure;
7876
$closure
7977
->setReturnType('stdClass')

tests/PhpGenerator/expected/PhpNamespace.expect

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class A implements A, C
1818
array $d,
1919
?callable $e,
2020
C|string $f,
21-
): static|A {
21+
): static|A
22+
{
2223
}
2324
}
2425

tests/PhpGenerator/expected/Printer.class-alt.expect

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,18 @@ final class Example extends ParentClass implements IExample
6060

6161
public function second() {
6262
}
63+
64+
65+
66+
public function multi(
67+
#[Foo] $foo,
68+
) {
69+
}
70+
71+
72+
73+
public function multiType(
74+
#[Foo] $foo,
75+
): array {
76+
}
6377
}

tests/PhpGenerator/expected/Printer.class.expect

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,17 @@ final class Example extends ParentClass implements IExample
5959
public function second()
6060
{
6161
}
62+
63+
64+
public function multi(
65+
#[Foo] $foo,
66+
) {
67+
}
68+
69+
70+
public function multiType(
71+
#[Foo] $foo,
72+
): array
73+
{
74+
}
6275
}

tests/PhpGenerator/expected/Printer.function.expect

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)