Skip to content

Commit fde2b59

Browse files
committed
✨ method parameter default value rendering
1 parent 60741fe commit fde2b59

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/DocBlock/Tags/Method.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,17 @@ public function __toString(): string
261261
{
262262
$arguments = [];
263263
foreach ($this->parameters as $parameter) {
264+
if ($parameter->getDefaultValue() !== null) {
265+
$parameterDefaultValueStr = $parameter->getDefaultValue();
266+
settype($parameterDefaultValueStr, (string)$parameter->getType());
267+
$parameterDefaultValueStr = sprintf(' = %s', var_export($parameterDefaultValueStr, true));
268+
}
269+
264270
$arguments[] = $parameter->getType() . ' ' .
265271
($parameter->isReference() ? '&' : '') .
266272
($parameter->isVariadic() ? '...' : '') .
267-
'$' . $parameter->getName();
273+
'$' . $parameter->getName() .
274+
$parameterDefaultValueStr ?? '';
268275
}
269276

270277
$argumentStr = '(' . implode(', ', $arguments) . ')';

tests/unit/DocBlock/Tags/MethodTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
use phpDocumentor\Reflection\Fqsen;
2020
use phpDocumentor\Reflection\TypeResolver;
2121
use phpDocumentor\Reflection\Types\Array_;
22+
use phpDocumentor\Reflection\Types\Boolean;
2223
use phpDocumentor\Reflection\Types\Compound;
2324
use phpDocumentor\Reflection\Types\Context;
25+
use phpDocumentor\Reflection\Types\Float_;
2426
use phpDocumentor\Reflection\Types\Integer;
2527
use phpDocumentor\Reflection\Types\Mixed_;
2628
use phpDocumentor\Reflection\Types\Object_;
@@ -698,4 +700,41 @@ public function testCreateWithReference(): void
698700
$this->assertSame($description, $fixture->getDescription());
699701
$this->assertTrue($fixture->returnsReference());
700702
}
703+
704+
/**
705+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct
706+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue()
707+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
708+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
709+
*
710+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
711+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
712+
*/
713+
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(): void
714+
{
715+
$arguments = [
716+
['name' => 'argument1', 'type' => new String_()],
717+
['name' => 'argument2', 'type' => new Object_()],
718+
];
719+
720+
$fixture = new Method(
721+
'myMethod',
722+
$arguments,
723+
new Void_(),
724+
false,
725+
null,
726+
false,
727+
[
728+
new MethodParameter('argument1', new String_(), false, false, '1'),
729+
new MethodParameter('argument2', new Integer(), false, false, '1'),
730+
new MethodParameter('argument3', new Boolean(), false, false, 'true'),
731+
new MethodParameter('argument4', new Float_(), false, false, '1.23'),
732+
]
733+
);
734+
735+
$this->assertSame(
736+
'@method void myMethod(string $argument1 = \'1\', int $argument2 = 1, bool $argument3 = true, float $argument4 = 1.23)',
737+
$fixture->render()
738+
);
739+
}
701740
}

0 commit comments

Comments
 (0)