Fix codestyle issues

This commit is contained in:
Jaapio
2024-11-04 21:01:58 +01:00
parent 6fc32d3738
commit 6a5a3b7748
4 changed files with 50 additions and 31 deletions
+3 -1
View File
@@ -57,7 +57,9 @@ final class MethodFactory implements PHPStanFactory
), ),
$param->isReference, $param->isReference,
$param->isVariadic, $param->isVariadic,
$param->defaultValue === null ? MethodParameter::NO_DEFAULT_VALUE : (string) $param->defaultValue $param->defaultValue === null ?
MethodParameter::NO_DEFAULT_VALUE :
(string) $param->defaultValue
); );
}, },
$tagValue->parameters $tagValue->parameters
@@ -13,9 +13,12 @@ declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory; namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter; use function array_key_last;
use function str_repeat; use function get_class;
use function strlen; 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. * @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 * Formats the given default value to a string-able mixin
* *
* @param mixed $defaultValue * @param mixed $defaultValue
* @return string
*/ */
public function format($defaultValue): 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 ' = ' . $this->{$method}($defaultValue);
} }
return ''; return '';
} }
@@ -43,7 +47,6 @@ final class MethodParameterFactory
/** /**
* @param mixed $defaultValue * @param mixed $defaultValue
* @return string
*/ */
private function formatNull($defaultValue): string private function formatNull($defaultValue): string
{ {
@@ -66,30 +69,32 @@ final class MethodParameterFactory
} }
/** /**
* @param array<array|null|int|float|bool|string|object> $defaultValue * @param array<(array<mixed>|int|float|bool|string|object|null)> $defaultValue
* @return string
*/ */
private function formatArray(array $defaultValue): string private function formatArray(array $defaultValue): string
{ {
$formatedValue = '['; $formatedValue = '[';
foreach ($defaultValue as $key => $value) { foreach ($defaultValue as $key => $value) {
if (method_exists($this, $method = 'format'.ucfirst(gettype($value)))) { $method = 'format' . ucfirst(gettype($value));
$formatedValue .= $this->{$method}($value); if (!method_exists($this, $method)) {
continue;
if ($key !== array_key_last($defaultValue)) {
$formatedValue .= ',';
}
} }
$formatedValue .= $this->{$method}($value);
if ($key === array_key_last($defaultValue)) {
continue;
}
$formatedValue .= ',';
} }
$formatedValue .= ']'; return $formatedValue . ']';
return $formatedValue;
} }
private function formatObject(object $defaultValue): string private function formatObject(object $defaultValue): string
{ {
return 'new '. get_class($defaultValue). '()'; return 'new ' . get_class($defaultValue) . '()';
} }
} }
+12 -9
View File
@@ -15,6 +15,9 @@ namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory; use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory;
use phpDocumentor\Reflection\Type; use phpDocumentor\Reflection\Type;
use function implode;
use function is_array;
final class MethodParameter final class MethodParameter
{ {
private Type $type; private Type $type;
@@ -25,9 +28,7 @@ final class MethodParameter
private string $name; private string $name;
/** /** @var mixed */
* @var mixed
*/
private $defaultValue; private $defaultValue;
public const NO_DEFAULT_VALUE = '__NO_VALUE__'; public const NO_DEFAULT_VALUE = '__NO_VALUE__';
@@ -71,13 +72,11 @@ final class MethodParameter
public function getDefaultValue(): ?string public function getDefaultValue(): ?string
{ {
if ($this->defaultValue === static::NO_DEFAULT_VALUE) { if ($this->defaultValue === self::NO_DEFAULT_VALUE) {
return null; return null;
} }
if (is_array($this->defaultValue)) {
return implode(',', $this->defaultValue); return (new MethodParameterFactory())->format($this->defaultValue);
}
return (string) $this->defaultValue;
} }
public function __toString(): string public function __toString(): string
@@ -86,6 +85,10 @@ final class MethodParameter
($this->isReference() ? '&' : '') . ($this->isReference() ? '&' : '') .
($this->isVariadic() ? '...' : '') . ($this->isVariadic() ? '...' : '') .
'$' . $this->getName() . '$' . $this->getName() .
($this->defaultValue !== self::NO_DEFAULT_VALUE ? (new MethodParameterFactory)->format($this->defaultValue) : ''); (
$this->defaultValue !== self::NO_DEFAULT_VALUE ?
(new MethodParameterFactory())->format($this->defaultValue) :
''
);
} }
} }
@@ -24,6 +24,9 @@ use phpDocumentor\Reflection\Types\Nullable;
use phpDocumentor\Reflection\Types\Object_; use phpDocumentor\Reflection\Types\Object_;
use phpDocumentor\Reflection\Types\String_; use phpDocumentor\Reflection\Types\String_;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use stdClass;
use function sprintf;
/** /**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Method * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Tags\Method
@@ -39,6 +42,7 @@ class MethodParameterTest extends TestCase
m::close(); m::close();
} }
/** @return array<array{0: Type, 1: mixed, 2: string}> */
public function collectionDefaultValuesProvider(): array public function collectionDefaultValuesProvider(): array
{ {
return [ return [
@@ -50,7 +54,7 @@ class MethodParameterTest extends TestCase
[new Array_(), [[1, 2], '2', true], '[[1,2],\'2\',true]'], [new Array_(), [[1, 2], '2', true], '[[1,2],\'2\',true]'],
[new Nullable(new Float_()), null, 'null'], [new Nullable(new Float_()), null, 'null'],
[new Nullable(new Float_()), 1.23, '1.23'], [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\MethodParameter::__toString
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter * @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* *
* @param mixed $defaultValue
*
* @dataProvider collectionDefaultValuesProvider * @dataProvider collectionDefaultValuesProvider
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render * @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::render
* @covers \phpDocumentor\Reflection\DocBlock\Tags\BaseTag::getName * @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); $fixture = new MethodParameter('argument', $type, false, false, $defaultValue);
$this->assertSame( $this->assertSame(