Valinor/tests/Fake/Definition/FakeClassDefinition.php

61 lines
1.5 KiB
PHP
Raw 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\ClassDefinition;
use CuyZ\Valinor\Definition\Methods;
use CuyZ\Valinor\Definition\Properties;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use stdClass;
use function array_map;
final class FakeClassDefinition
{
private function __construct()
{
}
/**
* @param class-string $name
*/
public static function new(string $name = stdClass::class): ClassDefinition
{
return new ClassDefinition(
$name,
$name,
new FakeAttributes(),
new Properties(),
new Methods()
);
}
/**
* @param ReflectionClass<object> $reflection
*/
public static function fromReflection(ReflectionClass $reflection): ClassDefinition
{
$properties = array_map(
static fn (ReflectionProperty $reflection) => FakePropertyDefinition::fromReflection($reflection),
$reflection->getProperties()
);
$methods = array_map(
static fn (ReflectionMethod $reflection) => FakeMethodDefinition::fromReflection($reflection),
$reflection->getMethods()
);
return new ClassDefinition(
$reflection->name,
'Signature::' . $reflection->name,
new FakeAttributes(),
new Properties(...$properties),
new Methods(...$methods)
);
}
}