Skip to content

Commit

Permalink
Printer: attributes on parameters are placed on its own line
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 26, 2023
1 parent 52d62be commit 8853465
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 34 deletions.
52 changes: 33 additions & 19 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,37 +326,51 @@ protected function printUses(PhpNamespace $namespace, string $of = PhpNamespace:

protected function printParameters(Closure|GlobalFunction|Method $function, int $column = 0): string
{
$params = [];
$list = $function->getParameters();
$multiline = false;

foreach ($list as $param) {
$special = false;
foreach ($function->getParameters() as $param) {
$param->validate();
$variadic = $function->isVariadic() && $param === end($list);
$type = $param->getType();
$special = $special || $param instanceof PromotedParameter || $param->getAttributes();
}

if (!$special) {
$line = $this->formatParameters($function, false);
if (!str_contains($line, "\n") && strlen($line) + $column <= $this->wrapLength) {
return $line;
}
}

return $this->formatParameters($function, true);
}


private function formatParameters(Closure|GlobalFunction|Method $function, bool $multiline): string
{
$params = $function->getParameters();
$res = '';

foreach ($params as $param) {
$variadic = $function->isVariadic() && $param === end($params);
$promoted = $param instanceof PromotedParameter ? $param : null;
$params[] =
$attrs = $this->printAttributes($param->getAttributes(), inline: true);
$res .=
($promoted ? $this->printDocComment($promoted) : '')
. ($attrs = $this->printAttributes($param->getAttributes(), inline: true))
. ($attrs ? ($multiline ? substr($attrs, 0, -1) . "\n" : $attrs) : '')
. ($promoted ?
($promoted->getVisibility() ?: 'public')
. ($promoted->isReadOnly() && $type ? ' readonly' : '')
. ($promoted->isReadOnly() && $param->getType() ? ' readonly' : '')
. ' ' : '')
. ltrim($this->printType($type, $param->isNullable()) . ' ')
. ltrim($this->printType($param->getType(), $param->isNullable()) . ' ')
. ($param->isReference() ? '&' : '')
. ($variadic ? '...' : '')
. '$' . $param->getName()
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '');

$multiline = $multiline || $promoted || $attrs;
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '')
. ($multiline ? ",\n" : ', ');
}

$line = implode(', ', $params);
$multiline = $multiline || count($params) > 1 && (strlen($line) + $column > $this->wrapLength);

return $multiline
? "(\n" . $this->indent(implode(",\n", $params)) . ",\n)"
: "($line)";
? "(\n" . $this->indent($res) . ')'
: '(' . substr($res, 0, -2) . ')';

}


Expand Down
121 changes: 115 additions & 6 deletions tests/PhpGenerator/Printer.function.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

declare(strict_types=1);

use Nette\PhpGenerator\GlobalFunction;
use Nette\PhpGenerator\Printer;
use Tester\Assert;

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


$printer = new Printer;
$function = new Nette\PhpGenerator\GlobalFunction('func');
$function = new GlobalFunction('func');
$function
->setReturnType('stdClass')
->setBody("func(); \r\nreturn 123;")
Expand All @@ -26,30 +27,138 @@ Assert::match(<<<'XX'
XX, $printer->printFunction($function));


$function = new Nette\PhpGenerator\GlobalFunction('multi');
$function = new GlobalFunction('multi');
$function->addParameter('foo')
->addAttribute('Foo');

Assert::match(<<<'XX'
function multi(
#[Foo] $foo,
#[Foo]
$foo,
) {
}

XX, $printer->printFunction($function));


$function = new Nette\PhpGenerator\GlobalFunction('multiType');
$function = new GlobalFunction('multiType');
$function
->setReturnType('array')
->addParameter('foo')
->addAttribute('Foo');

Assert::match(<<<'XX'
function multiType(
#[Foo] $foo,
#[Foo]
$foo,
): array
{
}
XX, $printer->printFunction($function));


$function = new GlobalFunction('func');
$function->addAttribute('Foo', [1, 2, 3]);
$function->addAttribute('Bar');

same(
<<<'XX'
#[Foo(1, 2, 3)]
#[Bar]
function func()
{
}

XX,
(string) $function,
);


// single
$function = new GlobalFunction('func');
$function->addAttribute('Foo', [1, 2, 3]);

Assert::match(<<<'XX'
#[Foo(1, 2, 3)]
function func()
{
}
XX, $printer->printFunction($function));


// multiple
$function = new GlobalFunction('func');
$function->addAttribute('Foo', [1, 2, 3]);
$function->addAttribute('Bar');

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


// multiline
$function = new GlobalFunction('func');
$function->addAttribute('Foo', ['a', str_repeat('x', 120)]);

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


// parameter: single
$function = new GlobalFunction('func');
$param = $function->addParameter('foo');
$param->addAttribute('Foo', [1, 2, 3]);

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


// parameter: multiple
$function = new GlobalFunction('func');
$param = $function->addParameter('foo');
$param->addAttribute('Foo', [1, 2, 3]);
$param->addAttribute('Bar');

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


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

Assert::match(<<<'XX'
function func(
#[Foo]
$bar,
#[Foo(
'a',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
)]
$foo,
) {
}
XX, $printer->printFunction($function));
3 changes: 2 additions & 1 deletion tests/PhpGenerator/expected/ClassType.attributes.expect
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Example
*/
#[ExampleAttribute]
public function getHandle(
#[ExampleAttribute, WithArguments(123)] $mode,
#[ExampleAttribute, WithArguments(123)]
$mode,
) {
}
}
3 changes: 2 additions & 1 deletion tests/PhpGenerator/expected/ClassType.from.expect
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class Class9
*/
#[ExampleAttribute]
public function getHandle(
#[WithArguments(123)] $mode,
#[WithArguments(123)]
$mode,
) {
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpGenerator/expected/ClassType.promotion.expect
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Example
$a,
public $b,
/** promo */
#[Example] private string $c,
#[Example]
private string $c,
public readonly Draft $d = new Draft(10),
) {
}
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpGenerator/expected/Extractor.classes.expect
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ class Class9
*/
#[ExampleAttribute]
public function getHandle(
#[WithArguments(123)] $mode,
#[WithArguments(123)]
$mode,
) {
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpGenerator/expected/PhpNamespace.expect
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class A implements A, C

#[A]
public function test(
#[\Bar\D] C $a,
#[\Bar\D]
C $a,
self $b,
parent $c,
array $d,
Expand Down
6 changes: 4 additions & 2 deletions tests/PhpGenerator/expected/Printer.class-alt.expect
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ final class Example extends ParentClass implements IExample


public function multi(
#[Foo] $foo,
#[Foo]
$foo,
) {
}



public function multiType(
#[Foo] $foo,
#[Foo]
$foo,
): array {
}
}
6 changes: 4 additions & 2 deletions tests/PhpGenerator/expected/Printer.class.expect
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ final class Example extends ParentClass implements IExample


public function multi(
#[Foo] $foo,
#[Foo]
$foo,
) {
}


public function multiType(
#[Foo] $foo,
#[Foo]
$foo,
): array
{
}
Expand Down

0 comments on commit 8853465

Please sign in to comment.