Skip to content

Fix GH-8421: Attributes that target functions are not valid for anonymous functions defined within a method #8424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Zend/tests/attributes/gh8421.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--TEST--
Bug GH-8421: Attributes that target functions are not valid for anonymous functions defined within a method
--FILE--
<?php
#[Attribute(Attribute::TARGET_FUNCTION)]
class FunctionAttribute
{
public int $number = 1;
}

$globalClosure = #[FunctionAttribute]
fn() => true;
$globalStaticClosure = #[FunctionAttribute]
static fn() => true;

class ClosureHolder
{
public function getClosureDefinedInScope(): Closure
{
return #[FunctionAttribute]
fn() => true;
}

public function getStaticClosureDefinedInScope(): Closure
{
return #[FunctionAttribute]
static fn() => true;
}

public static function getClosureDefinedInScopeStatically(): Closure
{
return #[FunctionAttribute]
fn() => true;
}

public static function getStaticClosureDefinedInScopeStatically(): Closure
{
return #[FunctionAttribute]
static fn() => true;
}
}

var_dump((new ReflectionFunction($globalClosure))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
var_dump((new ReflectionFunction($globalStaticClosure))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
var_dump((new ReflectionFunction(ClosureHolder::getClosureDefinedInScopeStatically()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
var_dump((new ReflectionFunction(ClosureHolder::getStaticClosureDefinedInScopeStatically()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);

$holder = new ClosureHolder;

var_dump((new ReflectionFunction($holder->getClosureDefinedInScope()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
var_dump((new ReflectionFunction($holder->getStaticClosureDefinedInScope()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
?>
--EXPECT--
int(1)
int(1)
int(1)
int(1)
int(1)
int(1)
2 changes: 1 addition & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ ZEND_METHOD(ReflectionFunctionAbstract, getAttributes)

GET_REFLECTION_OBJECT_PTR(fptr);

if (fptr->common.scope) {
if (fptr->common.scope && !(fptr->common.fn_flags & ZEND_ACC_CLOSURE)) {
target = ZEND_ATTRIBUTE_TARGET_METHOD;
} else {
target = ZEND_ATTRIBUTE_TARGET_FUNCTION;
Expand Down