Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 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 @@ -329,6 +330,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
$params = [];
$list = $function->getParameters();
$multiline = false;
$single = $this->singleParameterOnOneLine && count($list) === 1;

foreach ($list as $param) {
$param->validate();
Expand All @@ -348,7 +350,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
. '$' . $param->getName()
. ($param->hasDefaultValue() && !$variadic ? ' = ' . $this->dump($param->getDefaultValue()) : '');

$multiline = $multiline || $promoted || $attrs;
$multiline = $multiline || (!$single && ($promoted || $attrs));
}

$line = implode(', ', $params);
Expand Down
56 changes: 56 additions & 0 deletions tests/PhpGenerator/Printer.single.parameter.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

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));