Skip to content

Commit 82d3a83

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Fix GH-8421: Attributes that target functions are not valid for anonymous functions defined within a method
2 parents abd56ae + d0f1b98 commit 82d3a83

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Zend/tests/attributes/gh8421.phpt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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)

ext/reflection/php_reflection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)
18711871

18721872
GET_REFLECTION_OBJECT_PTR(fptr);
18731873

1874-
if (fptr->common.scope) {
1874+
if (fptr->common.scope && !(fptr->common.fn_flags & ZEND_ACC_CLOSURE)) {
18751875
target = ZEND_ATTRIBUTE_TARGET_METHOD;
18761876
} else {
18771877
target = ZEND_ATTRIBUTE_TARGET_FUNCTION;

0 commit comments

Comments
 (0)