Skip to content

✨ method parameter default value rendering #374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 4, 2024
10 changes: 9 additions & 1 deletion src/DocBlock/Tags/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,18 @@ public function __toString(): string
{
$arguments = [];
foreach ($this->parameters as $parameter) {
$parameterDefaultValueStr = null;
if ($parameter->getDefaultValue() !== null) {
$parameterDefaultValueStr = $parameter->getDefaultValue();
settype($parameterDefaultValueStr, (string)$parameter->getType());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be correct to me. Default values can be complex definitions.
It might be good to check with a number of testcases to ensure this works as expected.

Please do also add a testcase with a custom class.

Copy link
Contributor Author

@GautierDele GautierDele Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean isolate the test in a test class dedicated to this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no i understood, give me some time 😄

$parameterDefaultValueStr = sprintf(' = %s', var_export($parameterDefaultValueStr, true));
}

$arguments[] = $parameter->getType() . ' ' .
($parameter->isReference() ? '&' : '') .
($parameter->isVariadic() ? '...' : '') .
'$' . $parameter->getName();
'$' . $parameter->getName() .
($parameterDefaultValueStr ?? '');
}

$argumentStr = '(' . implode(', ', $arguments) . ')';
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/DocBlock/Tags/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
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_;
Expand Down Expand Up @@ -698,4 +700,41 @@ public function testCreateWithReference(): void
$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()
);
}
}
Loading