Skip to content

Commit 992c829

Browse files
committed
♻️ cover more default value cases
1 parent 456d2b0 commit 992c829

File tree

6 files changed

+203
-53
lines changed

6 files changed

+203
-53
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
6161
);
6262
},
6363
$tagValue->parameters
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
class MethodParameterFactory
21+
{
22+
/**
23+
* Formats the given default value to a string-able mixin
24+
*/
25+
public function format($defaultValue): string
26+
{
27+
if (method_exists($this, $method = 'format'.ucfirst(gettype($defaultValue)))) {
28+
return ' = ' . $this->{$method}($defaultValue);
29+
}
30+
return '';
31+
}
32+
33+
protected function formatDouble(float $defaultValue): string
34+
{
35+
return var_export($defaultValue, true);
36+
}
37+
38+
protected function formatNull($defaultValue): string
39+
{
40+
return 'null';
41+
}
42+
43+
protected function formatInteger(int $defaultValue): string
44+
{
45+
return var_export($defaultValue, true);
46+
}
47+
48+
protected function formatString(string $defaultValue): string
49+
{
50+
return var_export($defaultValue, true);
51+
}
52+
53+
protected function formatBoolean(bool $defaultValue): string
54+
{
55+
return var_export($defaultValue, true);
56+
}
57+
58+
protected function formatArray(array $defaultValue): string
59+
{
60+
$formatedValue = '[';
61+
62+
foreach ($defaultValue as $key => $value) {
63+
if (method_exists($this, $method = 'format'.ucfirst(gettype($value)))) {
64+
$formatedValue .= $this->{$method}($value);
65+
66+
if ($key !== array_key_last($defaultValue)) {
67+
$formatedValue .= ',';
68+
}
69+
}
70+
}
71+
72+
$formatedValue .= ']';
73+
74+
return $formatedValue;
75+
}
76+
77+
protected function formatObject(object $defaultValue): string
78+
{
79+
return 'new '. get_class($defaultValue). '()';
80+
}
81+
}

