Skip to content

Commit 8853465

Browse files
committed
Printer: attributes on parameters are placed on its own line
1 parent 52d62be commit 8853465

File tree

9 files changed

+166
-34
lines changed

9 files changed

+166
-34
lines changed

src/PhpGenerator/Printer.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -326,37 +326,51 @@ protected function printUses(PhpNamespace $namespace, string $of = PhpNamespace:
326326

327327
protected function printParameters(Closure|GlobalFunction|Method $function, int $column = 0): string
328328
{
329-
$params = [];
330-
$list = $function->getParameters();
331-
$multiline = false;
332-
333-
foreach ($list as $param) {
329+
$special = false;
330+
foreach ($function->getParameters() as $param) {
334331
$param->validate();
335-
$variadic = $function->isVariadic() && $param === end($list);
336-
$type = $param->getType();
332+
$special = $special || $param instanceof PromotedParameter || $param->getAttributes();
333+
}
334+
335+
if (!$special) {
336+
$line = $this->formatParameters($function, false);
337+
if (!str_contains($line, "\n") && strlen($line) + $column <= $this->wrapLength) {
338+
return $line;
339+
}
340+
}
341+
342+
return $this->formatParameters($function, true);
343+
}
344+
345+
346+
private function formatParameters(Closure|GlobalFunction|Method $function, bool $multiline): string
347+
{
348+
$params = $function->getParameters();
349+
$res = '';
350+
351+
foreach ($params as $param) {
352+
$variadic = $function->isVariadic() && $param === end($params);
337353
$promoted = $param instanceof PromotedParameter ? $param : null;
338-
$params[] =
354+
$attrs = $this->printAttributes($param->getAttributes(), inline: true);
355+
$res .=
339356
($promoted ? $this->printDocComment($promoted) : '')
340-
. ($attrs = $this->printAttributes($param->getAttributes(), inline: true))
357+
. ($attrs ? ($multiline ? substr($attrs, 0, -1) . "\n" : $attrs) : '')
341358
. ($promoted ?
342359
($promoted->getVisibility() ?: 'public')
343-
. ($promoted->isReadOnly() && $type ? ' readonly' : '')
360+
. ($promoted->isReadOnly() && $param->getType() ? ' readonly' : '')
344361
. ' ' : '')
345-
. ltrim($this->printType($type, $param->isNullable()) . ' ')
362+
. ltrim($this->printType($param->getType(), $param->isNullable()) . ' ')
346363
. ($param->isReference() ? '&' : '')
347364
. ($variadic ? '...' : '')
348365
. '$' . $param->getName()
349-
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '');
350-
351-
$multiline = $multiline || $promoted || $attrs;
366+
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '')
367+
. ($multiline ? ",\n" : ', ');
352368
}
353369

354-
$line = implode(', ', $params);
355-
$multiline = $multiline || count($params) > 1 && (strlen($line) + $column > $this->wrapLength);
356-
357370
return $multiline
358-
? "(\n" . $this->indent(implode(",\n", $params)) . ",\n)"
359-
: "($line)";
371+
? "(\n" . $this->indent($res) . ')'
372+
: '(' . substr($res, 0, -2) . ')';
373+
360374
}
361375

362376

tests/PhpGenerator/Printer.function.phpt

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
declare(strict_types=1);
44

5+
use Nette\PhpGenerator\GlobalFunction;
56
use Nette\PhpGenerator\Printer;
67
use Tester\Assert;
78

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

1011

1112
$printer = new Printer;
12-
$function = new Nette\PhpGenerator\GlobalFunction('func');
13+
$function = new GlobalFunction('func');
1314
$function
1415
->setReturnType('stdClass')
1516
->setBody("func(); \r\nreturn 123;")
@@ -26,30 +27,138 @@ Assert::match(<<<'XX'
2627
XX, $printer->printFunction($function));
2728

2829

29-
$function = new Nette\PhpGenerator\GlobalFunction('multi');
30+
$function = new GlobalFunction('multi');
3031
$function->addParameter('foo')
3132
->addAttribute('Foo');
3233

3334
Assert::match(<<<'XX'
3435
function multi(
35-
#[Foo] $foo,
36+
#[Foo]
37+
$foo,
3638
) {
3739
}
38-
3940
XX, $printer->printFunction($function));
4041

4142

42-
$function = new Nette\PhpGenerator\GlobalFunction('multiType');
43+
$function = new GlobalFunction('multiType');
4344
$function
4445
->setReturnType('array')
4546
->addParameter('foo')
4647
->addAttribute('Foo');
4748

