Skip to content

Commit 6a5a3b7

Browse files
committed
Fix codestyle issues
1 parent 6fc32d3 commit 6a5a3b7

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

src/DocBlock/Tags/Factory/MethodFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ function (MethodTagValueParameterNode $param) use ($context) {
5757
),
5858
$param->isReference,
5959
$param->isVariadic,
60-
$param->defaultValue === null ? MethodParameter::NO_DEFAULT_VALUE : (string) $param->defaultValue
60+
$param->defaultValue === null ?
61+
MethodParameter::NO_DEFAULT_VALUE :
62+
(string) $param->defaultValue
6163
);
6264
},
6365
$tagValue->parameters

src/DocBlock/Tags/Factory/MethodParameterFactory.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
1515

16-
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
17-
use function str_repeat;
18-
use function strlen;
16+
use function array_key_last;
17+
use function get_class;
18+
use function gettype;
19+
use function method_exists;
20+
use function ucfirst;
21+
use function var_export;
1922

2023
/**
2124
* @internal This class is not part of the BC promise of this library.
@@ -26,13 +29,14 @@ final class MethodParameterFactory
2629
* Formats the given default value to a string-able mixin
2730
*
2831
* @param mixed $defaultValue
29-
* @return string
3032
*/
3133
public function format($defaultValue): string
3234
{
33-
if (method_exists($this, $method = 'format'.ucfirst(gettype($defaultValue)))) {
35+
$method = 'format' . ucfirst(gettype($defaultValue));
36+
if (method_exists($this, $method)) {
3437
return ' = ' . $this->{$method}($defaultValue);
3538
}
39+
3640
return '';
3741
}
3842

@@ -43,7 +47,6 @@ private function formatDouble(float $defaultValue): string
4347

4448
/**
4549
* @param mixed $defaultValue
46-
* @return string
4750
*/
4851
private function formatNull($defaultValue): string
4952
{
@@ -66,30 +69,32 @@ private function formatBoolean(bool $defaultValue): string
6669
}
6770

6871
/**
69-
* @param array<array|null|int|float|bool|string|object> $defaultValue
70-
* @return string
72+
* @param array<(array<mixed>|int|float|bool|string|object|null)> $defaultValue
7173
*/
7274
private function formatArray(array $defaultValue): string
7375
{
7476
$formatedValue = '[';
7577

7678
foreach ($defaultValue as $key => $value) {
77-
if (method_exists($this, $method = 'format'.ucfirst(gettype($value)))) {
78-
$formatedValue .= $this->{$method}($value);
79+
$method = 'format' . ucfirst(gettype($value));
80+
if (!method_exists($this, $method)) {
81+
continue;
82+
}
83+
84+
$formatedValue .= $this->{$method}($value);
7985

80-
if ($key !== array_key_last($defaultValue)) {
81-
$formatedValue .= ',';
82-
}
86+
if ($key === array_key_last($defaultValue)) {
87+
continue;
8388
}
84-
}
8589

86-
$formatedValue .= ']';
90+
$formatedValue .= ',';
91+
}
8792

88-
return $formatedValue;
93+
return $formatedValue . ']';
8994
}
9095

9196
private function formatObject(object $defaultValue): string
9297
{
93-
return 'new '. get_class($defaultValue). '()';
98+
return 'new ' . get_class($defaultValue) . '()';
9499
}
95100
}

src/DocBlock/Tags/MethodParameter.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory;
1616
use phpDocumentor\Reflection\Type;
1717

18+
use function implode;
19+
use function is_array;
20+
1821
final class MethodParameter
1922
{
2023
private Type $type;
@@ -25,9 +28,7 @@ final class MethodParameter
2528

2629
private string $name;
2730

28-
/**
29-
* @var mixed
30-
*/
31+
/** @var mixed */
3132
private $defaultValue;
3233

3334
public const NO_DEFAULT_VALUE = '__NO_VALUE__';
@@ -71,13 +72,11 @@ public function isVariadic(): bool
7172

7273
public function getDefaultValue(): ?string
7374
{
74-
if ($this->defaultValue === static::NO_DEFAULT_VALUE) {
75+
if ($this->defaultValue === self::NO_DEFAULT_VALUE) {
7576
return null;
7677
}
77-
if (is_array($this->defaultValue)) {
78-
return implode(',', $this->defaultValue);
79-
}
80-
return (string) $this->defaultValue;
78+
79+
return (new MethodParameterFactory())->format($this->defaultValue);
8180
}
8281

8382
public function __toString(): string
@@ -86,6 +85,10 @@ public function __toString(): string
8685
($this->isReference() ? '&' : '') .
8786
($this->isVariadic() ? '...' : '') .
8887
'$' . $this->getName() .
89-
($this->defaultValue !== self::NO_DEFAULT_VALUE ? (new MethodParameterFactory)->format($this->defaultValue) : '');
88+
(
89+
$this->defaultValue !== self::NO_DEFAULT_VALUE ?
90+
(new MethodParameterFactory())->format($this->defaultValue) :
91+
''
92+
);
9093
}
9194
}

tests/unit/DocBlock/Tags/MethodParameterTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use phpDocumentor\Reflection\Types\Object_;
2525
use phpDocumentor\Reflection\Types\String_;
2626
use PHPUnit\Framework\TestCase;
27+
use stdClass;
28+
29+
use function sprintf;
2730

2831
/**
2932
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Method
@@ -39,6 +42,7 @@ public function tearDown(): void
3942
m::close();
4043
}
4144

45+
/** @return array<array{0: Type, 1: mixed, 2: string}> */
4246
public function collectionDefaultValuesProvider(): array
4347
{
4448
return [
@@ -50,7 +54,7 @@ public function collectionDefaultValuesProvider(): array
5054
[new Array_(), [[1, 2], '2', true], '[[1,2],\'2\',true]'],
5155
[new Nullable(new Float_()), null, 'null'],
5256
[new Nullable(new Float_()), 1.23, '1.23'],
53-
[new Object_(new Fqsen('\\stdClass')), new \stdClass(), 'new stdClass()'],
57+
[new Object_(new Fqsen('\\stdClass')), new stdClass(), 'new stdClass()'],
5458
];
5559
}
5660

@@ -60,12 +64,17 @@ public function collectionDefaultValuesProvider(): array
6064
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
6165
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
6266
*
67+
* @param mixed $defaultValue
68+
*
6369
* @dataProvider collectionDefaultValuesProvider
6470
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
6571
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
6672
*/
67-
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(Type $type, $defaultValue, string $defaultValueStr): void
68-
{
73+
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(
74+
Type $type,
75+
$defaultValue,
76+
string $defaultValueStr
77+
): void {
6978
$fixture = new MethodParameter('argument', $type, false, false, $defaultValue);
7079

7180
$this->assertSame(

0 commit comments

Comments
 (0)