summaryrefslogtreecommitdiff
path: root/Zend/tests/attributes/006_filter.phpt
blob: 2924e6ed798267b62281389317fa78b6789cd928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--TEST--
Attributes can be filtered by name and base type.
--FILE--
<?php

$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
$attr = $ref->getAttributes(A3::class);

var_dump(count($attr));

$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
$attr = $ref->getAttributes(A2::class);

var_dump(count($attr), $attr[0]->getName());

$ref = new \ReflectionFunction(#[A1] #[A2] #[A2] function () { });
$attr = $ref->getAttributes(A2::class);

var_dump(count($attr), $attr[0]->getName(), $attr[1]->getName());

echo "\n";

interface Base { }
class A1 implements Base { }
class A2 implements Base { }
class A3 extends A2 { }

$ref = new \ReflectionFunction(#[A1] #[A2] #[A5] function () { });
$attr = $ref->getAttributes(\stdClass::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
$attr = $ref->getAttributes(A1::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(#[A1] #[A2] function () { });
$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(#[A1] #[A2] #[A3] function () { });
$attr = $ref->getAttributes(A2::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

$ref = new \ReflectionFunction(#[A1] #[A2] #[A3] function () { });
$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::IS_INSTANCEOF);
var_dump(count($attr));
print_r(array_map(fn ($a) => $a->getName(), $attr));

echo "\n";

$ref = new \ReflectionFunction(function () { });

try {
    $ref->getAttributes(A1::class, 3);
} catch (\Error $e) {
    var_dump('ERROR 1', $e->getMessage());
}

$ref = new \ReflectionFunction(function () { });

try {
    $ref->getAttributes(SomeMissingClass::class, \ReflectionAttribute::IS_INSTANCEOF);
} catch (\Error $e) {
    var_dump('ERROR 2', $e->getMessage());
}

?>
--EXPECT--
int(0)
int(1)
string(2) "A2"
int(2)
string(2) "A2"
string(2) "A2"

int(0)
Array
(
)
int(1)
Array
(
    [0] => A1
)
int(2)
Array
(
    [0] => A1
    [1] => A2
)
int(2)
Array
(
    [0] => A2
    [1] => A3
)
int(3)
Array
(
    [0] => A1
    [1] => A2
    [2] => A3
)

string(7) "ERROR 1"
string(103) "ReflectionFunctionAbstract::getAttributes(): Argument #2 ($flags) must be a valid attribute filter flag"
string(7) "ERROR 2"
string(34) "Class "SomeMissingClass" not found"