|
| 1 | +--TEST-- |
| 2 | +Attributes 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 | + |
| 9 | +var_dump(count($attr)); |
| 10 | + |
| 11 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { }); |
| 12 | +$attr = $ref->getAttributes(A2::class); |
| 13 | + |
| 14 | +var_dump(count($attr), $attr[0]->getName()); |
| 15 | + |
| 16 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A2>> function () { }); |
| 17 | +$attr = $ref->getAttributes(A2::class); |
| 18 | + |
| 19 | +var_dump(count($attr), $attr[0]->getName(), $attr[1]->getName()); |
| 20 | + |
| 21 | +echo "\n"; |
| 22 | + |
| 23 | +interface Base { } |
| 24 | +class A1 implements Base { } |
| 25 | +class A2 implements Base { } |
| 26 | +class A3 extends A2 { } |
| 27 | + |
| 28 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A5>> function () { }); |
| 29 | +$attr = $ref->getAttributes(\stdClass::class, true); |
| 30 | +var_dump(count($attr)); |
| 31 | +print_r(array_map(fn ($a) => $a->getName(), $attr)); |
| 32 | + |
| 33 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { }); |
| 34 | +$attr = $ref->getAttributes(A1::class, true); |
| 35 | +var_dump(count($attr)); |
| 36 | +print_r(array_map(fn ($a) => $a->getName(), $attr)); |
| 37 | + |
| 38 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { }); |
| 39 | +$attr = $ref->getAttributes(Base::class, true); |
| 40 | +var_dump(count($attr)); |
| 41 | +print_r(array_map(fn ($a) => $a->getName(), $attr)); |
| 42 | + |
| 43 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A3>> function () { }); |
| 44 | +$attr = $ref->getAttributes(A2::class, true); |
| 45 | +var_dump(count($attr)); |
| 46 | +print_r(array_map(fn ($a) => $a->getName(), $attr)); |
| 47 | + |
| 48 | +$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A3>> function () { }); |
| 49 | +$attr = $ref->getAttributes(Base::class, true); |
| 50 | +var_dump(count($attr)); |
| 51 | +print_r(array_map(fn ($a) => $a->getName(), $attr)); |
| 52 | + |
| 53 | +?> |
| 54 | +--EXPECT-- |
| 55 | +int(0) |
| 56 | +int(1) |
| 57 | +string(2) "A2" |
| 58 | +int(2) |
| 59 | +string(2) "A2" |
| 60 | +string(2) "A2" |
| 61 | + |
| 62 | +int(0) |
| 63 | +Array |
| 64 | +( |
| 65 | +) |
| 66 | +int(1) |
| 67 | +Array |
| 68 | +( |
| 69 | + [0] => A1 |
| 70 | +) |
| 71 | +int(2) |
| 72 | +Array |
| 73 | +( |
| 74 | + [0] => A1 |
| 75 | + [1] => A2 |
| 76 | +) |
| 77 | +int(2) |
| 78 | +Array |
| 79 | +( |
| 80 | + [0] => A2 |
| 81 | + [1] => A3 |
| 82 | +) |
| 83 | +int(3) |
| 84 | +Array |
| 85 | +( |
| 86 | + [0] => A1 |
| 87 | + [1] => A2 |
| 88 | + [2] => A3 |
| 89 | +) |
0 commit comments