Skip to content

Commit a72bb0c

Browse files
committed
Refactor ReflectionMethod::__construct()
1 parent 95cf7a0 commit a72bb0c

File tree

4 files changed

+61
-64
lines changed

4 files changed

+61
-64
lines changed

ext/reflection/php_reflection.c

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,81 +2976,79 @@ ZEND_METHOD(ReflectionUnionType, getTypes)
29762976
/* {{{ Constructor. Throws an Exception in case the given method does not exist */
29772977
ZEND_METHOD(ReflectionMethod, __construct)
29782978
{
2979-
zval *classname;
2980-
zval *object, *orig_obj;
2981-
reflection_object *intern;
2979+
zend_object *arg1_obj;
2980+
zend_string *arg1_str;
2981+
zend_string *arg2_str = NULL;
2982+
2983+
zend_object *orig_obj = NULL;
2984+
zend_class_entry *ce = NULL;
2985+
zend_string *class_name;
2986+
char *method_name;
2987+
size_t method_name_len;
29822988
char *lcname;
2983-
zend_class_entry *ce;
2989+
2990+
zval *object;
2991+
reflection_object *intern;
29842992
zend_function *mptr;
2985-
char *name_str, *tmp;
2986-
size_t name_len, tmp_len;
2987-
zval ztmp;
2993+
char *tmp;
2994+
size_t tmp_len;
29882995

2989-
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "zs", &classname, &name_str, &name_len) == FAILURE) {
2990-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
2991-
RETURN_THROWS();
2992-
}
2996+
ZEND_PARSE_PARAMETERS_START(1, 2)
2997+
Z_PARAM_STR_OR_OBJ(arg1_str, arg1_obj)
2998+
Z_PARAM_OPTIONAL
2999+
Z_PARAM_STR_OR_NULL(arg2_str)
3000+
ZEND_PARSE_PARAMETERS_END();
29933001

2994-
if ((tmp = strstr(name_str, "::")) == NULL) {
3002+
if (arg1_obj && !arg2_str) {
3003+
zend_argument_value_error(2, "cannot be null when argument #1 ($objectOrMethod) is an object");
3004+
RETURN_THROWS();
3005+
}
3006+
3007+
if (arg1_obj) {
3008+
orig_obj = arg1_obj;
3009+
ce = arg1_obj->ce;
3010+
method_name = ZSTR_VAL(arg2_str);
3011+
method_name_len = ZSTR_LEN(arg2_str);
3012+
} else if (arg2_str) {
3013+
class_name = zend_string_copy(arg1_str);
3014+
method_name = ZSTR_VAL(arg2_str);
3015+
method_name_len = ZSTR_LEN(arg2_str);
3016+
} else {
3017+
char *name = ZSTR_VAL(arg1_str);
3018+
if ((tmp = strstr(name, "::")) == NULL) {
29953019
zend_argument_error(reflection_exception_ptr, 1, "must be a valid method name");
29963020
RETURN_THROWS();
29973021
}
2998-
classname = &ztmp;
2999-
tmp_len = tmp - name_str;
3000-
ZVAL_STRINGL(classname, name_str, tmp_len);
3001-
name_len = name_len - (tmp_len + 2);
3002-
name_str = tmp + 2;
3003-
orig_obj = NULL;
3004-
} else if (Z_TYPE_P(classname) == IS_OBJECT) {
3005-
orig_obj = classname;
3006-
} else {
3007-
orig_obj = NULL;
3008-
}
3009-
3010-
object = ZEND_THIS;
3011-
intern = Z_REFLECTION_P(object);
3022+
tmp_len = tmp - name;
30123023

3013-
switch (Z_TYPE_P(classname)) {
3014-
case IS_STRING:
3015-
if ((ce = zend_lookup_class(Z_STR_P(classname))) == NULL) {
3016-
if (!EG(exception)) {
3017-
zend_throw_exception_ex(reflection_exception_ptr, 0,
3018-
"Class \"%s\" does not exist", Z_STRVAL_P(classname));
3019-
}
3020-
if (classname == &ztmp) {
3021-
zval_ptr_dtor_str(&ztmp);
3022-
}
3023-
RETURN_THROWS();
3024-
}
3025-
break;
3026-
3027-
case IS_OBJECT:
3028-
ce = Z_OBJCE_P(classname);
3029-
break;
3030-
3031-
default:
3032-
if (classname == &ztmp) {
3033-
zval_ptr_dtor_str(&ztmp);
3034-
}
3035-
zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname));
3036-
RETURN_THROWS();
3024+
class_name = zend_string_init(name, tmp_len, 0);
3025+
method_name = tmp + 2;
3026+
method_name_len = name - tmp - 2;
30373027
}
30383028

