Skip to content

Commit

Permalink
Fix GH-8421: Attributes that target functions are not valid for anony…
Browse files Browse the repository at this point in the history
…mous functions defined within a method

Closes GH-8424
  • Loading branch information
ollieread authored and iluuu1994 committed Apr 23, 2022
1 parent d8612fb commit d0f1b98
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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 @@ -1770,7 +1770,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

0 comments on commit d0f1b98

Please sign in to comment.