Skip to content

Commit

Permalink
Printer: added $singleParameterOnOneLine (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsa authored and dg committed Apr 26, 2023
1 parent 210059d commit bf3b24b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ class MyPrinter extends Nette\PhpGenerator\Printer
public int $linesBetweenUseTypes = 0;
/** position of the opening brace for functions and methods */
public bool $bracesOnNextLine = true;
/** place one parameter in one line, even if it has an attribute or is promoted */
public bool $singleParameterOnOneLine = false;
/** separator between the right parenthesis and return type of functions and methods */
public string $returnTypeColon = ': ';
}
Expand Down
3 changes: 2 additions & 1 deletion src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Printer
public int $linesBetweenUseTypes = 0;
public string $returnTypeColon = ': ';
public bool $bracesOnNextLine = true;
public bool $singleParameterOnOneLine = false;
protected ?PhpNamespace $namespace = null;
protected ?Dumper $dumper;
private bool $resolveTypes = true;
Expand Down Expand Up @@ -332,7 +333,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
$special = $special || $param instanceof PromotedParameter || $param->getAttributes();
}

if (!$special) {
if (!$special || ($this->singleParameterOnOneLine && count($function->getParameters()) === 1)) {
$line = $this->formatParameters($function, false);
if (!str_contains($line, "\n") && strlen($line) + $column <= $this->wrapLength) {
return $line;
Expand Down
88 changes: 88 additions & 0 deletions tests/PhpGenerator/Printer.single.parameter.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

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

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


$printer = new Printer;
$printer->singleParameterOnOneLine = true;


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

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

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


$method = new Nette\PhpGenerator\Method('singleMethod');
$method
->setPublic()
->setReturnType('array')
->addParameter('foo')
->addAttribute('Foo');

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

XX, $printer->printMethod($method));


$method = new Nette\PhpGenerator\Method('singleMethod');
$method
->setPublic()
->setReturnType('array')
->addPromotedParameter('foo')
->setPublic();

Assert::match(<<<'XX'
public function singleMethod(public $foo): array
{
}

XX, $printer->printMethod($method));


$method = new Nette\PhpGenerator\Method('singleMethod');
$method
->setPublic()
->setReturnType('array')
->addPromotedParameter('looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong')
->setPublic();

Assert::match(<<<'XX'
public function singleMethod(
public $looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong,
): array
{
}

XX, $printer->printMethod($method));


$method = new Nette\PhpGenerator\Method('singleMethod');
$method->addParameter('foo')
->addAttribute('Foo', [new Literal("'\n'")]);

Assert::match(<<<'XX'
function singleMethod(
#[Foo('
')]
$foo,
) {
}
XX, $printer->printMethod($method));

0 comments on commit bf3b24b

Please sign in to comment.