Skip to content

Commit

Permalink
Merge pull request #1018 from spiral/bugfix/instantiator
Browse files Browse the repository at this point in the history
[spiral/tokenizer] Add `namedArguments` parameter to the TargetAttribute
  • Loading branch information
butschster authored Nov 30, 2023
2 parents 04fb57a + 46cdb1a commit cc5594a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
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,
) {
}
}

0 comments on commit cc5594a

Please sign in to comment.