From b135071d8da108445e4df2fc6a75522b23c0237d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 7 Mar 2024 23:52:24 +0100 Subject: [PATCH] PsrPrinter: opening bracket on the correct line [Closes #155] --- src/PhpGenerator/Printer.php | 10 ++++++++-- src/PhpGenerator/PsrPrinter.php | 6 ++++++ tests/PhpGenerator/PsrPrinter.phpt | 5 ++++- tests/PhpGenerator/expected/PsrPrinter.class.expect | 6 ++++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 8a17f049..1d9528ae 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -48,7 +48,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace $params = $this->printParameters($function, strlen($line) + strlen($returnType) + 2); // 2 = parentheses $body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace); $body = ltrim(rtrim(Strings::normalize($body)) . "\n"); - $braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType); + $braceOnNextLine = $this->isBraceOnNextLine(str_contains($params, "\n"), (bool) $returnType); return $this->printDocComment($function) . $this->printAttributes($function->getAttributes()) @@ -119,7 +119,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo $params = $this->printParameters($method, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2); $body = Helpers::simplifyTaggedNames($method->getBody(), $this->namespace); $body = ltrim(rtrim(Strings::normalize($body)) . "\n"); - $braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType); + $braceOnNextLine = $this->isBraceOnNextLine(str_contains($params, "\n"), (bool) $returnType); return $this->printDocComment($method) . $this->printAttributes($method->getAttributes()) @@ -466,4 +466,10 @@ private function joinProperties(array $props): string ? implode(str_repeat("\n", $this->linesBetweenProperties), $props) : preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $props)); } + + + protected function isBraceOnNextLine(bool $multiLine, bool $hasReturnType): bool + { + return $this->bracesOnNextLine && (!$multiLine || $hasReturnType); + } } diff --git a/src/PhpGenerator/PsrPrinter.php b/src/PhpGenerator/PsrPrinter.php index ab62c174..f1b3b37b 100644 --- a/src/PhpGenerator/PsrPrinter.php +++ b/src/PhpGenerator/PsrPrinter.php @@ -18,4 +18,10 @@ final class PsrPrinter extends Printer public string $indentation = ' '; public int $linesBetweenMethods = 1; public int $linesBetweenUseTypes = 1; + + + protected function isBraceOnNextLine(bool $multiLine, bool $hasReturnType): bool + { + return !$multiLine; + } } diff --git a/tests/PhpGenerator/PsrPrinter.phpt b/tests/PhpGenerator/PsrPrinter.phpt index a8040bb0..a11e16ef 100644 --- a/tests/PhpGenerator/PsrPrinter.phpt +++ b/tests/PhpGenerator/PsrPrinter.phpt @@ -47,7 +47,10 @@ $class->addMethod('first') ->addParameter('var') ->setType('stdClass'); -$class->addMethod('second'); +$class->addMethod('braces1') + ->setReturnType('stdClass') + ->addParameter('var') + ->addAttribute('attr'); sameFile(__DIR__ . '/expected/PsrPrinter.class.expect', $printer->printClass($class)); diff --git a/tests/PhpGenerator/expected/PsrPrinter.class.expect b/tests/PhpGenerator/expected/PsrPrinter.class.expect index 5b57db98..f4e5e464 100644 --- a/tests/PhpGenerator/expected/PsrPrinter.class.expect +++ b/tests/PhpGenerator/expected/PsrPrinter.class.expect @@ -54,7 +54,9 @@ final class Example extends ParentClass implements IExample ]; } - public function second() - { + public function braces1( + #[attr] + $var, + ): stdClass { } }