Skip to content

Commit

Permalink
Printer: added $bracesOnNextLine [Closes #112]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 17, 2022
1 parent 4fec8f7 commit f19b797
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ': ';
}
```
Expand Down
7 changes: 5 additions & 2 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
}


Expand Down Expand Up @@ -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())
Expand All @@ -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");
}


Expand Down
8 changes: 4 additions & 4 deletions tests/PhpGenerator/Printer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 2 additions & 4 deletions tests/PhpGenerator/expected/Printer.class-alt.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -59,7 +58,6 @@ final class Example extends ParentClass implements IExample



public function second()
{
public function second() {
}
}

0 comments on commit f19b797

Please sign in to comment.