Skip to content

Commit

Permalink
Fix codestyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio committed Nov 4, 2024
1 parent 6fc32d3 commit 6a5a3b7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/DocBlock/Tags/Factory/MethodFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function (MethodTagValueParameterNode $param) use ($context) {
),
$param->isReference,
$param->isVariadic,
$param->defaultValue === null ? MethodParameter::NO_DEFAULT_VALUE : (string) $param->defaultValue
$param->defaultValue === null ?
MethodParameter::NO_DEFAULT_VALUE :
(string) $param->defaultValue
);
},
$tagValue->parameters
Expand Down
39 changes: 22 additions & 17 deletions src/DocBlock/Tags/Factory/MethodParameterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;

use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use function str_repeat;
use function strlen;
use function array_key_last;
use function get_class;
use function gettype;
use function method_exists;
use function ucfirst;
use function var_export;

/**
* @internal This class is not part of the BC promise of this library.
Expand All @@ -26,13 +29,14 @@ final class MethodParameterFactory
* Formats the given default value to a string-able mixin
*
* @param mixed $defaultValue
* @return string
*/
public function format($defaultValue): string
{
if (method_exists($this, $method = 'format'.ucfirst(gettype($defaultValue)))) {
$method = 'format' . ucfirst(gettype($defaultValue));
if (method_exists($this, $method)) {
return ' = ' . $this->{$method}($defaultValue);
}

return '';
}

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

/**
* @param mixed $defaultValue
* @return string
*/
private function formatNull($defaultValue): string
{
Expand All @@ -66,30 +69,32 @@ private function formatBoolean(bool $defaultValue): string
}

/**
* @param array<array|null|int|float|bool|string|object> $defaultValue
* @return string
* @param array<(array<mixed>|int|float|bool|string|object|null)> $defaultValue
*/
private function formatArray(array $defaultValue): string
{
$formatedValue = '[';

foreach ($defaultValue as $key => $value) {
if (method_exists($this, $method = 'format'.ucfirst(gettype($value)))) {
$formatedValue .= $this->{$method}($value);
$method = 'format' . ucfirst(gettype($value));
if (!method_exists($this, $method)) {
continue;
}

$formatedValue .= $this->{$method}($value);

if ($key !== array_key_last($defaultValue)) {
$formatedValue .= ',';
}
if ($key === array_key_last($defaultValue)) {
continue;
}
}

$formatedValue .= ']';
$formatedValue .= ',';
}

return $formatedValue;
return $formatedValue . ']';
}

private function formatObject(object $defaultValue): string
{
return 'new '. get_class($defaultValue). '()';
return 'new ' . get_class($defaultValue) . '()';
}
}
21 changes: 12 additions & 9 deletions src/DocBlock/Tags/MethodParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory;
use phpDocumentor\Reflection\Type;

use function implode;
use function is_array;

final class MethodParameter
{
private Type $type;
Expand All @@ -25,9 +28,7 @@ final class MethodParameter

private string $name;

/**
* @var mixed
*/
/** @var mixed */
private $defaultValue;

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

public function getDefaultValue(): ?string
{
if ($this->defaultValue === static::NO_DEFAULT_VALUE) {
if ($this->defaultValue === self::NO_DEFAULT_VALUE) {
return null;
}
if (is_array($this->defaultValue)) {
return implode(',', $this->defaultValue);
}
return (string) $this->defaultValue;

return (new MethodParameterFactory())->format($this->defaultValue);
}

public function __toString(): string
Expand All @@ -86,6 +85,10 @@ public function __toString(): string
($this->isReference() ? '&' : '') .
($this->isVariadic() ? '...' : '') .
'$' . $this->getName() .
($this->defaultValue !== self::NO_DEFAULT_VALUE ? (new MethodParameterFactory)->format($this->defaultValue) : '');
(
$this->defaultValue !== self::NO_DEFAULT_VALUE ?
(new MethodParameterFactory())->format($this->defaultValue) :
''
);
}
}
15 changes: 12 additions & 3 deletions tests/unit/DocBlock/Tags/MethodParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\String_;
use PHPUnit\Framework\TestCase;
use stdClass;

use function sprintf;

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

/** @return array<array{0: Type, 1: mixed, 2: string}> */
public function collectionDefaultValuesProvider(): array
{
return [
Expand All @@ -50,7 +54,7 @@ public function collectionDefaultValuesProvider(): array
[new Array_(), [[1, 2], '2', true], '[[1,2],\'2\',true]'],
[new Nullable(new Float_()), null, 'null'],
[new Nullable(new Float_()), 1.23, '1.23'],
[new Object_(new Fqsen('\\stdClass')), new \stdClass(), 'new stdClass()'],
[new Object_(new Fqsen('\\stdClass')), new stdClass(), 'new stdClass()'],
];
}

Expand All @@ -60,12 +64,17 @@ public function collectionDefaultValuesProvider(): array
* @uses \phpDocumentor\Reflection\DocBlock\Tags\MethodParameter::__toString
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
*
* @param mixed $defaultValue
*
* @dataProvider collectionDefaultValuesProvider
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName
*/
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(Type $type, $defaultValue, string $defaultValueStr): void
{
public function testIfTagCanBeRenderedUsingMethodParameterWithDefaultValue(
Type $type,
$defaultValue,
string $defaultValueStr
): void {
$fixture = new MethodParameter('argument', $type, false, false, $defaultValue);

$this->assertSame(
Expand Down

0 comments on commit 6a5a3b7

Please sign in to comment.