Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[spiral/tokenizer] Add namedArguments parameter to the TargetAttribute #1018

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Tokenizer/src/Attribute/TargetAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\Factory;
use Spiral\Attributes\Internal\Instantiator\NamedArgumentsInstantiator;
use Spiral\Attributes\NamedArgumentConstructor;
use Spiral\Tokenizer\TokenizationListenerInterface;

Expand All @@ -24,6 +25,7 @@ public function __construct(
private readonly string $attribute,
?string $scope = null,
private readonly bool $useAnnotations = false,
private readonly bool $namedArguments = true,
) {
parent::__construct($scope);
}
Expand All @@ -37,7 +39,7 @@ public function filter(array $classes): \Generator
// It will slow down the process a bit, but it will allow us to use annotations
$reader = $this->useAnnotations
? (new Factory())->create()
: new AttributeReader();
: new AttributeReader($this->namedArguments ? new NamedArgumentsInstantiator() : null);

if ($attribute === null) {
return;
Expand Down
25 changes: 22 additions & 3 deletions src/Tokenizer/tests/Attribute/TargetAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,35 @@
namespace Spiral\Tests\Tokenizer\Attribute;

use PHPUnit\Framework\TestCase;
use Spiral\Attributes\Exception\SyntaxAttributeException;
use Spiral\Tests\Tokenizer\Classes\Targets\ClassWithAttributeWithArgsOnClass;
use Spiral\Tests\Tokenizer\Fixtures\Attributes\WithTargetClassWithArgs;
use Spiral\Tokenizer\Attribute\TargetAttribute;

final class TargetAttributeTest extends TestCase
{
public function testToString(): void
{
$attribute = new TargetAttribute('foo');
$this->assertSame('86c8823f14c6ebe7e7a801ce4050f8a4', (string) $attribute);
$this->assertSame('d8d66e598d7117a26f6268ea9780774f', (string) $attribute);

$attribute = new TargetAttribute('foo', 'bar');
$this->assertSame('11dd26b3b753e5ad457331d7699250d8', (string) $attribute);
$this->assertSame('5bd549fe54f1e987a4fbfc8513d2dc68', (string) $attribute);
}
}

public function testFilter(): void
{
$attribute = new TargetAttribute(attribute: WithTargetClassWithArgs::class);
$this->assertEquals([
ClassWithAttributeWithArgsOnClass::class,
], \iterator_to_array($attribute->filter([new \ReflectionClass(ClassWithAttributeWithArgsOnClass::class)])));
}

public function testFilterExceptionAttrWithoutNamedArgument(): void
{
$attribute = new TargetAttribute(attribute: WithTargetClassWithArgs::class, namedArguments: false);

$this->expectException(SyntaxAttributeException::class);
\iterator_to_array($attribute->filter([new \ReflectionClass(ClassWithAttributeWithArgsOnClass::class)]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Tokenizer\Classes\Targets;

use Spiral\Tests\Tokenizer\Fixtures\Attributes\WithTargetClassWithArgs;

#[WithTargetClassWithArgs('foo', 'bar')]
class ClassWithAttributeWithArgsOnClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Tokenizer\Fixtures\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class WithTargetClassWithArgs
{
public function __construct(
private string $foo,
private string $bar,
) {
}
}
Loading