Skip to content

Commit 6fc32d3

Browse files
authored
Merge pull request #374 from GautierDele/5.x
✨ method parameter default value rendering
2 parents c29b3d1 + 1ed543b commit 6fc32d3

File tree

5 files changed

+219
-8
lines changed

5 files changed

+219
-8
lines changed

src/DocBlock/Tags/Factory/MethodFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function (MethodTagValueParameterNode $param) use ($context) {
5757
),
5858
$param->isReference,
5959
$param->isVariadic,
60-
(string) $param->defaultValue
60+
$param->defaultValue === null ? MethodParameter::NO_DEFAULT_VALUE : (string) $param->defaultValue
6161
);
6262
},
6363
$tagValue->parameters
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
15+
16+
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
17+
use function str_repeat;
18+
use function strlen;
19+
20+
/**
21+
* @internal This class is not part of the BC promise of this library.
22+
*/
23+
final class MethodParameterFactory
24+
{
25+
/**
26+
* Formats the given default value to a string-able mixin
27+
*
28+
* @param mixed $defaultValue
29+
* @return string
30+
*/
31+
public function format($defaultValue): string
32+
{
33+
if (method_exists($this, $method = 'format'.ucfirst(gettype($defaultValue)))) {
34+
return ' = ' . $this->{$method}($defaultValue);
35+
}
36+
return '';
37+
}
38+
39+
private function formatDouble(float $defaultValue): string
40+
{
41+
return var_export($defaultValue, true);
42+
}
43+
44+
/**
45+
* @param mixed $defaultValue
46+
* @return string
47+
*/
48+
private function formatNull($defaultValue): string
49+
{
50+
return 'null';
51+
}
52+
53+
private function formatInteger(int $defaultValue): string
54+
{
55+
return var_export($defaultValue, true);
56+
}
57+
58+
private function formatString(string $defaultValue): string
59+
{
60+
return var_export($defaultValue, true);
61+
}
62+
63+
private function formatBoolean(bool $defaultValue): string
64+
{
65+
return var_export($defaultValue, true);
66+
}
67+
68+
/**
69+
* @param array<array|null|int|float|bool|string|object> $defaultValue
70+
* @return string
71+
*/
72+
private function formatArray(array $defaultValue): string
73+
{
74+
$formatedValue = '[';
75+
76+
foreach ($defaultValue as $key => $value) {
77+
if (method_exists($this, $method = 'format'.ucfirst(gettype($value)))) {
78+
$formatedValue .= $this->{$method}($value);
79+
80+
if ($key !== array_key_last($defaultValue)) {
81+
$formatedValue .= ',';
82+
}
83+
}
84+
}
85+
86+
$formatedValue .= ']';
87+
88+
return $formatedValue;
89+
}
90+
91+
private function formatObject(object $defaultValue): string
92+
{
93+
return 'new '. get_class($defaultValue). '()';
94+
}
95+
}

src/DocBlock/Tags/Method.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,7 @@ public function __toString(): string
261261
{
262262
$arguments = [];
263263
foreach ($this->parameters as $parameter) {
264-
$arguments[] = $parameter->getType() . ' ' .
265-
($parameter->isReference() ? '&' : '') .
266-
($parameter->isVariadic() ? '...' : '') .
267-
'$' . $parameter->getName();
264+
$arguments[] = (string) $parameter;
268265
}
269266

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

src/DocBlock/Tags/MethodParameter.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace phpDocumentor\Reflection\DocBlock\Tags;
1414

15+
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory;
1516
use phpDocumentor\Reflection\Type;
1617

1718
final class MethodParameter
@@ -24,14 +25,22 @@ final class MethodParameter
2425

2526
private string $name;
2627

27-
private ?string $defaultValue = null;
28+
/**
29+
* @var mixed
30+
*/
31+
private $defaultValue;
2832

33+
public const NO_DEFAULT_VALUE = '__NO_VALUE__';
34+
35+
/**
36+
* @param mixed $defaultValue
37+
*/
2938
public function __construct(
3039
string $name,
3140
Type $type,
3241
bool $isReference = false,
3342
bool $isVariadic = false,
34-
?string $defaultValue = null
43+
$defaultValue = self::NO_DEFAULT_VALUE
3544
) {
3645
$this->type = $type;
3746
$this->isReference = $isReference;
@@ -62,6 +71,21 @@ public function isVariadic(): bool
6271

6372
public function getDefaultValue(): ?string
6473
{
65-
return $this->defaultValue;
74+
if ($this->defaultValue === static::NO_DEFAULT_VALUE) {
75+
return null;
76+
}
77+
if (is_array($this->defaultValue)) {
78+
return implode(',', $this->defaultValue);
79+
}
80+
return (string) $this->defaultValue;
81+
}
82+
83+
public function __toString(): string
84+
{
85+
return $this->getType() . ' ' .
86+
($this->isReference() ? '&' : '') .
87+
($this->isVariadic() ? '...' : '') .
88+
'$' . $this->getName() .
89+
($this->defaultValue !== self::NO_DEFAULT_VALUE ? (new MethodParameterFactory)->format($this->defaultValue) : '');
6690
}
6791
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\DocBlock\Tags;
15+
16+
use Mockery as m;
17+
use phpDocumentor\Reflection\Fqsen;
18+
use phpDocumentor\Reflection\Type;
19+
use phpDocumentor\Reflection\Types\Array_;
20+
use phpDocumentor\Reflection\Types\Boolean;
21+
use phpDocumentor\Reflection\Types\Float_;
22+
use phpDocumentor\Reflection\Types\Integer;
23+
use phpDocumentor\Reflection\Types\Nullable;
24+
use phpDocumentor\Reflection\Types\Object_;
25+
use phpDocumentor\Reflection\Types\String_;
26+
use PHPUnit\Framework\TestCase;
27+
28+
/**
29+
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Method
30+
* @covers ::<private>
31+
*/
32+
class MethodParameterTest extends TestCase
33+
{
34+
/**
35+
* Call Mockery::close after each test.
36+
*/
37+
public function tearDown(): void
38+
{
39+
m::close();
40+
}
41+
42+
public function collectionDefaultValuesProvider(): array
43+
{
44+
return [
45+
[new String_(), '1', '\'1\''],
46+
[new Integer(), 1, '1'],
47+
[new Boolean(), true, 'true'],
48+
[new Float_(), 1.23, '1.23'],
49+
[new Array_(), [1, '2', true], '[1,\'2\',true]'],
50+
[new Array_(), [[1, 2], '2', true], '[[1,2],\'2\',true]'],
51+
[new Nullable(new Float_()), null, 'null'],
52+
[new Nullable(new Float_()), 1.23, '1.23'],
53+
[new Object_(new Fqsen('\\stdClass')), new \stdClass(), 'new stdClass()'],
54+
];
55+
}
56+
57+
/**
58+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct
59+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue()
60+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
61+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
62+
*
63+
* @dataProvider collectionDefaultValuesProvider
64+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
65+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
66+
*/
67+
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(Type $type, $defaultValue, string $defaultValueStr): void
68+
{
69+
$fixture = new MethodParameter('argument', $type, false, false, $defaultValue);
70+
71+
$this->assertSame(
72+
sprintf('%s $argument = %s', $type, $defaultValueStr),
73+
(string) $fixture
74+
);
75+
}
76+
77+
/**
78+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct
79+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue()
80+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
81+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
82+
*
83+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
84+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
85+
*/
86+
public function testIfTagCanBeRenderedUsingMethodParameterWithNoDefaultValue(): void
87+
{
88+
$fixture = new MethodParameter('argument', new Float_());
89+
90+
$this->assertSame(
91+
'float $argument',
92+
(string) $fixture
93+
);
94+
}
95+
}

0 commit comments

Comments
 (0)