Skip to content

Commit 290bbb7

Browse files
committed
Parameters can have comments
1 parent df22ef1 commit 290bbb7

File tree

9 files changed

+26
-13
lines changed

9 files changed

+26
-13
lines changed

src/PhpGenerator/Parameter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Parameter
2323
use Nette\SmartObject;
2424
use Traits\NameAware;
2525
use Traits\AttributeAware;
26+
use Traits\CommentAware;
2627

2728
private bool $reference = false;
2829
private ?string $type = null;

src/PhpGenerator/Printer.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected function printParameters(Closure|GlobalFunction|Method $function, int
331331
$special = false;
332332
foreach ($function->getParameters() as $param) {
333333
$param->validate();
334-
$special = $special || $param instanceof PromotedParameter || $param->getAttributes();
334+
$special = $special || $param instanceof PromotedParameter || $param->getAttributes() || $param->getComment();
335335
}
336336

337337
if (!$special || ($this->singleParameterOnOneLine && count($function->getParameters()) === 1)) {
@@ -352,15 +352,13 @@ private function formatParameters(Closure|GlobalFunction|Method $function, bool
352352

353353
foreach ($params as $param) {
354354
$variadic = $function->isVariadic() && $param === end($params);
355-
$promoted = $param instanceof PromotedParameter ? $param : null;
356355
$attrs = $this->printAttributes($param->getAttributes(), inline: true);
357356
$res .=
358-
($promoted ? $this->printDocComment($promoted) : '')
357+
$this->printDocComment($param)
359358
. ($attrs ? ($multiline ? substr($attrs, 0, -1) . "\n" : $attrs) : '')
360-
. ($promoted ?
361-
($promoted->getVisibility() ?: 'public')
362-
. ($promoted->isReadOnly() && $param->getType() ? ' readonly' : '')
363-
. ' ' : '')
359+
. ($param instanceof PromotedParameter
360+
? ($param->getVisibility() ?: 'public') . ($param->isReadOnly() && $param->getType() ? ' readonly' : '') . ' '
361+
: '')
364362
. ltrim($this->printType($param->getType(), $param->isNullable()) . ' ')
365363
. ($param->isReference() ? '&' : '')
366364
. ($variadic ? '...' : '')

src/PhpGenerator/PromotedParameter.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
final class PromotedParameter extends Parameter
1919
{
2020
use Traits\VisibilityAware;
21-
use Traits\CommentAware;
2221

2322
private bool $readOnly = false;
2423

tests/PhpGenerator/ClassType.attributes.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ $method = $class->addMethod('getHandle')
3434
->addAttribute('ExampleAttribute');
3535

3636
$method->addParameter('mode')
37+
->addComment('comment')
3738
->addAttribute('ExampleAttribute')
3839
->addAttribute('WithArguments', [123]);
3940

tests/PhpGenerator/ClassType.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ Assert::same($p, $method->getParameter('foo'));
129129
$method->removeParameter('foo');
130130
Assert::false($method->hasParameter('foo'));
131131

132-
$method->addParameter('item');
132+
$method->addParameter('item')
133+
->addComment('comment');
133134

134135
$method->addParameter('res', null)
135136
->setReference()

tests/PhpGenerator/expected/ClassType.attributes.expect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Example
2121
*/
2222
#[ExampleAttribute]
2323
public function getHandle(
24+
/** comment */
2425
#[ExampleAttribute, WithArguments(123)]
2526
$mode,
2627
) {

tests/PhpGenerator/expected/ClassType.expect

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@ abstract class Example extends ParentClass implements IExample, IOne
4848
}
4949

5050

51-
abstract public function show($item, array|null &$res = null, stdClass|string|null $bar = null);
51+
abstract public function show(
52+
/** comment */
53+
$item,
54+
array|null &$res = null,
55+
stdClass|string|null $bar = null,
56+
);
5257
}

tests/PhpGenerator/expected/Extractor.classes.expect

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ class Class2 extends Class1 implements Interface2
6060
* Func3
6161
* @return Class1
6262
*/
63-
private function &func3(array $a, Class2 $b, Unknown $c, \Xyz\Unknown $d, ?callable $e, $f)
64-
{
63+
private function &func3(
64+
/** foo */
65+
array $a,
66+
Class2 $b,
67+
Unknown $c,
68+
\Xyz\Unknown $d,
69+
?callable $e,
70+
$f,
71+
) {
6572
}
6673

6774

tests/PhpGenerator/fixtures/classes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Class2 extends Class1 implements Interface2
6767
* Func3
6868
* @return Class1
6969
*/
70-
private function &func3(array $a, Class2 $b, \Abc\Unknown $c, \Xyz\Unknown $d, ?callable $e, $f)
70+
private function &func3(/** foo */array $a, Class2 $b, \Abc\Unknown $c, \Xyz\Unknown $d, ?callable $e, $f)
7171
{
7272
}
7373

0 commit comments

Comments
 (0)