Skip to content

Commit

Permalink
Printer: multiline attributes are in own attribute block
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 26, 2023
1 parent 8853465 commit 210059d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ protected function printAttributes(array $attrs, bool $inline = false): string
$args = $this->dumper->format('...?:', $attr->getArguments());
$args = Helpers::simplifyTaggedNames($args, $this->namespace);
$items[] = $this->printType($attr->getName(), nullable: false) . ($args ? "($args)" : '');
$inline = $inline && !str_contains($args, "\n");
}

return $inline
Expand Down
21 changes: 21 additions & 0 deletions tests/PhpGenerator/Closure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,24 @@ same(
XX,
(string) $function,
);



$function = new Closure;
$function->setBody('return $a + $b;');
$function->addAttribute('Foo', ['a', str_repeat('b', 120)]);
$function->addAttribute('Bar');

same(
<<<'XX'
#[Foo(
'a',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
)]
#[Bar]
function () {
return $a + $b;
}
XX,
(string) $function,
);
19 changes: 19 additions & 0 deletions tests/PhpGenerator/Printer.function.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,22 @@ Assert::match(<<<'XX'
) {
}
XX, $printer->printFunction($function));


// parameter: multiple & multiline
$function = new GlobalFunction('func');
$param = $function->addParameter('foo');
$param->addAttribute('Bar');
$param->addAttribute('Foo', ['a', str_repeat('x', 120)]);

Assert::match(<<<'XX'
function func(
#[Bar]
#[Foo(
'a',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
)]
$foo,
) {
}
XX, $printer->printFunction($function));

0 comments on commit 210059d

Please sign in to comment.