Skip to content

Commit 551c7d5

Browse files
committed
Changed boolean argument of getAttributes() into a flags type.
1 parent 47adcc3 commit 551c7d5

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

Zend/tests/attributes/filter.phpt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,48 @@ class A2 implements Base { }
2626
class A3 extends A2 { }
2727

2828
$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A5>> function () { });
29-
$attr = $ref->getAttributes(\stdClass::class, true);
29+
$attr = $ref->getAttributes(\stdClass::class, \ReflectionAttribute::FILTER_INSTANCEOF);
3030
var_dump(count($attr));
3131
print_r(array_map(fn ($a) => $a->getName(), $attr));
3232

3333
$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { });
34-
$attr = $ref->getAttributes(A1::class, true);
34+
$attr = $ref->getAttributes(A1::class, \ReflectionAttribute::FILTER_INSTANCEOF);
3535
var_dump(count($attr));
3636
print_r(array_map(fn ($a) => $a->getName(), $attr));
3737

3838
$ref = new \ReflectionFunction(<<A1>> <<A2>> function () { });
39-
$attr = $ref->getAttributes(Base::class, true);
39+
$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::FILTER_INSTANCEOF);
4040
var_dump(count($attr));
4141
print_r(array_map(fn ($a) => $a->getName(), $attr));
4242

4343
$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A3>> function () { });
44-
$attr = $ref->getAttributes(A2::class, true);
44+
$attr = $ref->getAttributes(A2::class, \ReflectionAttribute::FILTER_INSTANCEOF);
4545
var_dump(count($attr));
4646
print_r(array_map(fn ($a) => $a->getName(), $attr));
4747

4848
$ref = new \ReflectionFunction(<<A1>> <<A2>> <<A3>> function () { });
49-
$attr = $ref->getAttributes(Base::class, true);
49+
$attr = $ref->getAttributes(Base::class, \ReflectionAttribute::FILTER_INSTANCEOF);
5050
var_dump(count($attr));
5151
print_r(array_map(fn ($a) => $a->getName(), $attr));
5252

53+
echo "\n";
54+
55+
$ref = new \ReflectionFunction(function () { });
56+
57+
try {
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+
65+
try {
66+
$ref->getAttributes(SomeMissingClass::class, \ReflectionAttribute::FILTER_INSTANCEOF);
67+
} catch (\Error $e) {
68+
var_dump('ERROR 2', $e->getMessage());
69+
}
70+
5371
?>
5472
--EXPECT--
5573
int(0)
@@ -87,3 +105,8 @@ Array
87105
[1] => A2
88106
[2] => A3
89107
)
108+
109+
string(7) "ERROR 1"
110+
string(39) "Invalid attribute filter flag specified"
111+
string(7) "ERROR 2"
112+
string(34) "Class 'SomeMissingClass' not found"

