♻️ cover more default value cases

This commit is contained in:
Gautier DELEGLISE
2024-10-28 10:28:22 +01:00
parent 456d2b0be2
commit 992c829bad
6 changed files with 203 additions and 53 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ final class MethodFactory implements PHPStanFactory
),
$param->isReference,
$param->isVariadic,
(string) $param->defaultValue
$param->defaultValue
);
},
$tagValue->parameters
@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter;
use function str_repeat;
use function strlen;
class MethodParameterFactory
{
/**
* Formats the given default value to a string-able mixin
*/
public function format($defaultValue): string
{
if (method_exists($this, $method = 'format'.ucfirst(gettype($defaultValue)))) {
return ' = ' . $this->{$method}($defaultValue);
}
return '';
}
protected function formatDouble(float $defaultValue): string
{
return var_export($defaultValue, true);
}
protected function formatNull($defaultValue): string
{
return 'null';
}
protected function formatInteger(int $defaultValue): string
{
return var_export($defaultValue, true);
}
protected function formatString(string $defaultValue): string
{
return var_export($defaultValue, true);
}
protected function formatBoolean(bool $defaultValue): string
{
return var_export($defaultValue, true);
}
protected 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 .= ',';
}
}
}
$formatedValue .= ']';
return $formatedValue;
}
protected function formatObject(object $defaultValue): string
{
return 'new '. get_class($defaultValue). '()';
}
}
+1 -12
View File
@@ -261,18 +261,7 @@ final class Method extends BaseTag implements Factory\StaticMethod
{
$arguments = [];
foreach ($this->parameters as $parameter) {
$parameterDefaultValueStr = null;
if ($parameter->getDefaultValue() !== null) {
$parameterDefaultValueStr = $parameter->getDefaultValue();
settype($parameterDefaultValueStr, (string)$parameter->getType());
$parameterDefaultValueStr = sprintf(' = %s', var_export($parameterDefaultValueStr, true));
}
$arguments[] = $parameter->getType() . ' ' .
($parameter->isReference() ? '&' : '') .
($parameter->isVariadic() ? '...' : '') .
'$' . $parameter->getName() .
($parameterDefaultValueStr ?? '');
$arguments[] = (string) $parameter;
}
$argumentStr = '(' . implode(', ', $arguments) . ')';
+15 -3
View File
@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace phpDocumentor\Reflection\DocBlock\Tags;
use phpDocumentor\Reflection\DocBlock\Tags\Factory\MethodParameterFactory;
use phpDocumentor\Reflection\Type;
final class MethodParameter
@@ -24,14 +25,16 @@ final class MethodParameter
private string $name;
private ?string $defaultValue = null;
private mixed $defaultValue;
private const NO_DEFAULT_VALUE = '__NO_VALUE__';
public function __construct(
string $name,
Type $type,
bool $isReference = false,
bool $isVariadic = false,
?string $defaultValue = null
$defaultValue = self::NO_DEFAULT_VALUE
) {
$this->type = $type;
$this->isReference = $isReference;
@@ -60,8 +63,17 @@ final class MethodParameter
return $this->isVariadic;
}
public function getDefaultValue(): ?string
public function getDefaultValue(): mixed
{
return $this->defaultValue;
}
public function __toString(): string
{
return $this->getType() . ' ' .
($this->isReference() ? '&' : '') .
($this->isVariadic() ? '...' : '') .
'$' . $this->getName() .
($this->getDefaultValue() !== self::NO_DEFAULT_VALUE ? (new MethodParameterFactory)->format($this->getDefaultValue()) : '');
}
}