Valinor/tests/Fake/Definition/FakeParameterDefinition.php

66 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2021-11-28 17:43:02 +01:00
<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Fake\Definition;
use CuyZ\Valinor\Definition\ParameterDefinition;
use CuyZ\Valinor\Tests\Fake\Type\FakeType;
use CuyZ\Valinor\Type\Type;
use ReflectionParameter;
final class FakeParameterDefinition
{
private function __construct()
{
}
public static function new(string $name = 'someParameter', Type $type = null): ParameterDefinition
{
return new ParameterDefinition(
$name,
$name,
$type ?? new FakeType(),
false,
false,
2021-11-28 17:43:02 +01:00
null,
new FakeAttributes()
);
}
2022-08-05 18:51:25 +02:00
/**
* @param mixed $defaultValue
*/
public static function optional(string $name, Type $type, $defaultValue): ParameterDefinition
{
return new ParameterDefinition(
$name,
$name,
$type,
true,
false,
$defaultValue,
new FakeAttributes()
);
}
2021-11-28 17:43:02 +01:00
public static function fromReflection(ReflectionParameter $reflection): ParameterDefinition
{
$type = new FakeType();
if ($reflection->hasType()) {
$type = FakeType::from($reflection->getType()->getName()); // @phpstan-ignore-line
}
return new ParameterDefinition(
$reflection->name,
'Signature::' . $reflection->name,
$type,
$reflection->isOptional(),
$reflection->isVariadic(),
2021-11-28 17:43:02 +01:00
$reflection->isDefaultValueAvailable() ? $reflection->getDefaultValue() : null,
new FakeAttributes()
);
}
}