-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix GH-8421: Attributes that target functions are not valid for anony…
…mous functions defined within a method Closes GH-8424
- Loading branch information
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters