Skip to content

Commit 3832547

Browse files
authored
support function attributes with array arguments (#113)
1 parent d2aa1bf commit 3832547

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

src/Support/ReflectionClosure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ public function getCode()
685685

686686
$name = $attribute->getName();
687687
$arguments = implode(', ', array_map(function ($argument, $key) {
688-
$argument = sprintf("'%s'", str_replace("'", "\\'", $argument));
688+
$argument = var_export($argument, true);
689689

690690
if (is_string($key)) {
691691
$argument = sprintf('%s: %s', $key, $argument);

tests/ReflectionClosurePhp81Test.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,8 @@ function () {
589589
return true;
590590
};
591591

592-
$e = <<<EOF
593-
#[MyAttribute('My " \' Argument 1', 'Tests\Fixtures\Model')]
592+
$e = <<<'EOF'
593+
#[MyAttribute('My " \' Argument 1', 'Tests\\Fixtures\\Model')]
594594
function () {
595595
return true;
596596
}
@@ -606,8 +606,8 @@ function () {
606606
return false;
607607
};
608608

609-
$e = <<<EOF
610-
#[MyAttribute(string: 'My " \' Argument 1', model: 'Tests\Fixtures\Model')]
609+
$e = <<<'EOF'
610+
#[MyAttribute(string: 'My " \' Argument 1', model: 'Tests\\Fixtures\\Model')]
611611
function () {
612612
return false;
613613
}
@@ -621,12 +621,32 @@ function () {
621621

622622
$f = (new SerializerPhp81Controller())->publicGetter(...);
623623

624-
$e = <<<EOF
624+
$e = <<<'EOF'
625625
#[Tests\Fixtures\ModelAttribute()]
626-
#[MyAttribute('My " \' Argument 1', 'Tests\Fixtures\Model')]
626+
#[MyAttribute('My " \' Argument 1', 'Tests\\Fixtures\\Model')]
627627
function ()
628628
{
629-
return \$this->privateGetter();
629+
return $this->privateGetter();
630+
}
631+
EOF;
632+
633+
expect($f)->toBeCode($e);
634+
});
635+
636+
test('function attributes with array arguments', function () {
637+
$model = new Model();
638+
639+
$f = #[MyAttribute('My Argument', ['one', 'two'])] function (): bool {
640+
return true;
641+
};
642+
643+
$e = <<<'EOF'
644+
#[MyAttribute('My Argument', array (
645+
0 => 'one',
646+
1 => 'two',
647+
))]
648+
function (): bool {
649+
return true;
630650
}
631651
EOF;
632652

tests/SerializerPhp81Test.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,30 @@ enum SerializerScopedBackedEnum: string {
386386
fn ($attribute) => $attribute
387387
->getName()->toBe(MyAttribute::class)
388388
->getArguments()->toBe([
389-
'My " \' Argument 1', Model::class,
389+
'My " \' Argument 1', 'Tests\Fixtures\Model',
390+
])
391+
);
392+
393+
expect($f())->toBeFalse();
394+
})->with('serializers');
395+
396+
test('function attributes with array arguments', function () {
397+
$model = new Model();
398+
399+
$f = #[MyAttribute("My Argument", ["one", "two"])] function () {
400+
return false;
401+
};
402+
403+
$f = s($f);
404+
405+
$reflector = new ReflectionFunction($f);
406+
407+
expect($reflector->getAttributes())->sequence(
408+
fn ($attribute) => $attribute
409+
->getName()->toBe(MyAttribute::class)
410+
->getArguments()->toBe([
411+
"My Argument",
412+
["one", "two"],
390413
])
391414
);
392415

@@ -571,12 +594,12 @@ enum SerializerScopedBackedEnum: string {
571594
->getName()->toBe(MyAttribute::class)
572595
->getArguments()->toBe([
573596
'string' => 'My " \' Argument 1',
574-
'model' => Model::class,
597+
'model' => 'Tests\\Fixtures\\Model',
575598
]);
576599

577600
expect($attribute->value->newInstance())
578601
->string->toBe('My " \' Argument 1')
579-
->model->toBe(Model::class);
602+
->model->toBe('Tests\\Fixtures\\Model');
580603
});
581604

582605
expect($f())->toBeFalse();
@@ -596,7 +619,7 @@ enum SerializerScopedBackedEnum: string {
596619
fn ($attribute) => $attribute
597620
->getName()->toBe(MyAttribute::class)
598621
->getArguments()->toBe([
599-
'My " \' Argument 1', Model::class,
622+
'My " \' Argument 1', 'Tests\\Fixtures\\Model',
600623
])
601624
);
602625

0 commit comments

Comments
 (0)