xref: /PHP-8.3/ext/reflection/tests/gh8982.phpt (revision 565a416e)
1--TEST--
2GH-8982 (Attribute target validation fails when read via ReflectionFunction)
3--FILE--
4<?php
5
6#[Attribute(Attribute::TARGET_FUNCTION)]
7class F
8{
9}
10
11#[Attribute(Attribute::TARGET_METHOD)]
12class M
13{
14}
15
16class C
17{
18    #[F]
19    #[M]
20    public function m()
21    {
22    }
23}
24
25#[F]
26#[M]
27function f() {}
28
29function test(string $attributeClass, $value) {
30    try {
31        var_dump((new ReflectionFunction($value))->getAttributes($attributeClass)[0]->newInstance());
32    } catch (Error $e) {
33        echo $e->getMessage(), "\n";
34    }
35}
36
37$m = [new C(), 'm'](...);
38$f = f(...);
39
40test(F::class, $f);
41test(M::class, $f);
42test(F::class, $m);
43test(M::class, $m);
44
45?>
46--EXPECT--
47object(F)#4 (0) {
48}
49Attribute "M" cannot target function (allowed targets: method)
50Attribute "F" cannot target method (allowed targets: function)
51object(M)#4 (0) {
52}
53