From f19b7975c7c4d729be5b64fce7eb72f0d4aac6fc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 17 Jun 2022 14:19:45 +0200 Subject: [PATCH] Printer: added $bracesOnNextLine [Closes #112] --- readme.md | 1 + src/PhpGenerator/Printer.php | 7 +++++-- tests/PhpGenerator/Printer.phpt | 8 ++++---- tests/PhpGenerator/expected/Printer.class-alt.expect | 6 ++---- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index 05698af8..2724b7db 100644 --- a/readme.md +++ b/readme.md @@ -487,6 +487,7 @@ class MyPrinter extends Nette\PhpGenerator\Printer public string $indentation = "\t"; public int $linesBetweenProperties = 0; public int $linesBetweenMethods = 2; + public bool $bracesOnNextLine = true; public string $returnTypeColon = ': '; } ``` diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index ec185f2f..2021cee3 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -25,6 +25,7 @@ class Printer public int $linesBetweenProperties = 0; public int $linesBetweenMethods = 2; public string $returnTypeColon = ': '; + public bool $bracesOnNextLine = true; protected ?PhpNamespace $namespace = null; protected ?Dumper $dumper; private bool $resolveTypes = true; @@ -51,7 +52,8 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace . $line . $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses . $returnType - . "\n{\n" . $this->indent($body) . "}\n"; + . ($this->bracesOnNextLine ? "\n" : ' ') + . "{\n" . $this->indent($body) . "}\n"; } @@ -114,6 +116,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"); return Helpers::formatDocComment($method->getComment() . "\n") . self::printAttributes($method->getAttributes()) @@ -122,7 +125,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo . $returnType . ($method->isAbstract() || $isInterface ? ";\n" - : (str_contains($params, "\n") ? ' ' : "\n") . "{\n" . $this->indent($body) . "}\n"); + : ($braceOnNextLine ? "\n" : ' ') . "{\n" . $this->indent($body) . "}\n"); } diff --git a/tests/PhpGenerator/Printer.phpt b/tests/PhpGenerator/Printer.phpt index a4103927..5a8cd996 100644 --- a/tests/PhpGenerator/Printer.phpt +++ b/tests/PhpGenerator/Printer.phpt @@ -56,14 +56,14 @@ sameFile(__DIR__ . '/expected/Printer.class.expect', $printer->printClass($class sameFile(__DIR__ . '/expected/Printer.method.expect', $printer->printMethod($class->getMethod('first'))); -Assert::with($printer, function () { - $this->linesBetweenProperties = 1; - $this->linesBetweenMethods = 3; -}); +$printer->linesBetweenProperties = 1; +$printer->linesBetweenMethods = 3; +$printer->bracesOnNextLine = false; sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($class)); +$printer = new Printer; $function = new Nette\PhpGenerator\GlobalFunction('func'); $function ->setReturnType('stdClass') diff --git a/tests/PhpGenerator/expected/Printer.class-alt.expect b/tests/PhpGenerator/expected/Printer.class-alt.expect index 17defce8..ee340d14 100644 --- a/tests/PhpGenerator/expected/Printer.class-alt.expect +++ b/tests/PhpGenerator/expected/Printer.class-alt.expect @@ -44,8 +44,7 @@ final class Example extends ParentClass implements IExample /** * @return resource */ - final public function first(stdClass $var): stdClass - { + final public function first(stdClass $var): stdClass { func(); return [ 'aaaaaaaaaaaa' => 1, @@ -59,7 +58,6 @@ final class Example extends ParentClass implements IExample - public function second() - { + public function second() { } }