Skip to content

Commit c7b0067

Browse files
committed
Support variadic method param doc block - close #80
1 parent 74000d2 commit c7b0067

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/Code/DocBlock/Tag/ParamTag.php

+24-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
*/
2121
final class ParamTag extends AbstractTypeableTag
2222
{
23-
/**
24-
* @var string
25-
*/
2623
protected string $variableName;
2724

25+
private bool $variadic = false;
26+
2827
/**
2928
* ParamTag constructor.
3029
* @param string|null $variableName
@@ -67,14 +66,35 @@ public function getVariableName(): string
6766
return $this->variableName;
6867
}
6968

69+
/**
70+
* @param bool $variadic
71+
*
72+
* @return ParamTag
73+
*/
74+
public function setVariadic($variadic): self
75+
{
76+
$this->variadic = (bool) $variadic;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* @return bool
83+
*/
84+
public function getVariadic(): bool
85+
{
86+
return $this->variadic;
87+
}
88+
7089
/**
7190
* @return string
7291
*/
7392
public function generate(): string
7493
{
7594
$output = '@param'
7695
. (! empty($this->types) ? ' ' . $this->getTypesAsString() : '')
77-
. (! empty($this->variableName) ? ' $' . $this->variableName : '')
96+
. ($this->variadic ? ' ...' : ' ')
97+
. (! empty($this->variableName) ? '$' . $this->variableName : '')
7898
. (! empty($this->description) ? ' ' . $this->description : '');
7999

80100
return $output;

src/Code/MethodGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private function generateAttributes(): array
265265
if ($typeHint = $parameter->getTypeDocBlockHint()) {
266266
$types = $typeHint;
267267
}
268-
$docBlock->addTag(new ParamTag($parameter->getName(), $types));
268+
$docBlock->addTag((new ParamTag($parameter->getName(), $types))->setVariadic($parameter->getVariadic()));
269269
}
270270

271271
$returnType = null;

tests/Code/MethodGeneratorTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ public function getItems() : array;
8282
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()]));
8383
}
8484

85+
/**
86+
* @test
87+
*/
88+
public function it_generates_method_with_variadic_doc_block(): void
89+
{
90+
$method = new MethodGenerator('setItems');
91+
$method->setParameter((new ParameterGenerator('items'))->setVariadic(true)->setTypeDocBlockHint('mixed'));
92+
$method->setReturnType('void');
93+
$method->setDocBlockComment('');
94+
95+
$expectedOutput = <<<'EOF'
96+
<?php
97+
98+
/**
99+
* @param mixed ...$items
100+
*/
101+
public function setItems(...$items) : void;
102+
EOF;
103+
104+
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()]));
105+
}
106+
107+
/**
108+
* @test
109+
*/
110+
public function it_generates_method_with_mixed_variadic_without_doc_block(): void
111+
{
112+
$method = new MethodGenerator('setItems');
113+
$method->setParameter((new ParameterGenerator('items'))->setVariadic(true)->setType('mixed'));
114+
$method->setReturnType('void');
115+
116+
$expectedOutput = <<<'EOF'
117+
<?php
118+
119+
public function setItems(mixed ...$items) : void;
120+
EOF;
121+
122+
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()]));
123+
}
124+
85125
/**
86126
* @test
87127
*/

0 commit comments

Comments
 (0)