ext/reflection/php_reflection.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ PHPAPI zend_class_entry *reflection_attribute_ptr;
111111
#define REGISTER_REFLECTION_CLASS_CONST_LONG(class_name, const_name, value) \
112112
zend_declare_class_constant_long(reflection_ ## class_name ## _ptr, const_name, sizeof(const_name)-1, (zend_long)value);
113113

114+
#define REFLECTION_ATTRIBUTE_FILTER_INSTANCEOF (1 << 1)
115+
114116
/* {{{ Object structure */
115117

116118
/* Struct for properties */
@@ -1215,14 +1217,19 @@ static void reflect_attributes(INTERNAL_FUNCTION_PARAMETERS, HashTable *attribut
12151217
zval ret;
12161218

12171219
zend_string *name = NULL;
1218-
zend_bool inherited = 0;
1220+
zend_long flags = 0;
12191221
zend_class_entry *base = NULL;
12201222

1221-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sb", &name, &inherited) == FAILURE) {
1223+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|Sl", &name, &flags) == FAILURE) {
12221224
RETURN_THROWS();
12231225
}
12241226

1225-
if (name && inherited) {
1227+
if (flags & ~REFLECTION_ATTRIBUTE_FILTER_INSTANCEOF) {
1228+
zend_throw_error(NULL, "Invalid attribute filter flag specified");
1229+
RETURN_THROWS();
1230+
}
1231+
1232+
if (name && (flags & REFLECTION_ATTRIBUTE_FILTER_INSTANCEOF)) {
12261233
if (NULL == (base = zend_lookup_class(name))) {
12271234
if (!EG(exception)) {
12281235
zend_throw_error(NULL, "Class '%s' not found", ZSTR_VAL(name));
@@ -7138,6 +7145,8 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
71387145
_reflection_entry.ce_flags |= ZEND_ACC_FINAL;
71397146
reflection_attribute_ptr = zend_register_internal_class(&_reflection_entry);
71407147

7148+
REGISTER_REFLECTION_CLASS_CONST_LONG(attribute, "FILTER_INSTANCEOF", REFLECTION_ATTRIBUTE_FILTER_INSTANCEOF);
7149+
71417150
REFLECTION_G(key_initialized) = 0;
71427151

71437152
return SUCCESS;

ext/reflection/php_reflection.stub.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function hasReturnType() {}
9494
public function getReturnType() {}
9595

9696
/** @return ReflectionAttribute[] */
97-
public function getAttributes(?string $name = null, bool $inheritance = false) {}
97+
public function getAttributes(?string $name = null, int $flags = 0) {}
9898
}
9999

100100
class ReflectionFunction extends ReflectionFunctionAbstract
@@ -353,7 +353,7 @@ public function getNamespaceName() {}
353353
public function getShortName() {}
354354

355355
/** @return ReflectionAttribute[] */
356-
public function getAttributes(?string $name = null, bool $inheritance = false) {}
356+
public function getAttributes(?string $name = null, int $flags = 0) {}
357357
}
358358

359359
class ReflectionObject extends ReflectionClass
@@ -420,7 +420,7 @@ public function hasDefaultValue(): bool {}
420420
public function getDefaultValue() {}
421421

422422
/** @return ReflectionAttribute[] */
423-
public function getAttributes(?string $name = null, bool $inheritance = false) {}
423+
public function getAttributes(?string $name = null, int $flags = 0) {}
424424
}
425425

426426
class ReflectionClassConstant implements Reflector
@@ -456,7 +456,7 @@ public function getDeclaringClass() {}
456456
public function getDocComment() {}
457457

458458
/** @return ReflectionAttribute[] */
459-
public function getAttributes(?string $name = null, bool $inheritance = false) {}
459+
public function getAttributes(?string $name = null, int $flags = 0) {}
460460
}
461461

462462
class ReflectionParameter implements Reflector
@@ -525,7 +525,7 @@ public function getDefaultValueConstantName() {}
525525
public function isVariadic() {}
526526

527527
/** @return ReflectionAttribute[] */
528-
public function getAttributes(?string $name = null, bool $inheritance = false) {}
528+
public function getAttributes(?string $name = null, int $flags = 0) {}
529529
}
530530

531531
abstract class ReflectionType implements Stringable

ext/reflection/php_reflection_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ZEND_END_ARG_INFO()
5959

6060
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionFunctionAbstract_getAttributes, 0, 0, 0)
6161
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
62-
ZEND_ARG_TYPE_INFO(0, inheritance, _IS_BOOL, 0)
62+
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
6363
ZEND_END_ARG_INFO()
6464

6565
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionFunction___construct, 0, 0, 1)

ext/reflection/tests/ReflectionClass_toString_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector, String
112112

113113
- Parameters [2] {
114114
Parameter #0 [ <optional> ?string $name ]
115-
Parameter #1 [ <optional> bool $inheritance ]
115+
Parameter #1 [ <optional> int $flags ]
116116
}
117117
}
118118

0 commit comments

Comments
 (0)