Skip to content

Commit d7b73de

Browse files
committed
Improve error messages of ext/reflection
Closes GH-5277
1 parent 568592f commit d7b73de

10 files changed

+26
-29
lines changed

ext/reflection/php_reflection.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ ZEND_METHOD(reflection_parameter, __construct)
22112211
break;
22122212

22132213
default:
2214-
_DO_THROW("The parameter class is expected to be either a string, an array(class, method) or a callable object");
2214+
zend_argument_error(reflection_exception_ptr, 1, "must be either a string, an array(class, method) or a callable object, %s given", zend_zval_type_name(reference));
22152215
RETURN_THROWS();
22162216
}
22172217

@@ -2876,8 +2876,7 @@ ZEND_METHOD(reflection_method, __construct)
28762876
}
28772877

28782878
if ((tmp = strstr(name_str, "::")) == NULL) {
2879-
zend_throw_exception_ex(reflection_exception_ptr, 0,
2880-
"Invalid method name %s", name_str);
2879+
zend_argument_error(reflection_exception_ptr, 1, "must be a valid method name");
28812880
RETURN_THROWS();
28822881
}
28832882
classname = &ztmp;
@@ -2918,7 +2917,7 @@ ZEND_METHOD(reflection_method, __construct)
29182917
if (classname == &ztmp) {
29192918
zval_ptr_dtor_str(&ztmp);
29202919
}
2921-
_DO_THROW("The parameter class is expected to be either a string or an object");
2920+
zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname));
29222921
RETURN_THROWS();
29232922
}
29242923

@@ -3457,7 +3456,7 @@ ZEND_METHOD(reflection_class_constant, __construct)
34573456
break;
34583457

34593458
default:
3460-
_DO_THROW("The parameter class is expected to be either a string or an object");
3459+
zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname));
34613460
RETURN_THROWS();
34623461
}
34633462

@@ -4924,8 +4923,7 @@ ZEND_METHOD(reflection_class, isSubclassOf)
49244923
}
49254924
/* no break */
49264925
default:
4927-
zend_throw_exception_ex(reflection_exception_ptr, 0,
4928-
"Parameter one must either be a string or a ReflectionClass object");
4926+
zend_argument_error(reflection_exception_ptr, 1, "must be of type ReflectionClass|string, %s given", zend_zval_type_name(class_name));
49294927
RETURN_THROWS();
49304928
}
49314929

@@ -4967,8 +4965,7 @@ ZEND_METHOD(reflection_class, implementsInterface)
49674965
}
49684966
/* no break */
49694967
default:
4970-
zend_throw_exception_ex(reflection_exception_ptr, 0,
4971-
"Parameter one must either be a string or a ReflectionClass object");
4968+
zend_argument_error(reflection_exception_ptr, 1, "must be of type ReflectionClass|string, %s given", zend_zval_type_name(interface));
49724969
RETURN_THROWS();
49734970
}
49744971

@@ -5155,7 +5152,7 @@ ZEND_METHOD(reflection_property, __construct)
51555152
break;
51565153

51575154
default:
5158-
_DO_THROW("The parameter class is expected to be either a string or an object");
5155+
zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname));
51595156
RETURN_THROWS();
51605157
}
51615158

@@ -6144,7 +6141,7 @@ ZEND_METHOD(reflection_reference, fromArrayElement)
61446141
} else if (Z_TYPE_P(key) == IS_STRING) {
61456142
item = zend_symtable_find(ht, Z_STR_P(key));
61466143
} else {
6147-
zend_type_error("Key must be array or string");
6144+
zend_argument_type_error(2, "must be of type string|int, %s given", zend_zval_type_name(key));
61486145
RETURN_THROWS();
61496146
}
61506147

