Skip to content

Commit bf3b24b

Browse files
jonsadg
authored andcommitted
Printer: added $singleParameterOnOneLine (#132)
1 parent 210059d commit bf3b24b

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ class MyPrinter extends Nette\PhpGenerator\Printer
504504
public int $linesBetweenUseTypes = 0;
505505
/** position of the opening brace for functions and methods */
506506
public bool $bracesOnNextLine = true;
507+
/** place one parameter in one line, even if it has an attribute or is promoted */
508+
public bool $singleParameterOnOneLine = false;
507509
/** separator between the right parenthesis and return type of functions and methods */
508510
public string $returnTypeColon = ': ';
509511
}

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Printer
2727
public int $linesBetweenUseTypes = 0;
2828
public string $returnTypeColon = ': ';
2929
public bool $bracesOnNextLine = true;
30+
public bool $singleParameterOnOneLine = false;
3031
protected ?PhpNamespace $namespace = null;
3132
protected ?Dumper $dumper;
3233
private bool $resolveTypes = true;
@@ -332,7 +333,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
332333
$special = $special || $param instanceof PromotedParameter || $param->getAttributes();
333334
}
334335

335-
if (!$special) {
336+
if (!$special || ($this->singleParameterOnOneLine && count($function->getParameters()) === 1)) {
336337
$line = $this->formatParameters($function, false);
337338
if (!str_contains($line, "\n") && strlen($line) + $column <= $this->wrapLength) {
338339
return $line;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Nette\PhpGenerator\Literal;
6+
use Nette\PhpGenerator\Printer;
7+
use Tester\Assert;
8+
9+
require __DIR__ . '/../bootstrap.php';
10+
11+
12+
$printer = new Printer;
13+
$printer->singleParameterOnOneLine = true;
14+
15+
16+
$function = new Nette\PhpGenerator\GlobalFunction('singleFunction');
17+
$function
18+
->setReturnType('array')
19+
->addParameter('foo')
20+
->addAttribute('Foo');
21+
22+
Assert::match(<<<'XX'
23+
function singleFunction(#[Foo] $foo): array
24+
{
25+
}
26+
27+
XX, $printer->printFunction($function));
28+
29+
30+
$method = new Nette\PhpGenerator\Method('singleMethod');
31+
$method
32+
->setPublic()
33+
->setReturnType('array')
34+
->addParameter('foo')
35+
->addAttribute('Foo');
36+
37+
Assert::match(<<<'XX'
38+
public function singleMethod(#[Foo] $foo): array
39+
{
40+
}
41+
42+
XX, $printer->printMethod($method));
43+
44+
45+
$method = new Nette\PhpGenerator\Method('singleMethod');
46+
$method
47+
->setPublic()
48+
->setReturnType('array')
49+
->addPromotedParameter('foo')
50+
->setPublic();
51+
52+
Assert::match(<<<'XX'
53+
public function singleMethod(public $foo): array
54+
{
55+
}
56+
57+
XX, $printer->printMethod($method));
58+
59+
60+
$method = new Nette\PhpGenerator\Method('singleMethod');
61+
$method
62+
->setPublic()
63+
->setReturnType('array')
64+
->addPromotedParameter('looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong')
65+
->setPublic();
66+
67+
Assert::match(<<<'XX'
68+
public function singleMethod(
69+
public $looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
70+
): array
71+
{
72+
}
73+
74+
XX, $printer->printMethod($method));
75+
76+
77+
$method = new Nette\PhpGenerator\Method('singleMethod');
78+
$method->addParameter('foo')
79+
->addAttribute('Foo', [new Literal("'\n'")]);
80+
81+
Assert::match(<<<'XX'
82+
function singleMethod(
83+
#[Foo('
84+
')]
85+
$foo,
86+
) {
87+
}
88+
XX, $printer->printMethod($method));

0 commit comments

Comments
 (0)