Skip to content

Commit b135071

Browse files
committed
PsrPrinter: opening bracket on the correct line [Closes #155]
1 parent af74f21 commit b135071

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

src/PhpGenerator/Printer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
4848
$params = $this->printParameters($function, strlen($line) + strlen($returnType) + 2); // 2 = parentheses
4949
$body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
5050
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
51-
$braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType);
51+
$braceOnNextLine = $this->isBraceOnNextLine(str_contains($params, "\n"), (bool) $returnType);
5252

5353
return $this->printDocComment($function)
5454
. $this->printAttributes($function->getAttributes())
@@ -119,7 +119,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
119119
$params = $this->printParameters($method, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2);
120120
$body = Helpers::simplifyTaggedNames($method->getBody(), $this->namespace);
121121
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
122-
$braceOnNextLine = $this->bracesOnNextLine && (!str_contains($params, "\n") || $returnType);
122+
$braceOnNextLine = $this->isBraceOnNextLine(str_contains($params, "\n"), (bool) $returnType);
123123

124124
return $this->printDocComment($method)
125125
. $this->printAttributes($method->getAttributes())
@@ -466,4 +466,10 @@ private function joinProperties(array $props): string
466466
? implode(str_repeat("\n", $this->linesBetweenProperties), $props)
467467
: preg_replace('#^(\w.*\n)\n(?=\w.*;)#m', '$1', implode("\n", $props));
468468
}
469+
470+
471+
protected function isBraceOnNextLine(bool $multiLine, bool $hasReturnType): bool
472+
{
473+
return $this->bracesOnNextLine && (!$multiLine || $hasReturnType);
474+
}
469475
}

src/PhpGenerator/PsrPrinter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ final class PsrPrinter extends Printer
1818
public string $indentation = ' ';
1919
public int $linesBetweenMethods = 1;
2020
public int $linesBetweenUseTypes = 1;
21+
22+
23+
protected function isBraceOnNextLine(bool $multiLine, bool $hasReturnType): bool
24+
{
25+
return !$multiLine;
26+
}
2127
}

tests/PhpGenerator/PsrPrinter.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ $class->addMethod('first')
4747
->addParameter('var')
4848
->setType('stdClass');
4949

50-
$class->addMethod('second');
50+
$class->addMethod('braces1')
51+
->setReturnType('stdClass')
52+
->addParameter('var')
53+
->addAttribute('attr');
5154

5255

5356
sameFile(__DIR__ . '/expected/PsrPrinter.class.expect', $printer->printClass($class));

tests/PhpGenerator/expected/PsrPrinter.class.expect

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ final class Example extends ParentClass implements IExample
5454
];
5555
}
5656

57-
public function second()
58-
{
57+
public function braces1(
58+
#[attr]
59+
$var,
60+
): stdClass {
5961
}
6062
}

0 commit comments

Comments
 (0)