Skip to content

Commit

Permalink
PsrPrinter: opening bracket on the correct line [Closes #155]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 7, 2024
1 parent af74f21 commit b135071
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/PhpGenerator/PsrPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion tests/PhpGenerator/PsrPrinter.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
6 changes: 4 additions & 2 deletions tests/PhpGenerator/expected/PsrPrinter.class.expect
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ final class Example extends ParentClass implements IExample
];
}

public function second()
{
public function braces1(
#[attr]
$var,
): stdClass {
}
}

0 comments on commit b135071

Please sign in to comment.