method parameter default value rendering

This commit is contained in:
Gautier DELEGLISE
2024-10-26 23:49:53 +02:00
parent 60741fe387
commit fde2b5916d
2 changed files with 47 additions and 1 deletions
+8 -1
View File
@@ -261,10 +261,17 @@ final class Method extends BaseTag implements Factory\StaticMethod
{ {
$arguments = []; $arguments = [];
foreach ($this->parameters as $parameter) { 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() . ' ' . $arguments[] = $parameter->getType() . ' ' .
($parameter->isReference() ? '&' : '') . ($parameter->isReference() ? '&' : '') .
($parameter->isVariadic() ? '...' : '') . ($parameter->isVariadic() ? '...' : '') .
'$' . $parameter->getName(); '$' . $parameter->getName() .
$parameterDefaultValueStr ?? '';
} }
$argumentStr = '(' . implode(', ', $arguments) . ')'; $argumentStr = '(' . implode(', ', $arguments) . ')';
+39
View File
@@ -19,8 +19,10 @@ use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\Fqsen; use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\Array_; use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Boolean;
use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Float_;
use phpDocumentor\Reflection\Types\Integer; use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_; use phpDocumentor\Reflection\Types\Mixed_;
use phpDocumentor\Reflection\Types\Object_; use phpDocumentor\Reflection\Types\Object_;
@@ -698,4 +700,41 @@ class MethodTest extends TestCase
$this->assertSame($description, $fixture->getDescription()); $this->assertSame($description, $fixture->getDescription());
$this->assertTrue($fixture->returnsReference()); $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()
);
}
} }