File tree Expand file tree Collapse file tree 2 files changed +60
-1
lines changed Expand file tree Collapse file tree 2 files changed +60
-1
lines changed Original file line number Diff line number Diff line change
1
+ --TEST--
2
+ Bug GH-8421: Attributes that target functions are not valid for anonymous functions defined within a method
3
+ --FILE--
4
+ <?php
5
+ #[Attribute(Attribute::TARGET_FUNCTION )]
6
+ class FunctionAttribute
7
+ {
8
+ public int $ number = 1 ;
9
+ }
10
+
11
+ $ globalClosure = #[FunctionAttribute]
12
+ fn () => true ;
13
+ $ globalStaticClosure = #[FunctionAttribute]
14
+ static fn () => true ;
15
+
16
+ class ClosureHolder
17
+ {
18
+ public function getClosureDefinedInScope (): Closure
19
+ {
20
+ return #[FunctionAttribute]
21
+ fn () => true ;
22
+ }
23
+
24
+ public function getStaticClosureDefinedInScope (): Closure
25
+ {
26
+ return #[FunctionAttribute]
27
+ static fn () => true ;
28
+ }
29
+
30
+ public static function getClosureDefinedInScopeStatically (): Closure
31
+ {
32
+ return #[FunctionAttribute]
33
+ fn () => true ;
34
+ }
35
+
36
+ public static function getStaticClosureDefinedInScopeStatically (): Closure
37
+ {
38
+ return #[FunctionAttribute]
39
+ static fn () => true ;
40
+ }
41
+ }
42
+
43
+ var_dump ((new ReflectionFunction ($ globalClosure ))->getAttributes (FunctionAttribute::class)[0 ]->newInstance ()->number );
44
+ var_dump ((new ReflectionFunction ($ globalStaticClosure ))->getAttributes (FunctionAttribute::class)[0 ]->newInstance ()->number );
45
+ var_dump ((new ReflectionFunction (ClosureHolder::getClosureDefinedInScopeStatically ()))->getAttributes (FunctionAttribute::class)[0 ]->newInstance ()->number );
46
+ var_dump ((new ReflectionFunction (ClosureHolder::getStaticClosureDefinedInScopeStatically ()))->getAttributes (FunctionAttribute::class)[0 ]->newInstance ()->number );
47
+
48
+ $ holder = new ClosureHolder ;
49
+
50
+ var_dump ((new ReflectionFunction ($ holder ->getClosureDefinedInScope ()))->getAttributes (FunctionAttribute::class)[0 ]->newInstance ()->number );
51
+ var_dump ((new ReflectionFunction ($ holder ->getStaticClosureDefinedInScope ()))->getAttributes (FunctionAttribute::class)[0 ]->newInstance ()->number );
52
+ ?>
53
+ --EXPECT--
54
+ int(1)
55
+ int(1)
56
+ int(1)
57
+ int(1)
58
+ int(1)
59
+ int(1)
Original file line number Diff line number Diff line change @@ -1871,7 +1871,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)
1871
1871
1872
1872
GET_REFLECTION_OBJECT_PTR (fptr );
1873
1873
1874
- if (fptr -> common .scope ) {
1874
+ if (fptr -> common .scope && !( fptr -> common . fn_flags & ZEND_ACC_CLOSURE ) ) {
1875
1875
target = ZEND_ATTRIBUTE_TARGET_METHOD ;
1876
1876
} else {
1877
1877
target = ZEND_ATTRIBUTE_TARGET_FUNCTION ;
You can’t perform that action at this time.
0 commit comments