3039-
if (classname == &ztmp) {
3040-
zval_ptr_dtor_str(&ztmp);
3029+
if (!ce && (ce = zend_lookup_class(class_name)) == NULL) {
3030+
if (!EG(exception)) {
3031+
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_name));
3032+
}
3033+
zend_string_release(class_name);
3034+
RETURN_THROWS();
30413035
}
3036+
zend_string_release(class_name);
3037+
3038+
object = ZEND_THIS;
3039+
intern = Z_REFLECTION_P(object);
30423040

3043-
lcname = zend_str_tolower_dup(name_str, name_len);
3041+
lcname = zend_str_tolower_dup(method_name, method_name_len);
30443042

3045-
if (ce == zend_ce_closure && orig_obj && (name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
3043+
if (ce == zend_ce_closure && orig_obj && (method_name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
30463044
&& memcmp(lcname, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0
3047-
&& (mptr = zend_get_closure_invoke_method(Z_OBJ_P(orig_obj))) != NULL)
3045+
&& (mptr = zend_get_closure_invoke_method(orig_obj)) != NULL)
30483046
{
30493047
/* do nothing, mptr already set */
3050-
} else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, name_len)) == NULL) {
3048+
} else if ((mptr = zend_hash_str_find_ptr(&ce->function_table, lcname, method_name_len)) == NULL) {
30513049
efree(lcname);
30523050
zend_throw_exception_ex(reflection_exception_ptr, 0,
3053-
"Method %s::%s() does not exist", ZSTR_VAL(ce->name), name_str);
3051+
"Method %s::%s() does not exist", ZSTR_VAL(ce->name), method_name);
30543052
RETURN_THROWS();
30553053
}
30563054
efree(lcname);

ext/reflection/php_reflection.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ public function getExecutingGenerator() {}
147147

148148
class ReflectionMethod extends ReflectionFunctionAbstract
149149
{
150-
/** @param object|string $objectOrMethod */
151-
public function __construct($objectOrMethod, string $method = UNKNOWN) {}
150+
public function __construct(object|string $objectOrMethod, ?string $method = null) {}
152151

153152
public function __toString(): string {}
154153

ext/reflection/php_reflection_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 1311fc5c498d6f16afb5a18aee2d60e72048174f */
2+
* Stub hash: d698afd338e4bf7c782f0edddfcbe95859eef477 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
@@ -101,8 +101,8 @@ ZEND_END_ARG_INFO()
101101
#define arginfo_class_ReflectionGenerator_getExecutingGenerator arginfo_class_ReflectionFunctionAbstract___clone
102102

103103
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionMethod___construct, 0, 0, 1)
104-
ZEND_ARG_INFO(0, objectOrMethod)
105-
ZEND_ARG_TYPE_INFO(0, method, IS_STRING, 0)
104+
ZEND_ARG_TYPE_MASK(0, objectOrMethod, MAY_BE_OBJECT|MAY_BE_STRING, NULL)
105+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, method, IS_STRING, 1, "null")
106106
ZEND_END_ARG_INFO()
107107

108108
#define arginfo_class_ReflectionMethod___toString arginfo_class_ReflectionFunction___toString

ext/reflection/tests/ReflectionMethod_constructor_error1.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ Stack trace:
7575
#0 %s ReflectionMethod->__construct('3')
7676
#1 {main}
7777
Wrong type of argument (bool, string):
78-
ReflectionException: ReflectionMethod::__construct(): Argument #1 ($objectOrMethod) must be of type object|string, bool given in %s:%d
78+
ReflectionException: Class "1" does not exist in %s:%d
7979
Stack trace:
80-
#0 %s ReflectionMethod->__construct(true, 'foo')
80+
#0 %s ReflectionMethod->__construct('1', 'foo')
8181
#1 {main}
8282
Wrong type of argument (string, bool):
8383
ReflectionException: Method TestClass::1() does not exist in %s:%d

0 commit comments

Comments
 (0)