1--TEST--
2Attributes can be filtered by name and base type.
3--FILE--
4<?php
5
6$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
7$attr = $ref->getAttributes(A3::class);
8
9var_dump(count($attr));
10
11$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
12$attr = $ref->getAttributes(A2::class);
13
14var_dump(count($attr), $attr[0]->getName());
15
16$ref = new \ReflectionFunction(#[A1] #[A2] #[A2] function () { });
17$attr = $ref->getAttributes(A2::class);
18
19var_dump(count($attr), $attr[0]->getName(), $attr[1]->getName());
20
21echo "\n";
22
23interface Base { }
24class A1 implements Base { }
25class A2 implements Base { }
26class A3 extends A2 { }
27
28$ref = new \ReflectionFunction(#[A1] #[A2] #[A5] function () { });
29$attr = $ref->getAttributes(\stdClass::class, \ReflectionAttribute::IS_INSTANCEOF);
30var_dump(count($attr));
31print_r(array_map(fn ($a) => $a->getName(), $attr));
32
33$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
34$attr = $ref->getAttributes(A1::class, \ReflectionAttribute::IS_INSTANCEOF);
35var_dump(count($attr));
36print_r(array_map(fn ($a) => $a->getName(), $attr));
37
38$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
39$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::IS_INSTANCEOF);
40var_dump(count($attr));
41print_r(array_map(fn ($a) => $a->getName(), $attr));
42
43$ref = new \ReflectionFunction(#[A1] #[A2] #[A3] function () { });
44$attr = $ref->getAttributes(A2::class, \ReflectionAttribute::IS_INSTANCEOF);
45var_dump(count($attr));
46print_r(array_map(fn ($a) => $a->getName(), $attr));
47
48$ref = new \ReflectionFunction(#[A1] #[A2] #[A3] function () { });
49$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::IS_INSTANCEOF);
50var_dump(count($attr));
51print_r(array_map(fn ($a) => $a->getName(), $attr));
52
53echo "\n";
54
55$ref = new \ReflectionFunction(function () { });
56
57try {
58    $ref->getAttributes(A1::class, 3);
59} catch (\Error $e) {
60    var_dump('ERROR 1', $e->getMessage());
61}
62
63$ref = new \ReflectionFunction(function () { });
64
65try {
66    $ref->getAttributes(SomeMissingClass::class, \ReflectionAttribute::IS_INSTANCEOF);
67} catch (\Error $e) {
68    var_dump('ERROR 2', $e->getMessage());
69}
70
71?>
72--EXPECT--
73int(0)
74int(1)
75string(2) "A2"
76int(2)
77string(2) "A2"
78string(2) "A2"
79
80int(0)
81Array
82(
83)
84int(1)
85Array
86(
87    [0] => A1
88)
89int(2)
90Array
91(
92    [0] => A1
93    [1] => A2
94)
95int(2)
96Array
97(
98    [0] => A2
99    [1] => A3
100)
101int(3)
102Array
103(
104    [0] => A1
105    [1] => A2
106    [2] => A3
107)
108
109string(7) "ERROR 1"
110string(103) "ReflectionFunctionAbstract::getAttributes(): Argument #2 ($flags) must be a valid attribute filter flag"
111string(7) "ERROR 2"
112string(34) "Class "SomeMissingClass" not found"
113