Skip to content

Commit

Permalink
misc: handle class name in function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Mar 17, 2022
1 parent 1a599b0 commit e2451df
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/Definition/FunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -35,6 +47,14 @@ public function signature(): string
return $this->signature;
}

/**
* @return class-string|null
*/
public function class(): ?string
{
return $this->class;
}

/**
* @phpstan-return Parameters
* @return Parameters&ParameterDefinition[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use CuyZ\Valinor\Definition\FunctionDefinition;
use CuyZ\Valinor\Definition\ParameterDefinition;

use function var_export;

/** @internal */
final class FunctionDefinitionCompiler implements CacheCompiler
{
Expand All @@ -30,13 +32,15 @@ public function compile($value): string
iterator_to_array($value->parameters())
);

$class = var_export($value->class(), true);
$parameters = implode(', ', $parameters);
$returnType = $this->typeCompiler->compile($value->returnType());

return <<<PHP
new \CuyZ\Valinor\Definition\FunctionDefinition(
'{$value->name()}',
'{$value->signature()}',
$class,
new \CuyZ\Valinor\Definition\Parameters($parameters),
$returnType
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
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;
Expand Down Expand Up @@ -43,15 +45,28 @@ public function for(callable $function): FunctionDefinition
return new FunctionDefinition(
$reflection->getName(),
Reflection::signature($reflection),
// @PHP 8.0 nullsafe operator
$reflection->getClosureScopeClass() ? $reflection->getClosureScopeClass()->name : null,
new Parameters(...$parameters),
$returnType
);
}

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);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Fake/Definition/FakeFunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CuyZ\Valinor\Definition\ParameterDefinition;
use CuyZ\Valinor\Definition\Parameters;
use CuyZ\Valinor\Type\Types\NativeStringType;
use stdClass;

final class FakeFunctionDefinition
{
Expand All @@ -17,6 +18,7 @@ public static function new(): FunctionDefinition
return new FunctionDefinition(
'foo',
'foo:42-1337',
stdClass::class,
new Parameters(
new ParameterDefinition(
'bar',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use CuyZ\Valinor\Type\Types\NativeStringType;
use Error;
use PHPUnit\Framework\TestCase;
use stdClass;

final class FunctionDefinitionCompilerTest extends TestCase
{
Expand All @@ -29,6 +30,7 @@ public function test_function_is_compiled_correctly(): void
$function = new FunctionDefinition(
'foo',
'foo:42-1337',
stdClass::class,
new Parameters(
new ParameterDefinition(
'bar',
Expand All @@ -48,6 +50,7 @@ public function test_function_is_compiled_correctly(): void

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());
}
Expand Down

0 comments on commit e2451df

Please sign in to comment.