From 290bbb7d328f6942a79f95fa3e1bdd0372b7d043 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 29 Aug 2023 18:41:23 +0200 Subject: [PATCH] Parameters can have comments --- src/PhpGenerator/Parameter.php | 1 + src/PhpGenerator/Printer.php | 12 +++++------- src/PhpGenerator/PromotedParameter.php | 1 - tests/PhpGenerator/ClassType.attributes.phpt | 1 + tests/PhpGenerator/ClassType.phpt | 3 ++- .../expected/ClassType.attributes.expect | 1 + tests/PhpGenerator/expected/ClassType.expect | 7 ++++++- tests/PhpGenerator/expected/Extractor.classes.expect | 11 +++++++++-- tests/PhpGenerator/fixtures/classes.php | 2 +- 9 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/PhpGenerator/Parameter.php b/src/PhpGenerator/Parameter.php index 68397aa9..94cd6710 100644 --- a/src/PhpGenerator/Parameter.php +++ b/src/PhpGenerator/Parameter.php @@ -23,6 +23,7 @@ class Parameter use Nette\SmartObject; use Traits\NameAware; use Traits\AttributeAware; + use Traits\CommentAware; private bool $reference = false; private ?string $type = null; diff --git a/src/PhpGenerator/Printer.php b/src/PhpGenerator/Printer.php index 1c5ee6bf..19087961 100644 --- a/src/PhpGenerator/Printer.php +++ b/src/PhpGenerator/Printer.php @@ -331,7 +331,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int $special = false; foreach ($function->getParameters() as $param) { $param->validate(); - $special = $special || $param instanceof PromotedParameter || $param->getAttributes(); + $special = $special || $param instanceof PromotedParameter || $param->getAttributes() || $param->getComment(); } if (!$special || ($this->singleParameterOnOneLine && count($function->getParameters()) === 1)) { @@ -352,15 +352,13 @@ private function formatParameters(Closure|GlobalFunction|Method $function, bool foreach ($params as $param) { $variadic = $function->isVariadic() && $param === end($params); - $promoted = $param instanceof PromotedParameter ? $param : null; $attrs = $this->printAttributes($param->getAttributes(), inline: true); $res .= - ($promoted ? $this->printDocComment($promoted) : '') + $this->printDocComment($param) . ($attrs ? ($multiline ? substr($attrs, 0, -1) . "\n" : $attrs) : '') - . ($promoted ? - ($promoted->getVisibility() ?: 'public') - . ($promoted->isReadOnly() && $param->getType() ? ' readonly' : '') - . ' ' : '') + . ($param instanceof PromotedParameter + ? ($param->getVisibility() ?: 'public') . ($param->isReadOnly() && $param->getType() ? ' readonly' : '') . ' ' + : '') . ltrim($this->printType($param->getType(), $param->isNullable()) . ' ') . ($param->isReference() ? '&' : '') . ($variadic ? '...' : '') diff --git a/src/PhpGenerator/PromotedParameter.php b/src/PhpGenerator/PromotedParameter.php index 65851db2..f9ae4efe 100644 --- a/src/PhpGenerator/PromotedParameter.php +++ b/src/PhpGenerator/PromotedParameter.php @@ -18,7 +18,6 @@ final class PromotedParameter extends Parameter { use Traits\VisibilityAware; - use Traits\CommentAware; private bool $readOnly = false; diff --git a/tests/PhpGenerator/ClassType.attributes.phpt b/tests/PhpGenerator/ClassType.attributes.phpt index 1ea41b09..b6ea2a00 100644 --- a/tests/PhpGenerator/ClassType.attributes.phpt +++ b/tests/PhpGenerator/ClassType.attributes.phpt @@ -34,6 +34,7 @@ $method = $class->addMethod('getHandle') ->addAttribute('ExampleAttribute'); $method->addParameter('mode') + ->addComment('comment') ->addAttribute('ExampleAttribute') ->addAttribute('WithArguments', [123]); diff --git a/tests/PhpGenerator/ClassType.phpt b/tests/PhpGenerator/ClassType.phpt index 2c87f0ca..b012a42a 100644 --- a/tests/PhpGenerator/ClassType.phpt +++ b/tests/PhpGenerator/ClassType.phpt @@ -129,7 +129,8 @@ Assert::same($p, $method->getParameter('foo')); $method->removeParameter('foo'); Assert::false($method->hasParameter('foo')); -$method->addParameter('item'); +$method->addParameter('item') + ->addComment('comment'); $method->addParameter('res', null) ->setReference() diff --git a/tests/PhpGenerator/expected/ClassType.attributes.expect b/tests/PhpGenerator/expected/ClassType.attributes.expect index 9ed82714..46ee3389 100644 --- a/tests/PhpGenerator/expected/ClassType.attributes.expect +++ b/tests/PhpGenerator/expected/ClassType.attributes.expect @@ -21,6 +21,7 @@ class Example */ #[ExampleAttribute] public function getHandle( + /** comment */ #[ExampleAttribute, WithArguments(123)] $mode, ) { diff --git a/tests/PhpGenerator/expected/ClassType.expect b/tests/PhpGenerator/expected/ClassType.expect index ccd736b2..0c64edb0 100644 --- a/tests/PhpGenerator/expected/ClassType.expect +++ b/tests/PhpGenerator/expected/ClassType.expect @@ -48,5 +48,10 @@ abstract class Example extends ParentClass implements IExample, IOne } - abstract public function show($item, array|null &$res = null, stdClass|string|null $bar = null); + abstract public function show( + /** comment */ + $item, + array|null &$res = null, + stdClass|string|null $bar = null, + ); } diff --git a/tests/PhpGenerator/expected/Extractor.classes.expect b/tests/PhpGenerator/expected/Extractor.classes.expect index 3397988c..c37fe1f4 100644 --- a/tests/PhpGenerator/expected/Extractor.classes.expect +++ b/tests/PhpGenerator/expected/Extractor.classes.expect @@ -60,8 +60,15 @@ class Class2 extends Class1 implements Interface2 * Func3 * @return Class1 */ - private function &func3(array $a, Class2 $b, Unknown $c, \Xyz\Unknown $d, ?callable $e, $f) - { + private function &func3( + /** foo */ + array $a, + Class2 $b, + Unknown $c, + \Xyz\Unknown $d, + ?callable $e, + $f, + ) { } diff --git a/tests/PhpGenerator/fixtures/classes.php b/tests/PhpGenerator/fixtures/classes.php index ae7ba49e..3a58dba7 100644 --- a/tests/PhpGenerator/fixtures/classes.php +++ b/tests/PhpGenerator/fixtures/classes.php @@ -67,7 +67,7 @@ class Class2 extends Class1 implements Interface2 * Func3 * @return Class1 */ - private function &func3(array $a, Class2 $b, \Abc\Unknown $c, \Xyz\Unknown $d, ?callable $e, $f) + private function &func3(/** foo */array $a, Class2 $b, \Abc\Unknown $c, \Xyz\Unknown $d, ?callable $e, $f) { }