From 6a5a3b7748d3ae43c6c202e759ed5756608e5e1b Mon Sep 17 00:00:00 2001 From: Jaapio Date: Mon, 4 Nov 2024 21:01:58 +0100 Subject: [PATCH] Fix codestyle issues --- src/DocBlock/Tags/Factory/MethodFactory.php | 4 +- .../Tags/Factory/MethodParameterFactory.php | 41 +++++++++++-------- src/DocBlock/Tags/MethodParameter.php | 21 ++++++---- .../DocBlock/Tags/MethodParameterTest.php | 15 +++++-- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/DocBlock/Tags/Factory/MethodFactory.php b/src/DocBlock/Tags/Factory/MethodFactory.php index 5d90759..17c768f 100644 --- a/src/DocBlock/Tags/Factory/MethodFactory.php +++ b/src/DocBlock/Tags/Factory/MethodFactory.php @@ -57,7 +57,9 @@ final class MethodFactory implements PHPStanFactory ), $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 diff --git a/src/DocBlock/Tags/Factory/MethodParameterFactory.php b/src/DocBlock/Tags/Factory/MethodParameterFactory.php index 92ff69d..da96896 100644 --- a/src/DocBlock/Tags/Factory/MethodParameterFactory.php +++ b/src/DocBlock/Tags/Factory/MethodParameterFactory.php @@ -13,9 +13,12 @@ declare(strict_types=1); 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. @@ -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 ''; } @@ -43,7 +47,6 @@ final class MethodParameterFactory /** * @param mixed $defaultValue - * @return string */ private function formatNull($defaultValue): string { @@ -66,30 +69,32 @@ final class MethodParameterFactory } /** - * @param array $defaultValue - * @return string + * @param array<(array|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); - - if ($key !== array_key_last($defaultValue)) { - $formatedValue .= ','; - } + $method = 'format' . ucfirst(gettype($value)); + if (!method_exists($this, $method)) { + continue; } + + $formatedValue .= $this->{$method}($value); + + 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) . '()'; } } diff --git a/src/DocBlock/Tags/MethodParameter.php b/src/DocBlock/Tags/MethodParameter.php index 06fc054..b448a4f 100644 --- a/src/DocBlock/Tags/MethodParameter.php +++ b/src/DocBlock/Tags/MethodParameter.php @@ -15,6 +15,9 @@ namespace phpDocumentor\Reflection\DocBlock\Tags; use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory; use phpDocumentor\Reflection\Type; +use function implode; +use function is_array; + final class MethodParameter { private Type $type; @@ -25,9 +28,7 @@ final class MethodParameter private string $name; - /** - * @var mixed - */ + /** @var mixed */ private $defaultValue; public const NO_DEFAULT_VALUE = '__NO_VALUE__'; @@ -71,13 +72,11 @@ final class MethodParameter 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 @@ -86,6 +85,10 @@ final class MethodParameter ($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) : + '' + ); } } diff --git a/tests/unit/DocBlock/Tags/MethodParameterTest.php b/tests/unit/DocBlock/Tags/MethodParameterTest.php index ef64f6e..a5fb2f0 100644 --- a/tests/unit/DocBlock/Tags/MethodParameterTest.php +++ b/tests/unit/DocBlock/Tags/MethodParameterTest.php @@ -24,6 +24,9 @@ use phpDocumentor\Reflection\Types\Nullable; 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 @@ -39,6 +42,7 @@ class MethodParameterTest extends TestCase m::close(); } + /** @return array */ public function collectionDefaultValuesProvider(): array { return [ @@ -50,7 +54,7 @@ class MethodParameterTest extends TestCase [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()'], ]; } @@ -60,12 +64,17 @@ class MethodParameterTest extends TestCase * @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(