Skip to content

Commit 006bfdc

Browse files
committed
feat(zend): allow turbofish on attribute declarations
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent 6ab30dc commit 006bfdc

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Turbofish: type arguments are accepted on attribute declarations
3+
--FILE--
4+
<?php
5+
6+
// An event-listener attribute parameterised on the event type.
7+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
8+
class Listens<TEvent : object> {
9+
public function __construct(public int $priority = 0) {}
10+
}
11+
12+
class UserCreated {}
13+
class UserDeleted {}
14+
class OrderPlaced {}
15+
16+
class EventListener {
17+
#[Listens::<UserCreated>]
18+
public function onUserCreated(object $event): void {}
19+
20+
#[Listens::<UserDeleted>()]
21+
public function onUserDeleted(object $event): void {}
22+
23+
#[Listens::<OrderPlaced>(priority: -1)]
24+
public function onOrderPlaced(object $event): void {}
25+
}
26+
27+
$rc = new ReflectionClass(EventListener::class);
28+
foreach ($rc->getMethods() as $method) {
29+
foreach ($method->getAttributes() as $attr) {
30+
$instance = $attr->newInstance();
31+
echo $method->getName(), ": #[", $attr->getName(),
32+
"] priority=", $instance->priority, "\n";
33+
}
34+
}
35+
?>
36+
--EXPECT--
37+
onUserCreated: #[Listens] priority=0
38+
onUserDeleted: #[Listens] priority=0
39+
onOrderPlaced: #[Listens] priority=-1

Zend/zend_language_parser.y

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,16 @@ name:
376376
;
377377

378378
attribute_decl:
379-
class_name
380-
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, NULL); }
381-
| class_name argument_list
382-
{ $$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, $2); }
379+
class_name optional_call_type_argument_list
380+
{
381+
if ($2) zend_ast_destroy($2);
382+
$$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, NULL);
383+
}
384+
| class_name optional_call_type_argument_list argument_list
385+
{
386+
if ($2) zend_ast_destroy($2);
387+
$$ = zend_ast_create(ZEND_AST_ATTRIBUTE, $1, $3);
388+
}
383389
;
384390

385391
attribute_group:

0 commit comments

Comments
 (0)