Skip to content

Commit 210059d

Browse files
committed
Printer: multiline attributes are in own attribute block
1 parent 8853465 commit 210059d

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/PhpGenerator/Printer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ protected function printAttributes(array $attrs, bool $inline = false): string
425425
$args = $this->dumper->format('...?:', $attr->getArguments());
426426
$args = Helpers::simplifyTaggedNames($args, $this->namespace);
427427
$items[] = $this->printType($attr->getName(), nullable: false) . ($args ? "($args)" : '');
428+
$inline = $inline && !str_contains($args, "\n");
428429
}
429430

430431
return $inline

tests/PhpGenerator/Closure.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,24 @@ same(
8484
XX,
8585
(string) $function,
8686
);
87+
88+
89+
90+
$function = new Closure;
91+
$function->setBody('return $a + $b;');
92+
$function->addAttribute('Foo', ['a', str_repeat('b', 120)]);
93+
$function->addAttribute('Bar');
94+
95+
same(
96+
<<<'XX'
97+
#[Foo(
98+
'a',
99+
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
100+
)]
101+
#[Bar]
102+
function () {
103+
return $a + $b;
104+
}
105+
XX,
106+
(string) $function,
107+
);

tests/PhpGenerator/Printer.function.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,22 @@ Assert::match(<<<'XX'
162162
) {
163163
}
164164
XX, $printer->printFunction($function));
165+
166+
167+
// parameter: multiple & multiline
168+
$function = new GlobalFunction('func');
169+
$param = $function->addParameter('foo');
170+
$param->addAttribute('Bar');
171+
$param->addAttribute('Foo', ['a', str_repeat('x', 120)]);
172+
173+
Assert::match(<<<'XX'
174+
function func(
175+
#[Bar]
176+
#[Foo(
177+
'a',
178+
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
179+
)]
180+
$foo,
181+
) {
182+
}
183+
XX, $printer->printFunction($function));

0 commit comments

Comments
 (0)