Skip to content

Commit d895ebf

Browse files
committed
Change the new macro to Z_PARAM_CLASS_NAME_OR_OBJ
1 parent 58783a1 commit d895ebf

File tree

3 files changed

+57
-33
lines changed

3 files changed

+57
-33
lines changed

Zend/tests/010.phpt

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,37 @@ var_dump(get_parent_class("bar"));
3232
var_dump(get_parent_class("foo"));
3333
var_dump(get_parent_class("i"));
3434

35-
var_dump(get_parent_class(""));
36-
var_dump(get_parent_class("[[[["));
37-
var_dump(get_parent_class(" "));
35+
try {
36+
get_parent_class("");
37+
} catch (TypeError $exception) {
38+
echo $exception->getMessage() . "\n";
39+
}
40+
41+
try {
42+
get_parent_class("[[[[");
43+
} catch (TypeError $exception) {
44+
echo $exception->getMessage() . "\n";
45+
}
46+
47+
try {
48+
get_parent_class(" ");
49+
} catch (TypeError $exception) {
50+
echo $exception->getMessage() . "\n";
51+
}
52+
3853
var_dump(get_parent_class(new stdclass));
54+
3955
try {
4056
get_parent_class(array());
4157
} catch (TypeError $exception) {
4258
echo $exception->getMessage() . "\n";
4359
}
44-
var_dump(get_parent_class(1));
60+
61+
try {
62+
get_parent_class(1);
63+
} catch (TypeError $exception) {
64+
echo $exception->getMessage() . "\n";
65+
}
4566

4667
echo "Done\n";
4768
?>
@@ -54,10 +75,10 @@ bool(false)
5475
string(3) "foo"
5576
bool(false)
5677
bool(false)
78+
get_parent_class(): Argument #1 ($object) must be a valid class name or object, string given
79+
get_parent_class(): Argument #1 ($object) must be a valid class name or object, string given
80+
get_parent_class(): Argument #1 ($object) must be a valid class name or object, string given
5781
bool(false)
58-
bool(false)
59-
bool(false)
60-
bool(false)
61-
get_parent_class(): Argument #1 ($object) must be of type string|object, array given
62-
bool(false)
82+
get_parent_class(): Argument #1 ($object) must be a valid class name or object, array given
83+
get_parent_class(): Argument #1 ($object) must be a valid class name or object, int given
6384
Done

Zend/zend_API.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,8 +1217,8 @@ static zend_always_inline zval *zend_try_array_init(zval *zv)
12171217
_(Z_EXPECTED_STRING_OR_ARRAY_OR_NULL, "of type string|array|null") \
12181218
_(Z_EXPECTED_STRING_OR_LONG, "of type string|int") \
12191219
_(Z_EXPECTED_STRING_OR_LONG_OR_NULL, "of type string|int|null") \
1220-
_(Z_EXPECTED_STRING_OR_OBJECT, "of type string|object") \
1221-
_(Z_EXPECTED_STRING_OR_OBJECT_OR_NULL, "of type string|object|null") \
1220+
_(Z_EXPECTED_CLASS_NAME_OR_OBJECT, "a valid class name or object") \
1221+
_(Z_EXPECTED_CLASS_NAME_OR_OBJECT_OR_NULL, "a valid class name, object, or null") \
12221222

12231223
#define Z_EXPECTED_TYPE
12241224

@@ -1397,6 +1397,20 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num
13971397
#define Z_PARAM_CLASS(dest) \
13981398
Z_PARAM_CLASS_EX(dest, 0, 0)
13991399

