Skip to content

Commit 7de3361

Browse files
committed
Address code review comments + fix tests
1 parent 153e734 commit 7de3361

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

ext/reflection/php_reflection.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,7 +2982,7 @@ ZEND_METHOD(ReflectionMethod, __construct)
29822982

29832983
zend_object *orig_obj = NULL;
29842984
zend_class_entry *ce = NULL;
2985-
zend_string *class_name;
2985+
zend_string *class_name = NULL;
29862986
char *method_name;
29872987
size_t method_name_len;
29882988
char *lcname;
@@ -2999,12 +2999,12 @@ ZEND_METHOD(ReflectionMethod, __construct)
29992999
Z_PARAM_STR_OR_NULL(arg2_str)
30003000
ZEND_PARSE_PARAMETERS_END();
30013001

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-
30073002
if (arg1_obj) {
3003+
if (!arg2_str) {
3004+
zend_argument_value_error(2, "cannot be null when argument #1 ($objectOrMethod) is an object");
3005+
RETURN_THROWS();
3006+
}
3007+
30083008
orig_obj = arg1_obj;
30093009
ce = arg1_obj->ce;
30103010
method_name = ZSTR_VAL(arg2_str);
@@ -3023,17 +3023,20 @@ ZEND_METHOD(ReflectionMethod, __construct)
30233023

30243024
class_name = zend_string_init(name, tmp_len, 0);
30253025
method_name = tmp + 2;
3026-
method_name_len = name - tmp - 2;
3026+
method_name_len = ZSTR_LEN(arg1_str) - tmp_len - 2;
30273027
}
30283028

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));
3029+
if (class_name) {
3030+
if ((ce = zend_lookup_class(class_name)) == NULL) {
3031+
if (!EG(exception)) {
3032+
zend_throw_exception_ex(reflection_exception_ptr, 0, "Class \"%s\" does not exist", ZSTR_VAL(class_name));
3033+
}
3034+
zend_string_release(class_name);
3035+
RETURN_THROWS();
30323036
}
3037+
30333038
zend_string_release(class_name);
3034-
RETURN_THROWS();
30353039
}
3036-
zend_string_release(class_name);
30373040

30383041
object = ZEND_THIS;
30393042
intern = Z_REFLECTION_P(object);

ext/reflection/tests/008.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ string(24) "Class "a" does not exist"
3434
string(23) "Class "" does not exist"
3535
string(24) "Class "a" does not exist"
3636
string(23) "Class "" does not exist"
37-
string(103) "ReflectionMethod::__construct(): Argument #1 ($objectOrMethod) must be of type object|string, int given"
37+
string(24) "Class "1" does not exist"
3838
string(23) "Class "" does not exist"
3939
Done

ext/reflection/tests/ReflectionMethod_006.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ Steve Seear <[email protected]>
88

99
try {
1010
new ReflectionMethod();
11-
} catch (TypeError $re) {
11+
} catch (ArgumentCountError $re) {
1212
echo "Ok - ".$re->getMessage().PHP_EOL;
1313
}
1414
try {
1515
new ReflectionMethod('a', 'b', 'c');
16-
} catch (TypeError $re) {
16+
} catch (ArgumentCountError $re) {
1717
echo "Ok - ".$re->getMessage().PHP_EOL;
1818
}
1919

2020
?>
2121
--EXPECT--
22-
Ok - ReflectionMethod::__construct() expects exactly 1 argument, 0 given
23-
Ok - ReflectionMethod::__construct() expects exactly 1 argument, 3 given
22+
Ok - ReflectionMethod::__construct() expects at least 1 argument, 0 given
23+
Ok - ReflectionMethod::__construct() expects at most 2 arguments, 3 given

ext/reflection/tests/ReflectionMethod_constructor_error2.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class TestClass
1616
try {
1717
echo "Too few arguments:\n";
1818
$methodInfo = new ReflectionMethod();
19-
} catch (TypeError $re) {
19+
} catch (ArgumentCountError $re) {
2020
echo "Ok - ".$re->getMessage().PHP_EOL;
2121
}
2222
try {
2323
echo "\nToo many arguments:\n";
2424
$methodInfo = new ReflectionMethod("TestClass", "foo", true);
25-
} catch (TypeError $re) {
25+
} catch (ArgumentCountError $re) {
2626
echo "Ok - ".$re->getMessage().PHP_EOL;
2727
}
2828

@@ -38,7 +38,7 @@ try {
3838
try {
3939
//invalid 1st param
4040
$methodInfo = new ReflectionMethod([], "foo");
41-
} catch (ReflectionException $re) {
41+
} catch (TypeError $re) {
4242
echo "Ok - ".$re->getMessage().PHP_EOL;
4343
}
4444

@@ -52,10 +52,10 @@ try{
5252
?>
5353
--EXPECT--
5454
Too few arguments:
55-
Ok - ReflectionMethod::__construct() expects exactly 1 argument, 0 given
55+
Ok - ReflectionMethod::__construct() expects at least 1 argument, 0 given
5656

5757
Too many arguments:
58-
Ok - ReflectionMethod::__construct() expects exactly 1 argument, 3 given
58+
Ok - ReflectionMethod::__construct() expects at most 2 arguments, 3 given
5959
Ok - Class "InvalidClassName" does not exist
6060
Ok - ReflectionMethod::__construct(): Argument #1 ($objectOrMethod) must be of type object|string, array given
61-
Ok - ReflectionMethod::__construct() expects exactly 1 argument, 2 given
61+
Ok - ReflectionMethod::__construct(): Argument #2 ($method) must be of type ?string, array given

0 commit comments

Comments
 (0)