diff --git a/src/DocBlock/Tags/Method.php b/src/DocBlock/Tags/Method.php index 471ebe9..ceefa6a 100644 --- a/src/DocBlock/Tags/Method.php +++ b/src/DocBlock/Tags/Method.php @@ -261,10 +261,17 @@ final class Method extends BaseTag implements Factory\StaticMethod { $arguments = []; foreach ($this->parameters as $parameter) { + if ($parameter->getDefaultValue() !== null) { + $parameterDefaultValueStr = $parameter->getDefaultValue(); + settype($parameterDefaultValueStr, (string)$parameter->getType()); + $parameterDefaultValueStr = sprintf(' = %s', var_export($parameterDefaultValueStr, true)); + } + $arguments[] = $parameter->getType() . ' ' . ($parameter->isReference() ? '&' : '') . ($parameter->isVariadic() ? '...' : '') . - '$' . $parameter->getName(); + '$' . $parameter->getName() . + $parameterDefaultValueStr ?? ''; } $argumentStr = '(' . implode(', ', $arguments) . ')'; diff --git a/tests/unit/DocBlock/Tags/MethodTest.php b/tests/unit/DocBlock/Tags/MethodTest.php index dfc0032..ab33d3f 100644 --- a/tests/unit/DocBlock/Tags/MethodTest.php +++ b/tests/unit/DocBlock/Tags/MethodTest.php @@ -19,8 +19,10 @@ use phpDocumentor\Reflection\DocBlock\DescriptionFactory; use phpDocumentor\Reflection\Fqsen; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\Array_; +use phpDocumentor\Reflection\Types\Boolean; use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Context; +use phpDocumentor\Reflection\Types\Float_; use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\Mixed_; use phpDocumentor\Reflection\Types\Object_; @@ -698,4 +700,41 @@ class MethodTest extends TestCase $this->assertSame($description, $fixture->getDescription()); $this->assertTrue($fixture->returnsReference()); } + + /** + * @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct + * @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue() + * @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString + * @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter + * + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render + * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName + */ + public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(): void + { + $arguments = [ + ['name' => 'argument1', 'type' => new String_()], + ['name' => 'argument2', 'type' => new Object_()], + ]; + + $fixture = new Method( + 'myMethod', + $arguments, + new Void_(), + false, + null, + false, + [ + new MethodParameter('argument1', new String_(), false, false, '1'), + new MethodParameter('argument2', new Integer(), false, false, '1'), + new MethodParameter('argument3', new Boolean(), false, false, 'true'), + new MethodParameter('argument4', new Float_(), false, false, '1.23'), + ] + ); + + $this->assertSame( + '@method void myMethod(string $argument1 = \'1\', int $argument2 = 1, bool $argument3 = true, float $argument4 = 1.23)', + $fixture->render() + ); + } }