xref: /PHP-8.0/Zend/tests/attributes/gh8421.phpt (revision d0f1b987)
1--TEST--
2Bug 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)]
6class FunctionAttribute
7{
8    public int $number = 1;
9}
10
11$globalClosure = #[FunctionAttribute]
12fn() => true;
13$globalStaticClosure = #[FunctionAttribute]
14static fn() => true;
15
16class 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
43var_dump((new ReflectionFunction($globalClosure))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
44var_dump((new ReflectionFunction($globalStaticClosure))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
45var_dump((new ReflectionFunction(ClosureHolder::getClosureDefinedInScopeStatically()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
46var_dump((new ReflectionFunction(ClosureHolder::getStaticClosureDefinedInScopeStatically()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
47
48$holder = new ClosureHolder;
49
50var_dump((new ReflectionFunction($holder->getClosureDefinedInScope()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
51var_dump((new ReflectionFunction($holder->getStaticClosureDefinedInScope()))->getAttributes(FunctionAttribute::class)[0]->newInstance()->number);
52?>
53--EXPECT--
54int(1)
55int(1)
56int(1)
57int(1)
58int(1)
59int(1)
60