src/DocBlock/Tags/Method.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,7 @@ public function __toString(): string
261261
{
262262
$arguments = [];
263263
foreach ($this->parameters as $parameter) {
264-
$parameterDefaultValueStr = null;
265-
if ($parameter->getDefaultValue() !== null) {
266-
$parameterDefaultValueStr = $parameter->getDefaultValue();
267-
settype($parameterDefaultValueStr, (string)$parameter->getType());
268-
$parameterDefaultValueStr = sprintf(' = %s', var_export($parameterDefaultValueStr, true));
269-
}
270-
271-
$arguments[] = $parameter->getType() . ' ' .
272-
($parameter->isReference() ? '&' : '') .
273-
($parameter->isVariadic() ? '...' : '') .
274-
'$' . $parameter->getName() .
275-
($parameterDefaultValueStr ?? '');
264+
$arguments[] = (string) $parameter;
276265
}
277266

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

src/DocBlock/Tags/MethodParameter.php

Lines changed: 15 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,16 @@ final class MethodParameter
2425

2526
private string $name;
2627

27-
private ?string $defaultValue = null;
28+
private mixed $defaultValue;
29+
30+
private const NO_DEFAULT_VALUE = '__NO_VALUE__';
2831

2932
public function __construct(
3033
string $name,
3134
Type $type,
3235
bool $isReference = false,
3336
bool $isVariadic = false,
34-
?string $defaultValue = null
37+
$defaultValue = self::NO_DEFAULT_VALUE
3538
) {
3639
$this->type = $type;
3740
$this->isReference = $isReference;
@@ -60,8 +63,17 @@ public function isVariadic(): bool
6063
return $this->isVariadic;
6164
}
6265

63-
public function getDefaultValue(): ?string
66+
public function getDefaultValue(): mixed
6467
{
6568
return $this->defaultValue;
6669
}
70+
71+
public function __toString(): string
72+
{
73+
return $this->getType() . ' ' .
74+
($this->isReference() ? '&' : '') .
75+
($this->isVariadic() ? '...' : '') .
76+
'$' . $this->getName() .
77+
($this->getDefaultValue() !== self::NO_DEFAULT_VALUE ? (new MethodParameterFactory)->format($this->getDefaultValue()) : '');
78+
}
6779
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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\DocBlock\Description;
18+
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
19+
use phpDocumentor\Reflection\Fqsen;
20+
use phpDocumentor\Reflection\Type;
21+
use phpDocumentor\Reflection\TypeResolver;
22+
use phpDocumentor\Reflection\Types\Array_;
23+
use phpDocumentor\Reflection\Types\Boolean;
24+
use phpDocumentor\Reflection\Types\Compound;
25+
use phpDocumentor\Reflection\Types\Context;
26+
use phpDocumentor\Reflection\Types\Float_;
27+
use phpDocumentor\Reflection\Types\Integer;
28+
use phpDocumentor\Reflection\Types\Mixed_;
29+
use phpDocumentor\Reflection\Types\Null_;
30+
use phpDocumentor\Reflection\Types\Nullable;
31+
use phpDocumentor\Reflection\Types\Object_;
32+
use phpDocumentor\Reflection\Types\String_;
33+
use phpDocumentor\Reflection\Types\This;
34+
use phpDocumentor\Reflection\Types\Void_;
35+
use PHPUnit\Framework\TestCase;
36+
37+
/**
38+
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Method
39+
* @covers ::<private>
40+
*/
41+
class MethodParameterTest extends TestCase
42+
{
43+
/**
44+
* Call Mockery::close after each test.
45+
*/
46+
public function tearDown(): void
47+
{
48+
m::close();
49+
}
50+
51+
public function collectionDefaultValuesProvider(): array
52+
{
53+
return [
54+
[new String_(), '1', '\'1\''],
55+
[new Integer(), 1, '1'],
56+
[new Boolean(), true, 'true'],
57+
[new Float_(), 1.23, '1.23'],
58+
[new Array_(), [1, '2', true], '[1,\'2\',true]'],
59+
[new Array_(), [[1, 2], '2', true], '[[1,2],\'2\',true]'],
60+
[new Nullable(new Float_()), null, 'null'],
61+
[new Nullable(new Float_()), 1.23, '1.23'],
62+
[new Object_(new Fqsen('\\stdClass')), new \stdClass(), 'new stdClass()'],
63+
];
64+
}
65+
66+
/**
67+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct
68+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue()
69+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
70+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
71+
*
72+
* @dataProvider collectionDefaultValuesProvider
73+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
74+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
75+
*/
76+
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(Type $type, $defaultValue, string $defaultValueStr): void
77+
{
78+
$fixture = new MethodParameter('argument', $type, false, false, $defaultValue);
79+
80+
$this->assertSame(
81+
sprintf('%s $argument = %s', $type, $defaultValueStr),
82+
(string) $fixture
83+
);
84+
}
85+
86+
/**
87+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct
88+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue()
89+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
90+
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
91+
*
92+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
93+
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
94+
*/
95+
public function testIfTagCanBeRenderedUsingMethodParameterWithNoDefaultValue(): void
96+
{
97+
$fixture = new MethodParameter('argument', new Float_());
98+
99+
$this->assertSame(
100+
'float $argument',
101+
(string) $fixture
102+
);
103+
}
104+
}

tests/unit/DocBlock/Tags/MethodTest.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpDocumentor\Reflection\DocBlock\Description;
1818
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
1919
use phpDocumentor\Reflection\Fqsen;
20+
use phpDocumentor\Reflection\Type;
2021
use phpDocumentor\Reflection\TypeResolver;
2122
use phpDocumentor\Reflection\Types\Array_;
2223
use phpDocumentor\Reflection\Types\Boolean;
@@ -700,41 +701,4 @@ public function testCreateWithReference(): void
700701
$this->assertSame($description, $fixture->getDescription());
701702
$this->assertTrue($fixture->returnsReference());
702703
}
703-
704-
/**
705-
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__construct
706-
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::getDefaultValue()
707-
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
708-
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
709-
*
710-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
711-
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
712-
*/
713-
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(): void
714-
{
715-
$arguments = [
716-
['name' => 'argument1', 'type' => new String_()],
717-
['name' => 'argument2', 'type' => new Object_()],
718-
];
719-
720-
$fixture = new Method(
721-
'myMethod',
722-
$arguments,
723-
new Void_(),
724-
false,
725-
null,
726-
false,
727-
[
728-
new MethodParameter('argument1', new String_(), false, false, '1'),
729-
new MethodParameter('argument2', new Integer(), false, false, '1'),
730-
new MethodParameter('argument3', new Boolean(), false, false, 'true'),
731-
new MethodParameter('argument4', new Float_(), false, false, '1.23'),
732-
]
733-
);
734-
735-
$this->assertSame(
736-
'@method void myMethod(string $argument1 = \'1\', int $argument2 = 1, bool $argument3 = true, float $argument4 = 1.23)',
737-
$fixture->render()
738-
);
739-
}
740704
}

0 commit comments

Comments
 (0)