Skip to content

Commit

Permalink
Printer: print brace on next line when method/function has typehint
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 13, 2023
1 parent cd40df5 commit 43d6101
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 20 deletions.
8 changes: 5 additions & 3 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
. ($function->getReturnReference() ? '&' : '')
. $function->getName();
$returnType = $this->printReturnType($function);
$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);

return $this->printDocComment($function)
. $this->printAttributes($function->getAttributes())
. $line
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
. $params
. $returnType
. ($this->bracesOnNextLine ? "\n" : ' ')
. ($braceOnNextLine ? "\n" : ' ')
. "{\n" . $this->indent($body) . "}\n";
}

Expand Down Expand Up @@ -117,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");
$braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType);

This comment has been minimized.

Copy link
@Jeroeny

Jeroeny May 22, 2023

Contributor

It would be nice if the PsrPrinter could still follow the PSR codestyle: https://www.php-fig.org/psr/psr-12/ (on a single line regardless of return type).
Or if that could be adjusted in an extended class.

This comment has been minimized.

Copy link
@dg

dg May 22, 2023

Author Member

Feel free to send a PR


return $this->printDocComment($method)
. $this->printAttributes($method->getAttributes())
Expand Down
55 changes: 55 additions & 0 deletions tests/PhpGenerator/Printer.function.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

use Nette\PhpGenerator\Printer;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


$printer = new Printer;
$function = new Nette\PhpGenerator\GlobalFunction('func');
$function
->setReturnType('stdClass')
->setBody("func(); \r\nreturn 123;")
->addParameter('var')
->setType('stdClass');

Assert::match(<<<'XX'
function func(stdClass $var): stdClass
{
func();
return 123;
}

XX, $printer->printFunction($function));


$function = new Nette\PhpGenerator\GlobalFunction('multi');
$function->addParameter('foo')
->addAttribute('Foo');

Assert::match(<<<'XX'
function multi(
#[Foo] $foo,
) {
}

XX, $printer->printFunction($function));


$function = new Nette\PhpGenerator\GlobalFunction('multiType');
$function
->setReturnType('array')
->addParameter('foo')
->addAttribute('Foo');

Assert::match(<<<'XX'
function multiType(
#[Foo] $foo,
): array
{
}

XX, $printer->printFunction($function));
20 changes: 9 additions & 11 deletions tests/PhpGenerator/Printer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ $class->addMethod('first')

$class->addMethod('second');

$method = $class->addMethod('multi')
->addParameter('foo')
->addAttribute('Foo');

$method = $class->addMethod('multiType')
->setReturnType('array')
->addParameter('foo')
->addAttribute('Foo');


sameFile(__DIR__ . '/expected/Printer.class.expect', $printer->printClass($class));
sameFile(__DIR__ . '/expected/Printer.method.expect', $printer->printMethod($class->getMethod('first')));
Expand All @@ -63,17 +72,6 @@ sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($c



$printer = new Printer;
$function = new Nette\PhpGenerator\GlobalFunction('func');
$function
->setReturnType('stdClass')
->setBody("func(); \r\nreturn 123;")
->addParameter('var')
->setType('stdClass');

sameFile(__DIR__ . '/expected/Printer.function.expect', $printer->printFunction($function));


$closure = new Nette\PhpGenerator\Closure;
$closure
->setReturnType('stdClass')
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpGenerator/expected/PhpNamespace.expect
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class A implements A, C
array $d,
?callable $e,
C|string $f,
): static|A {
): static|A
{
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PhpGenerator/expected/Printer.class-alt.expect
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ final class Example extends ParentClass implements IExample

public function second() {
}



public function multi(
#[Foo] $foo,
) {
}



public function multiType(
#[Foo] $foo,
): array {
}
}
13 changes: 13 additions & 0 deletions tests/PhpGenerator/expected/Printer.class.expect
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,17 @@ final class Example extends ParentClass implements IExample
public function second()
{
}


public function multi(
#[Foo] $foo,
) {
}


public function multiType(
#[Foo] $foo,
): array
{
}
}
5 changes: 0 additions & 5 deletions tests/PhpGenerator/expected/Printer.function.expect

This file was deleted.

0 comments on commit 43d6101

Please sign in to comment.