mirror of
https://github.com/danog/Valinor.git
synced 2024-11-26 20:24:40 +01:00
misc: handle class name in function definition
This commit is contained in:
parent
1a599b0bdf
commit
e2451df2c1
@ -17,10 +17,22 @@ final class FunctionDefinition
|
||||
|
||||
private Type $returnType;
|
||||
|
||||
public function __construct(string $name, string $signature, Parameters $parameters, Type $returnType)
|
||||
{
|
||||
/** @var class-string|null */
|
||||
private ?string $class;
|
||||
|
||||
/**
|
||||
* @param class-string|null $class
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $signature,
|
||||
?string $class,
|
||||
Parameters $parameters,
|
||||
Type $returnType
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->signature = $signature;
|
||||
$this->class = $class;
|
||||
$this->parameters = $parameters;
|
||||
$this->returnType = $returnType;
|
||||
}
|
||||
@ -35,6 +47,14 @@ final class FunctionDefinition
|
||||
return $this->signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return class-string|null
|
||||
*/
|
||||
public function class(): ?string
|
||||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-return Parameters
|
||||
* @return Parameters&ParameterDefinition[]
|
||||
|
@ -8,6 +8,8 @@ use CuyZ\Valinor\Cache\Compiled\CacheCompiler;
|
||||
use CuyZ\Valinor\Definition\FunctionDefinition;
|
||||
use CuyZ\Valinor\Definition\ParameterDefinition;
|
||||
|
||||
use function var_export;
|
||||
|
||||
/** @internal */
|
||||
final class FunctionDefinitionCompiler implements CacheCompiler
|
||||
{
|
||||
@ -30,6 +32,7 @@ final class FunctionDefinitionCompiler implements CacheCompiler
|
||||
iterator_to_array($value->parameters())
|
||||
);
|
||||
|
||||
$class = var_export($value->class(), true);
|
||||
$parameters = implode(', ', $parameters);
|
||||
$returnType = $this->typeCompiler->compile($value->returnType());
|
||||
|
||||
@ -37,6 +40,7 @@ final class FunctionDefinitionCompiler implements CacheCompiler
|
||||
new \CuyZ\Valinor\Definition\FunctionDefinition(
|
||||
'{$value->name()}',
|
||||
'{$value->signature()}',
|
||||
$class,
|
||||
new \CuyZ\Valinor\Definition\Parameters($parameters),
|
||||
$returnType
|
||||
)
|
||||
|
@ -9,6 +9,8 @@ use CuyZ\Valinor\Definition\Parameters;
|
||||
use CuyZ\Valinor\Definition\Repository\AttributesRepository;
|
||||
use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository;
|
||||
use CuyZ\Valinor\Type\Parser\Factory\Specifications\AliasSpecification;
|
||||
use CuyZ\Valinor\Type\Parser\Factory\Specifications\ClassContextSpecification;
|
||||
use CuyZ\Valinor\Type\Parser\Factory\Specifications\HandleClassGenericSpecification;
|
||||
use CuyZ\Valinor\Type\Parser\Factory\TypeParserFactory;
|
||||
use CuyZ\Valinor\Utility\Reflection\Reflection;
|
||||
use ReflectionFunction;
|
||||
@ -43,6 +45,8 @@ final class ReflectionFunctionDefinitionRepository implements FunctionDefinition
|
||||
return new FunctionDefinition(
|
||||
$reflection->getName(),
|
||||
Reflection::signature($reflection),
|
||||
// @PHP 8.0 nullsafe operator
|
||||
$reflection->getClosureScopeClass() ? $reflection->getClosureScopeClass()->name : null,
|
||||
new Parameters(...$parameters),
|
||||
$returnType
|
||||
);
|
||||
@ -50,8 +54,19 @@ final class ReflectionFunctionDefinitionRepository implements FunctionDefinition
|
||||
|
||||
private function typeResolver(ReflectionFunction $reflection): ReflectionTypeResolver
|
||||
{
|
||||
$advancedParser = $this->typeParserFactory->get(new AliasSpecification($reflection));
|
||||
$nativeParser = $this->typeParserFactory->get();
|
||||
$class = $reflection->getClosureScopeClass();
|
||||
|
||||
$nativeSpecifications = [];
|
||||
$advancedSpecification = [new AliasSpecification($reflection)];
|
||||
|
||||
if ($class !== null) {
|
||||
$nativeSpecifications[] = new ClassContextSpecification($class->name);
|
||||
$advancedSpecification[] = new ClassContextSpecification($class->name);
|
||||
$advancedSpecification[] = new HandleClassGenericSpecification();
|
||||
}
|
||||
|
||||
$nativeParser = $this->typeParserFactory->get(...$nativeSpecifications);
|
||||
$advancedParser = $this->typeParserFactory->get(...$advancedSpecification);
|
||||
|
||||
return new ReflectionTypeResolver($nativeParser, $advancedParser);
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use CuyZ\Valinor\Definition\FunctionDefinition;
|
||||
use CuyZ\Valinor\Definition\ParameterDefinition;
|
||||
use CuyZ\Valinor\Definition\Parameters;
|
||||
use CuyZ\Valinor\Type\Types\NativeStringType;
|
||||
use stdClass;
|
||||
|
||||
final class FakeFunctionDefinition
|
||||
{
|
||||
@ -17,6 +18,7 @@ final class FakeFunctionDefinition
|
||||
return new FunctionDefinition(
|
||||
'foo',
|
||||
'foo:42-1337',
|
||||
stdClass::class,
|
||||
new Parameters(
|
||||
new ParameterDefinition(
|
||||
'bar',
|
||||
|
@ -12,6 +12,7 @@ use CuyZ\Valinor\Definition\Repository\Cache\Compiler\FunctionDefinitionCompiler
|
||||
use CuyZ\Valinor\Type\Types\NativeStringType;
|
||||
use Error;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use stdClass;
|
||||
|
||||
final class FunctionDefinitionCompilerTest extends TestCase
|
||||
{
|
||||
@ -29,6 +30,7 @@ final class FunctionDefinitionCompilerTest extends TestCase
|
||||
$function = new FunctionDefinition(
|
||||
'foo',
|
||||
'foo:42-1337',
|
||||
stdClass::class,
|
||||
new Parameters(
|
||||
new ParameterDefinition(
|
||||
'bar',
|
||||
@ -48,6 +50,7 @@ final class FunctionDefinitionCompilerTest extends TestCase
|
||||
|
||||
self::assertSame('foo', $compiledFunction->name());
|
||||
self::assertSame('foo:42-1337', $compiledFunction->signature());
|
||||
self::assertSame(stdClass::class, $compiledFunction->class());
|
||||
self::assertTrue($compiledFunction->parameters()->has('bar'));
|
||||
self::assertInstanceOf(NativeStringType::class, $compiledFunction->returnType());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user