From cd40df575bf84cef0393c31b8493b7ecfc6e3a82 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 13 Mar 2023 15:29:58 +0100 Subject: [PATCH] Printer: promoted parameters / parameter with attributes are always multiline --- src/PhpGenerator/Printer.php | 7 ++++--- tests/PhpGenerator/expected/ClassType.attributes.expect | 5 +++-- tests/PhpGenerator/expected/ClassType.from.expect | 5 +++-- tests/PhpGenerator/expected/Extractor.classes.81.expect | 5 +++-- tests/PhpGenerator/expected/Extractor.classes.expect | 5 +++-- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 115a39be..b818b60d 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -324,7 +324,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int { $params = []; $list = $function->getParameters(); - $special = false; + $multiline = false; foreach ($list as $param) { $param->validate(); @@ -344,12 +344,13 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int . '$' . $param->getName() . ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : ''); - $special = $special || $promoted || $attrs; + $multiline = $multiline || $promoted || $attrs; } $line = implode(', ', $params); + $multiline = $multiline || count($params) > 1 && (strlen($line) + $column > $this->wrapLength); - return count($params) > 1 && ($special || strlen($line) + $column > $this->wrapLength) + return $multiline ? "(\n" . $this->indent(implode(",\n", $params)) . ",\n)" : "($line)"; } diff --git a/tests/PhpGenerator/expected/ClassType.attributes.expect b/tests/PhpGenerator/expected/ClassType.attributes.expect index b10df65e..57d6307a 100644 --- a/tests/PhpGenerator/expected/ClassType.attributes.expect +++ b/tests/PhpGenerator/expected/ClassType.attributes.expect @@ -20,7 +20,8 @@ class Example * Returns file handle. */ #[ExampleAttribute] - public function getHandle(#[ExampleAttribute, WithArguments(123)] $mode) - { + public function getHandle( + #[ExampleAttribute, WithArguments(123)] $mode, + ) { } } diff --git a/tests/PhpGenerator/expected/ClassType.from.expect b/tests/PhpGenerator/expected/ClassType.from.expect index b52d045e..f66a53a9 100644 --- a/tests/PhpGenerator/expected/ClassType.from.expect +++ b/tests/PhpGenerator/expected/ClassType.from.expect @@ -138,8 +138,9 @@ class Class9 * Returns file handle */ #[ExampleAttribute] - public function getHandle(#[WithArguments(123)] $mode) - { + public function getHandle( + #[WithArguments(123)] $mode, + ) { } } diff --git a/tests/PhpGenerator/expected/Extractor.classes.81.expect b/tests/PhpGenerator/expected/Extractor.classes.81.expect index 831e8e31..1ace4741 100644 --- a/tests/PhpGenerator/expected/Extractor.classes.81.expect +++ b/tests/PhpGenerator/expected/Extractor.classes.81.expect @@ -33,8 +33,9 @@ class Class12 private readonly string $bar; - public function __construct(private readonly string $foo) - { + public function __construct( + private readonly string $foo, + ) { $this->bar = "foobar"; } } diff --git a/tests/PhpGenerator/expected/Extractor.classes.expect b/tests/PhpGenerator/expected/Extractor.classes.expect index 2ee92a2a..25d94e84 100644 --- a/tests/PhpGenerator/expected/Extractor.classes.expect +++ b/tests/PhpGenerator/expected/Extractor.classes.expect @@ -148,8 +148,9 @@ class Class9 * Returns file handle */ #[ExampleAttribute] - public function getHandle(#[WithArguments(123)] $mode) - { + public function getHandle( + #[WithArguments(123)] $mode, + ) { } }