Skip to content

Commit

Permalink
misc: fetch attributes for function definition
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Aug 29, 2022
1 parent c37ac1e commit ec494ce
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/Definition/DoctrineAnnotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CuyZ\Valinor\Definition\Exception\InvalidReflectionParameter;
use CuyZ\Valinor\Utility\Singleton;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionParameter;
use ReflectionProperty;
Expand Down Expand Up @@ -73,6 +74,10 @@ private function attributes(Reflector $reflection): array
return [];
}

if ($reflection instanceof ReflectionFunction) {
return [];
}

throw new InvalidReflectionParameter($reflection);
}
}
9 changes: 9 additions & 0 deletions src/Definition/FunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ final class FunctionDefinition

private string $signature;

private Attributes $attributes;

private ?string $fileName;

/** @var class-string|null */
Expand All @@ -30,6 +32,7 @@ final class FunctionDefinition
public function __construct(
string $name,
string $signature,
Attributes $attributes,
?string $fileName,
?string $class,
bool $isStatic,
Expand All @@ -38,6 +41,7 @@ public function __construct(
) {
$this->name = $name;
$this->signature = $signature;
$this->attributes = $attributes;
$this->fileName = $fileName;
$this->class = $class;
$this->isStatic = $isStatic;
Expand All @@ -55,6 +59,11 @@ public function signature(): string
return $this->signature;
}

public function attributes(): Attributes
{
return $this->attributes;
}

public function fileName(): ?string
{
return $this->fileName;
Expand Down
27 changes: 8 additions & 19 deletions src/Definition/NativeAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace CuyZ\Valinor\Definition;

use CuyZ\Valinor\Definition\Exception\InvalidReflectionParameter;
use Error;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionParameter;
use ReflectionProperty;
use Reflector;

use Traversable;

use function array_map;
Expand All @@ -25,6 +24,10 @@ final class NativeAttributes implements Attributes
/** @var array<ReflectionAttribute<object>> */
private array $reflectionAttributes;

/**
* @PHP8.0 union
* @param ReflectionClass<object>|ReflectionProperty|ReflectionMethod|ReflectionFunction|ReflectionParameter $reflection
*/
public function __construct(Reflector $reflection)
{
$this->reflectionAttributes = $this->attributes($reflection);
Expand Down Expand Up @@ -80,26 +83,12 @@ public function reflectionAttributes(): array
}

/**
* @PHP8.0 union
* @param ReflectionClass<object>|ReflectionProperty|ReflectionMethod|ReflectionFunction|ReflectionParameter $reflection
* @return array<ReflectionAttribute<object>>
*/
private function attributes(Reflector $reflection): array
{
if ($reflection instanceof ReflectionClass) {
return $reflection->getAttributes();
}

if ($reflection instanceof ReflectionProperty) {
return $reflection->getAttributes();
}

if ($reflection instanceof ReflectionMethod) {
return $reflection->getAttributes();
}

if ($reflection instanceof ReflectionParameter) {
return $reflection->getAttributes();
}

throw new InvalidReflectionParameter($reflection);
return $reflection->getAttributes();
}
}
8 changes: 8 additions & 0 deletions src/Definition/Repository/AttributesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
namespace CuyZ\Valinor\Definition\Repository;

use CuyZ\Valinor\Definition\Attributes;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionParameter;
use ReflectionProperty;
use Reflector;

/** @internal */
interface AttributesRepository
{
/**
* @param ReflectionClass<object>|ReflectionProperty|ReflectionMethod|ReflectionFunction|ReflectionParameter $reflector
*/
public function for(Reflector $reflector): Attributes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ final class FunctionDefinitionCompiler implements CacheCompiler
{
private TypeCompiler $typeCompiler;

private AttributesCompiler $attributesCompiler;

private ParameterDefinitionCompiler $parameterCompiler;

public function __construct()
{
$this->typeCompiler = new TypeCompiler();
$this->attributesCompiler = new AttributesCompiler();

$this->parameterCompiler = new ParameterDefinitionCompiler($this->typeCompiler, new AttributesCompiler());
}

Expand All @@ -32,6 +36,7 @@ public function compile($value): string
iterator_to_array($value->parameters())
);

$attributes = $this->attributesCompiler->compile($value->attributes());
$fileName = var_export($value->fileName(), true);
$class = var_export($value->class(), true);
$isStatic = var_export($value->isStatic(), true);
Expand All @@ -42,6 +47,7 @@ public function compile($value): string
new \CuyZ\Valinor\Definition\FunctionDefinition(
'{$value->name()}',
'{$value->signature()}',
$attributes,
$fileName,
$class,
$isStatic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ final class ReflectionFunctionDefinitionRepository implements FunctionDefinition

private ReflectionParameterDefinitionBuilder $parameterBuilder;

private AttributesRepository $attributesRepository;

public function __construct(TypeParserFactory $typeParserFactory, AttributesRepository $attributesRepository)
{
$this->typeParserFactory = $typeParserFactory;
$this->attributesRepository = $attributesRepository;

$this->parameterBuilder = new ReflectionParameterDefinitionBuilder($attributesRepository);
}

Expand All @@ -46,6 +50,7 @@ public function for(callable $function): FunctionDefinition
return new FunctionDefinition(
$reflection->getName(),
Reflection::signature($reflection),
$this->attributesRepository->for($reflection),
$reflection->getFileName() ?: null,
// @PHP 8.0 nullsafe operator
$class ? $class->name : null,
Expand Down
1 change: 1 addition & 0 deletions tests/Fake/Definition/FakeFunctionDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static function new(string $fileName = null): FunctionDefinition
return new FunctionDefinition(
'foo',
'foo:42-1337',
new FakeAttributes(),
$fileName ?? 'foo/bar',
stdClass::class,
true,
Expand Down
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\Definition\Repository\Cache\Compiler\FunctionDefinitionCompiler;
use CuyZ\Valinor\Tests\Fake\Definition\FakeAttributes;
use CuyZ\Valinor\Type\Types\NativeStringType;
use Error;
use PHPUnit\Framework\TestCase;
Expand All @@ -30,6 +31,7 @@ public function test_function_is_compiled_correctly(): void
$function = new FunctionDefinition(
'foo',
'foo:42-1337',
new FakeAttributes(),
'foo/bar',
stdClass::class,
true,
Expand Down
16 changes: 10 additions & 6 deletions tests/Unit/Definition/NativeAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace CuyZ\Valinor\Tests\Unit\Definition;

use CuyZ\Valinor\Definition\Exception\InvalidReflectionParameter;
use CuyZ\Valinor\Definition\NativeAttributes;
use CuyZ\Valinor\Tests\Fake\FakeReflector;
use CuyZ\Valinor\Tests\Fixture\Attribute\AttributeWithArguments;
use CuyZ\Valinor\Tests\Fixture\Attribute\BasicAttribute;
use CuyZ\Valinor\Tests\Fixture\Object\ObjectWithAttributes;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionParameter;
use ReflectionProperty;
Expand Down Expand Up @@ -96,11 +95,16 @@ public function test_parameter_attributes_are_fetched_correctly(): void
self::assertCount(1, $attributes->ofType(BasicAttribute::class));
}

public function test_throws_on_incompatible_reflection(): void
public function test_function_attributes_are_fetched_correctly(): void
{
$this->expectException(InvalidReflectionParameter::class);
$this->expectExceptionCode(1534263918);
$reflection = new ReflectionFunction(
#[BasicAttribute]
fn () => 'foo'
);
$attributes = new NativeAttributes($reflection);

new NativeAttributes(new FakeReflector());
self::assertCount(1, $attributes);
self::assertTrue($attributes->has(BasicAttribute::class));
self::assertCount(1, $attributes->ofType(BasicAttribute::class));
}
}

0 comments on commit ec494ce

Please sign in to comment.