Skip to content

Commit f19b797

Browse files
committed
Printer: added $bracesOnNextLine [Closes #112]
1 parent 4fec8f7 commit f19b797

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ class MyPrinter extends Nette\PhpGenerator\Printer
487487
public string $indentation = "\t";
488488
public int $linesBetweenProperties = 0;
489489
public int $linesBetweenMethods = 2;
490+
public bool $bracesOnNextLine = true;
490491
public string $returnTypeColon = ': ';
491492
}
492493
```

src/PhpGenerator/Printer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Printer
2525
public int $linesBetweenProperties = 0;
2626
public int $linesBetweenMethods = 2;
2727
public string $returnTypeColon = ': ';
28+
public bool $bracesOnNextLine = true;
2829
protected ?PhpNamespace $namespace = null;
2930
protected ?Dumper $dumper;
3031
private bool $resolveTypes = true;
@@ -51,7 +52,8 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
5152
. $line
5253
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
5354
. $returnType
54-
. "\n{\n" . $this->indent($body) . "}\n";
55+
. ($this->bracesOnNextLine ? "\n" : ' ')
56+
. "{\n" . $this->indent($body) . "}\n";
5557
}
5658

5759

@@ -114,6 +116,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
114116
$params = $this->printParameters($method, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2);
115117
$body = Helpers::simplifyTaggedNames($method->getBody(), $this->namespace);
116118
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
119+
$braceOnNextLine = $this->bracesOnNextLine && !str_contains($params, "\n");
117120

118121
return Helpers::formatDocComment($method->getComment() . "\n")
119122
. self::printAttributes($method->getAttributes())
@@ -122,7 +125,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
122125
. $returnType
123126
. ($method->isAbstract() || $isInterface
124127
? ";\n"
125-
: (str_contains($params, "\n") ? ' ' : "\n") . "{\n" . $this->indent($body) . "}\n");
128+
: ($braceOnNextLine ? "\n" : ' ') . "{\n" . $this->indent($body) . "}\n");
126129
}
127130

128131

tests/PhpGenerator/Printer.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ sameFile(__DIR__ . '/expected/Printer.class.expect', $printer->printClass($class
5656
sameFile(__DIR__ . '/expected/Printer.method.expect', $printer->printMethod($class->getMethod('first')));
5757

5858

59-
Assert::with($printer, function () {
60-
$this->linesBetweenProperties = 1;
61-
$this->linesBetweenMethods = 3;
62-
});
59+
$printer->linesBetweenProperties = 1;
60+
$printer->linesBetweenMethods = 3;
61+
$printer->bracesOnNextLine = false;
6362
sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($class));
6463

6564

6665

66+
$printer = new Printer;
6767
$function = new Nette\PhpGenerator\GlobalFunction('func');
6868
$function
6969
->setReturnType('stdClass')

tests/PhpGenerator/expected/Printer.class-alt.expect

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ final class Example extends ParentClass implements IExample
4444
/**
4545
* @return resource
4646
*/
47-
final public function first(stdClass $var): stdClass
48-
{
47+
final public function first(stdClass $var): stdClass {
4948
func();
5049
return [
5150
'aaaaaaaaaaaa' => 1,
@@ -59,7 +58,6 @@ final class Example extends ParentClass implements IExample
5958

6059

6160

62-
public function second()
63-
{
61+
public function second() {
6462
}
6563
}

0 commit comments

Comments
 (0)