ext/reflection/tests/008.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ foreach ($a as $key=>$val) {
2727
echo "Done\n";
2828
?>
2929
--EXPECT--
30-
string(20) "Invalid method name "
31-
string(21) "Invalid method name 1"
30+
string(91) "ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name"
31+
string(91) "ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name"
3232
string(21) "Class does not exist"
3333
string(22) "Class a does not exist"
3434
string(21) "Class does not exist"
3535
string(22) "Class a does not exist"
3636
string(21) "Class does not exist"
37-
string(66) "The parameter class is expected to be either a string or an object"
37+
string(104) "ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be of type object|string, int given"
3838
string(21) "Class does not exist"
3939
Done

ext/reflection/tests/ReflectionClass_implementsInterface_001.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ try {
6565
echo $e->getMessage() . "\n";
6666
}
6767
?>
68-
--EXPECTF--
68+
--EXPECT--
6969
Does A implement A?
7070
- Using object argument: A is not an interface
7171
- Using string argument: A is not an interface
@@ -146,6 +146,6 @@ Does I2 implement I2?
146146
Test bad arguments:
147147
ReflectionClass::implementsInterface() expects exactly 1 parameter, 0 given
148148
ReflectionClass::implementsInterface() expects exactly 1 parameter, 2 given
149-
Parameter one must either be a string or a ReflectionClass object
149+
ReflectionClass::implementsInterface(): Argument #1 ($interface) must be of type ReflectionClass|string, null given
150150
Interface ThisClassDoesNotExist does not exist
151-
Parameter one must either be a string or a ReflectionClass object
151+
ReflectionClass::implementsInterface(): Argument #1 ($interface) must be of type ReflectionClass|string, int given

ext/reflection/tests/ReflectionClass_isSubclassOf_002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ try {
3939
Test bad arguments:
4040
ReflectionClass::isSubclassOf() expects exactly 1 parameter, 0 given
4141
ReflectionClass::isSubclassOf() expects exactly 1 parameter, 2 given
42-
Parameter one must either be a string or a ReflectionClass object
42+
ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, null given
4343
Class ThisClassDoesNotExist does not exist
44-
Parameter one must either be a string or a ReflectionClass object
44+
ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, int given

ext/reflection/tests/ReflectionMethod_constructor_error1.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ try {
6565
?>
6666
--EXPECTF--
6767
Wrong type of argument (bool):
68-
ReflectionException: Invalid method name 1 in %s
68+
ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name in %s:%d
6969
Stack trace:
7070
#0 %s ReflectionMethod->__construct('1')
7171
#1 {main}
7272
Wrong type of argument (int):
73-
ReflectionException: Invalid method name 3 in %s
73+
ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name in %s:%d
7474
Stack trace:
7575
#0 %s ReflectionMethod->__construct('3')
7676
#1 {main}
7777
Wrong type of argument (bool, string):
78-
ReflectionException: The parameter class is expected to be either a string or an object in %s
78+
ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be of type object|string, bool given in %s:%d
7979
Stack trace:
8080
#0 %s ReflectionMethod->__construct(true, 'foo')
8181
#1 {main}
@@ -85,7 +85,7 @@ Stack trace:
8585
#0 %s ReflectionMethod->__construct('TestClass', '1')
8686
#1 {main}
8787
No method given:
88-
ReflectionException: Invalid method name TestClass in %s
88+
ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name in %s:%d
8989
Stack trace:
9090
#0 %s ReflectionMethod->__construct('TestClass')
9191
#1 {main}

ext/reflection/tests/ReflectionMethod_constructor_error2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 0 given
5757
Too many arguments:
5858
Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 3 given
5959
Ok - Class InvalidClassName does not exist
60-
Ok - The parameter class is expected to be either a string or an object
60+
Ok - ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be of type object|string, array given
6161
Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 2 given

ext/reflection/tests/ReflectionObject_isSubclassOf.002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ try {
3939
Test bad arguments:
4040
ReflectionClass::isSubclassOf() expects exactly 1 parameter, 0 given
4141
ReflectionClass::isSubclassOf() expects exactly 1 parameter, 2 given
42-
Parameter one must either be a string or a ReflectionClass object
42+
ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, null given
4343
Class ThisClassDoesNotExist does not exist
44-
Parameter one must either be a string or a ReflectionClass object
44+
ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, int given

ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ Class A does not exist
4444
Method C::b() does not exist
4545
Method C::b() does not exist
4646
Ok - ReflectionParameter::__construct() expects exactly 2 parameters, 1 given
47-
Ok - The parameter class is expected to be either a string, an array(class, method) or a callable object
47+
Ok - ReflectionParameter::__construct(): Argument #1 ($function) must be either a string, an array(class, method) or a callable object, int given
4848
Done.

ext/reflection/tests/ReflectionProperty_constructor_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Non-existent class:
3838
Class NonExistentClass does not exist
3939

4040
Wrong property parameter type:
41-
The parameter class is expected to be either a string or an object
41+
ReflectionProperty::__construct(): Argument #1 ($class) must be of type object|string, int given
4242

4343
Non-existent property:
4444
Property TestClass::$nonExistentProperty does not exist

ext/reflection/tests/ReflectionReference_errors.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var_dump(unserialize('O:19:"ReflectionReference":0:{}'));
4242
--EXPECTF--
4343
Call to private ReflectionReference::__construct() from invalid context
4444
ReflectionReference::fromArrayElement(): Argument #1 ($array) must be of type array, object given
45-
Key must be array or string
45+
ReflectionReference::fromArrayElement(): Argument #2 ($key) must be of type string|int, float given
4646
Array key not found
4747
Serialization of 'ReflectionReference' is not allowed
4848

0 commit comments

Comments
 (0)