1400+
#define Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest_str, dest_object, allow_null) \
1401+
Z_PARAM_PROLOGUE(0, 0); \
1402+
if (UNEXPECTED(!zend_parse_arg_class_name_or_obj(_arg, &dest_str, &dest_object, _i, allow_null))) { \
1403+
_expected_type = allow_null ? Z_EXPECTED_CLASS_NAME_OR_OBJECT_OR_NULL : Z_EXPECTED_CLASS_NAME_OR_OBJECT; \
1404+
_error_code = ZPP_ERROR_WRONG_ARG; \
1405+
break; \
1406+
}
1407+
1408+
#define Z_PARAM_CLASS_NAME_OR_OBJ(dest_str, dest_object) \
1409+
Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest_str, dest_object, 0);
1410+
1411+
#define Z_PARAM_CLASS_NAME_OR_OBJ_OR_NULL(dest_str, dest_object) \
1412+
Z_PARAM_CLASS_NAME_OR_OBJ_EX(dest_str, dest_object, 1);
1413+
14001414
/* old "d" */
14011415
#define Z_PARAM_DOUBLE_EX2(dest, is_null, check_null, deref, separate) \
14021416
Z_PARAM_PROLOGUE(deref, separate); \
@@ -1682,20 +1696,6 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num
16821696
#define Z_PARAM_STR_OR_LONG_OR_NULL(dest_str, dest_long, is_null) \
16831697
Z_PARAM_STR_OR_LONG_EX(dest_str, dest_long, is_null, 1);
16841698

1685-
#define Z_PARAM_STR_OR_OBJ_EX(dest_str, dest_object, allow_null) \
1686-
Z_PARAM_PROLOGUE(0, 0); \
1687-
if (UNEXPECTED(!zend_parse_arg_str_or_obj(_arg, &dest_str, &dest_object, allow_null))) { \
1688-
_expected_type = allow_null ? Z_EXPECTED_STRING_OR_OBJECT_OR_NULL : Z_EXPECTED_STRING_OR_OBJECT; \
1689-
_error_code = ZPP_ERROR_WRONG_ARG; \
1690-
break; \
1691-
}
1692-
1693-
#define Z_PARAM_STR_OR_OBJ(dest_str, dest_object) \
1694-
Z_PARAM_STR_OR_OBJ_EX(dest_str, dest_object, 0);
1695-
1696-
#define Z_PARAM_STR_OR_OBJ_OR_NULL(dest_str, dest_object) \
1697-
Z_PARAM_STR_OR_OBJ_EX(dest_str, dest_object, 1);
1698-
16991699
/* End of new parameter parsing API */
17001700

17011701
/* Inlined implementations shared by new and old parameter parsing APIs */
@@ -1954,21 +1954,24 @@ static zend_always_inline int zend_parse_arg_str_or_long(zval *arg, zend_string
19541954
return 1;
19551955
}
19561956

1957-
static zend_always_inline int zend_parse_arg_str_or_obj(
1958-
zval *arg, zend_string **dest_str, zend_object **dest_object, int allow_null
1957+
static zend_always_inline int zend_parse_arg_class_name_or_obj(
1958+
zval *arg, zend_string **dest_str, zend_object **dest_object, int num, int allow_null
19591959
) {
1960-
if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
1961-
*dest_str = Z_STR_P(arg);
1960+
zend_class_entry *class_entry;
1961+
1962+
if (EXPECTED(Z_TYPE_P(arg) == IS_STRING) && (class_entry = zend_lookup_class(Z_STR_P(arg)))) {
1963+
*dest_str = class_entry->name;
19621964
*dest_object = NULL;
19631965
} else if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
1964-
*dest_object = Z_OBJ_P(arg);
19651966
*dest_str = NULL;
1967+
*dest_object = Z_OBJ_P(arg);
19661968
} else if (allow_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
1967-
*dest_object = NULL;
19681969
*dest_str = NULL;
1970+
*dest_object = NULL;
19691971
} else {
1972+
*dest_str = NULL;
19701973
*dest_object = NULL;
1971-
return zend_parse_arg_str_slow(arg, dest_str);
1974+
return 0;
19721975
}
19731976

19741977
return 1;

Zend/zend_builtin_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ ZEND_FUNCTION(get_parent_class)
643643

644644
ZEND_PARSE_PARAMETERS_START(0, 1)
645645
Z_PARAM_OPTIONAL
646-
Z_PARAM_STR_OR_OBJ(str, object)
646+
Z_PARAM_CLASS_NAME_OR_OBJ(str, object)
647647
ZEND_PARSE_PARAMETERS_END();
648648

649649
if (object) {

0 commit comments

Comments
 (0)