Skip to content

Commit

Permalink
✨ method parameter default value rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
GautierDele committed Oct 26, 2024
1 parent 60741fe commit fde2b59
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/DocBlock/Tags/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,17 @@ public function __toString(): string
{
$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() . ' ' .

Check failure on line 270 in src/DocBlock/Tags/Method.php

View workflow job for this annotation

GitHub Actions / Static analysis / Static Code Analysis (8.0)

Expression on left side of ?? is not nullable.
($parameter->isReference() ? '&' : '') .
($parameter->isVariadic() ? '...' : '') .
'$' . $parameter->getName();
'$' . $parameter->getName() .
$parameterDefaultValueStr ?? '';

Check failure on line 274 in src/DocBlock/Tags/Method.php

View workflow job for this annotation

GitHub Actions / Static analysis / Static Code Analysis (8.0)

Variable $parameterDefaultValueStr might not be defined.
}

$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()
);
}
}

0 comments on commit fde2b59

Please sign in to comment.