Valinor/tests/Fake/Definition/FakeClassDefinition.php
Romain Canon 7869cbd09c refactor: remove unnecessary ClassSignature
This abstraction layer was not useful, so it is removed to simplify the
API around `ClassDefinition`.

A new method `ClassDefinition::type()` is also added, giving access to
the `ClassType` instance when working with a class definition.
2022-01-25 18:32:28 +01:00

60 lines
1.5 KiB
PHP

<?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 CuyZ\Valinor\Type\Types\ClassType;
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(
new ClassType($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(
new ClassType($reflection->name),
new FakeAttributes(),
new Properties(...$properties),
new Methods(...$methods)
);
}
}