Skip to content

Commit

Permalink
Parameters can have comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 29, 2023
1 parent df22ef1 commit 290bbb7
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/PhpGenerator/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/PhpGenerator/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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 ? '...' : '')
Expand Down
1 change: 0 additions & 1 deletion src/PhpGenerator/PromotedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
final class PromotedParameter extends Parameter
{
use Traits\VisibilityAware;
use Traits\CommentAware;

private bool $readOnly = false;

Expand Down
1 change: 1 addition & 0 deletions tests/PhpGenerator/ClassType.attributes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $method = $class->addMethod('getHandle')
->addAttribute('ExampleAttribute');

$method->addParameter('mode')
->addComment('comment')
->addAttribute('ExampleAttribute')
->addAttribute('WithArguments', [123]);

Expand Down
3 changes: 2 additions & 1 deletion tests/PhpGenerator/ClassType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/PhpGenerator/expected/ClassType.attributes.expect
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Example
*/
#[ExampleAttribute]
public function getHandle(
/** comment */
#[ExampleAttribute, WithArguments(123)]
$mode,
) {
Expand Down
7 changes: 6 additions & 1 deletion tests/PhpGenerator/expected/ClassType.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
11 changes: 9 additions & 2 deletions tests/PhpGenerator/expected/Extractor.classes.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}


Expand Down
2 changes: 1 addition & 1 deletion tests/PhpGenerator/fixtures/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down

0 comments on commit 290bbb7

Please sign in to comment.