4849
Assert::match(<<<'XX'
4950
function multiType(
50-
#[Foo] $foo,
51+
#[Foo]
52+
$foo,
5153
): array
5254
{
5355
}
56+
XX, $printer->printFunction($function));
57+
58+
59+
$function = new GlobalFunction('func');
60+
$function->addAttribute('Foo', [1, 2, 3]);
61+
$function->addAttribute('Bar');
62+
63+
same(
64+
<<<'XX'
65+
#[Foo(1, 2, 3)]
66+
#[Bar]
67+
function func()
68+
{
69+
}
70+
71+
XX,
72+
(string) $function,
73+
);
74+
75+
76+
// single
77+
$function = new GlobalFunction('func');
78+
$function->addAttribute('Foo', [1, 2, 3]);
79+
80+
Assert::match(<<<'XX'
81+
#[Foo(1, 2, 3)]
82+
function func()
83+
{
84+
}
85+
XX, $printer->printFunction($function));
86+
87+
88+
// multiple
89+
$function = new GlobalFunction('func');
90+
$function->addAttribute('Foo', [1, 2, 3]);
91+
$function->addAttribute('Bar');
92+
93+
Assert::match(<<<'XX'
94+
#[Foo(1, 2, 3)]
95+
#[Bar]
96+
function func()
97+
{
98+
}
99+
XX, $printer->printFunction($function));
100+
101+
102+
// multiline
103+
$function = new GlobalFunction('func');
104+
$function->addAttribute('Foo', ['a', str_repeat('x', 120)]);
105+
106+
Assert::match(<<<'XX'
107+
#[Foo(
108+
'a',
109+
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
110+
)]
111+
function func()
112+
{
113+
}
114+
XX, $printer->printFunction($function));
115+
116+
117+
// parameter: single
118+
$function = new GlobalFunction('func');
119+
$param = $function->addParameter('foo');
120+
$param->addAttribute('Foo', [1, 2, 3]);
121+
122+
Assert::match(<<<'XX'
123+
function func(
124+
#[Foo(1, 2, 3)]
125+
$foo,
126+
) {
127+
}
128+
XX, $printer->printFunction($function));
129+
54130

131+
// parameter: multiple
132+
$function = new GlobalFunction('func');
133+
$param = $function->addParameter('foo');
134+
$param->addAttribute('Foo', [1, 2, 3]);
135+
$param->addAttribute('Bar');
136+
137+
Assert::match(<<<'XX'
138+
function func(
139+
#[Foo(1, 2, 3), Bar]
140+
$foo,
141+
) {
142+
}
143+
XX, $printer->printFunction($function));
144+
145+
146+
// parameter: multiline
147+
$function = new GlobalFunction('func');
148+
$param = $function->addParameter('bar');
149+
$param->addAttribute('Foo');
150+
$param = $function->addParameter('foo');
151+
$param->addAttribute('Foo', ['a', str_repeat('x', 120)]);
152+
153+
Assert::match(<<<'XX'
154+
function func(
155+
#[Foo]
156+
$bar,
157+
#[Foo(
158+
'a',
159+
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
160+
)]
161+
$foo,
162+
) {
163+
}
55164
XX, $printer->printFunction($function));

tests/PhpGenerator/expected/ClassType.attributes.expect

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Example
2121
*/
2222
#[ExampleAttribute]
2323
public function getHandle(
24-
#[ExampleAttribute, WithArguments(123)] $mode,
24+
#[ExampleAttribute, WithArguments(123)]
25+
$mode,
2526
) {
2627
}
2728
}

tests/PhpGenerator/expected/ClassType.from.expect

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ class Class9
139139
*/
140140
#[ExampleAttribute]
141141
public function getHandle(
142-
#[WithArguments(123)] $mode,
142+
#[WithArguments(123)]
143+
$mode,
143144
) {
144145
}
145146
}

tests/PhpGenerator/expected/ClassType.promotion.expect

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class Example
44
$a,
55
public $b,
66
/** promo */
7-
#[Example] private string $c,
7+
#[Example]
8+
private string $c,
89
public readonly Draft $d = new Draft(10),
910
) {
1011
}

tests/PhpGenerator/expected/Extractor.classes.expect

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class Class9
149149
*/
150150
#[ExampleAttribute]
151151
public function getHandle(
152-
#[WithArguments(123)] $mode,
152+
#[WithArguments(123)]
153+
$mode,
153154
) {
154155
}
155156
}

tests/PhpGenerator/expected/PhpNamespace.expect

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class A implements A, C
1212

1313
#[A]
1414
public function test(
15-
#[\Bar\D] C $a,
15+
#[\Bar\D]
16+
C $a,
1617
self $b,
1718
parent $c,
1819
array $d,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,16 @@ final class Example extends ParentClass implements IExample
6464

6565

6666
public function multi(
67-
#[Foo] $foo,
67+
#[Foo]
68+
$foo,
6869
) {
6970
}
7071

7172

7273

7374
public function multiType(
74-
#[Foo] $foo,
75+
#[Foo]
76+
$foo,
7577
): array {
7678
}
7779
}

tests/PhpGenerator/expected/Printer.class.expect

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ final class Example extends ParentClass implements IExample
6262

6363

6464
public function multi(
65-
#[Foo] $foo,
65+
#[Foo]
66+
$foo,
6667
) {
6768
}
6869

6970

7071
public function multiType(
71-
#[Foo] $foo,
72+
#[Foo]
73+
$foo,
7274
): array
7375
{
7476
}

0 commit comments

Comments
 (0)