From bf3b24bddf4af8ae4bd373924916c8d62ed4a81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sandstr=C3=B6m?= Date: Thu, 30 Mar 2023 11:50:53 +0200 Subject: [PATCH] Printer: added $singleParameterOnOneLine (#132) --- readme.md | 2 + src/PhpGenerator/Printer.php | 3 +- .../Printer.single.parameter.phpt | 88 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/PhpGenerator/Printer.single.parameter.phpt diff --git a/readme.md b/readme.md index d6520a56..482a361a 100644 --- a/readme.md +++ b/readme.md @@ -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 = ': '; } diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index cb71b3e6..b3f380c8 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -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; @@ -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; diff --git a/tests/PhpGenerator/Printer.single.parameter.phpt b/tests/PhpGenerator/Printer.single.parameter.phpt new file mode 100644 index 00000000..0fa9fe39 --- /dev/null +++ b/tests/PhpGenerator/Printer.single.parameter.phpt @@ -0,0 +1,88 @@ +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));