From d7b73de8c9299ef1e7c55f22449dff3e0833f68e Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Wed, 18 Mar 2020 21:39:56 +0100 Subject: [PATCH] Improve error messages of ext/reflection Closes GH-5277 --- ext/reflection/php_reflection.c | 19 ++++++++----------- ext/reflection/tests/008.phpt | 6 +++--- ...flectionClass_implementsInterface_001.phpt | 6 +++--- .../ReflectionClass_isSubclassOf_002.phpt | 4 ++-- .../ReflectionMethod_constructor_error1.phpt | 8 ++++---- .../ReflectionMethod_constructor_error2.phpt | 2 +- .../ReflectionObject_isSubclassOf.002.phpt | 4 ++-- ...nParameter_invalidMethodInConstructor.phpt | 2 +- .../ReflectionProperty_constructor_error.phpt | 2 +- .../tests/ReflectionReference_errors.phpt | 2 +- 10 files changed, 26 insertions(+), 29 deletions(-) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 94f1b1659d..e3d16fd3e0 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -2211,7 +2211,7 @@ ZEND_METHOD(reflection_parameter, __construct) break; default: - _DO_THROW("The parameter class is expected to be either a string, an array(class, method) or a callable object"); + 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)); RETURN_THROWS(); } @@ -2876,8 +2876,7 @@ ZEND_METHOD(reflection_method, __construct) } if ((tmp = strstr(name_str, "::")) == NULL) { - zend_throw_exception_ex(reflection_exception_ptr, 0, - "Invalid method name %s", name_str); + zend_argument_error(reflection_exception_ptr, 1, "must be a valid method name"); RETURN_THROWS(); } classname = &ztmp; @@ -2918,7 +2917,7 @@ ZEND_METHOD(reflection_method, __construct) if (classname == &ztmp) { zval_ptr_dtor_str(&ztmp); } - _DO_THROW("The parameter class is expected to be either a string or an object"); + zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname)); RETURN_THROWS(); } @@ -3457,7 +3456,7 @@ ZEND_METHOD(reflection_class_constant, __construct) break; default: - _DO_THROW("The parameter class is expected to be either a string or an object"); + zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname)); RETURN_THROWS(); } @@ -4924,8 +4923,7 @@ ZEND_METHOD(reflection_class, isSubclassOf) } /* no break */ default: - zend_throw_exception_ex(reflection_exception_ptr, 0, - "Parameter one must either be a string or a ReflectionClass object"); + zend_argument_error(reflection_exception_ptr, 1, "must be of type ReflectionClass|string, %s given", zend_zval_type_name(class_name)); RETURN_THROWS(); } @@ -4967,8 +4965,7 @@ ZEND_METHOD(reflection_class, implementsInterface) } /* no break */ default: - zend_throw_exception_ex(reflection_exception_ptr, 0, - "Parameter one must either be a string or a ReflectionClass object"); + zend_argument_error(reflection_exception_ptr, 1, "must be of type ReflectionClass|string, %s given", zend_zval_type_name(interface)); RETURN_THROWS(); } @@ -5155,7 +5152,7 @@ ZEND_METHOD(reflection_property, __construct) break; default: - _DO_THROW("The parameter class is expected to be either a string or an object"); + zend_argument_error(reflection_exception_ptr, 1, "must be of type object|string, %s given", zend_zval_type_name(classname)); RETURN_THROWS(); } @@ -6144,7 +6141,7 @@ ZEND_METHOD(reflection_reference, fromArrayElement) } else if (Z_TYPE_P(key) == IS_STRING) { item = zend_symtable_find(ht, Z_STR_P(key)); } else { - zend_type_error("Key must be array or string"); + zend_argument_type_error(2, "must be of type string|int, %s given", zend_zval_type_name(key)); RETURN_THROWS(); } diff --git a/ext/reflection/tests/008.phpt b/ext/reflection/tests/008.phpt index 10806b0015..0b9a867884 100644 --- a/ext/reflection/tests/008.phpt +++ b/ext/reflection/tests/008.phpt @@ -27,13 +27,13 @@ foreach ($a as $key=>$val) { echo "Done\n"; ?> --EXPECT-- -string(20) "Invalid method name " -string(21) "Invalid method name 1" +string(91) "ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name" +string(91) "ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name" string(21) "Class does not exist" string(22) "Class a does not exist" string(21) "Class does not exist" string(22) "Class a does not exist" string(21) "Class does not exist" -string(66) "The parameter class is expected to be either a string or an object" +string(104) "ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be of type object|string, int given" string(21) "Class does not exist" Done diff --git a/ext/reflection/tests/ReflectionClass_implementsInterface_001.phpt b/ext/reflection/tests/ReflectionClass_implementsInterface_001.phpt index 7f28198dfa..d912b568e8 100644 --- a/ext/reflection/tests/ReflectionClass_implementsInterface_001.phpt +++ b/ext/reflection/tests/ReflectionClass_implementsInterface_001.phpt @@ -65,7 +65,7 @@ try { echo $e->getMessage() . "\n"; } ?> ---EXPECTF-- +--EXPECT-- Does A implement A? - Using object argument: A is not an interface - Using string argument: A is not an interface @@ -146,6 +146,6 @@ Does I2 implement I2? Test bad arguments: ReflectionClass::implementsInterface() expects exactly 1 parameter, 0 given ReflectionClass::implementsInterface() expects exactly 1 parameter, 2 given -Parameter one must either be a string or a ReflectionClass object +ReflectionClass::implementsInterface(): Argument #1 ($interface) must be of type ReflectionClass|string, null given Interface ThisClassDoesNotExist does not exist -Parameter one must either be a string or a ReflectionClass object +ReflectionClass::implementsInterface(): Argument #1 ($interface) must be of type ReflectionClass|string, int given diff --git a/ext/reflection/tests/ReflectionClass_isSubclassOf_002.phpt b/ext/reflection/tests/ReflectionClass_isSubclassOf_002.phpt index b86b70018d..6d4ae500a9 100644 --- a/ext/reflection/tests/ReflectionClass_isSubclassOf_002.phpt +++ b/ext/reflection/tests/ReflectionClass_isSubclassOf_002.phpt @@ -39,6 +39,6 @@ try { Test bad arguments: ReflectionClass::isSubclassOf() expects exactly 1 parameter, 0 given ReflectionClass::isSubclassOf() expects exactly 1 parameter, 2 given -Parameter one must either be a string or a ReflectionClass object +ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, null given Class ThisClassDoesNotExist does not exist -Parameter one must either be a string or a ReflectionClass object +ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, int given diff --git a/ext/reflection/tests/ReflectionMethod_constructor_error1.phpt b/ext/reflection/tests/ReflectionMethod_constructor_error1.phpt index 186c61b2b0..391beced52 100644 --- a/ext/reflection/tests/ReflectionMethod_constructor_error1.phpt +++ b/ext/reflection/tests/ReflectionMethod_constructor_error1.phpt @@ -65,17 +65,17 @@ try { ?> --EXPECTF-- Wrong type of argument (bool): -ReflectionException: Invalid method name 1 in %s +ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name in %s:%d Stack trace: #0 %s ReflectionMethod->__construct('1') #1 {main} Wrong type of argument (int): -ReflectionException: Invalid method name 3 in %s +ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name in %s:%d Stack trace: #0 %s ReflectionMethod->__construct('3') #1 {main} Wrong type of argument (bool, string): -ReflectionException: The parameter class is expected to be either a string or an object in %s +ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be of type object|string, bool given in %s:%d Stack trace: #0 %s ReflectionMethod->__construct(true, 'foo') #1 {main} @@ -85,7 +85,7 @@ Stack trace: #0 %s ReflectionMethod->__construct('TestClass', '1') #1 {main} No method given: -ReflectionException: Invalid method name TestClass in %s +ReflectionException: ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be a valid method name in %s:%d Stack trace: #0 %s ReflectionMethod->__construct('TestClass') #1 {main} diff --git a/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt b/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt index d84fa5dcff..cf0d3bbbaa 100644 --- a/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt +++ b/ext/reflection/tests/ReflectionMethod_constructor_error2.phpt @@ -57,5 +57,5 @@ Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 0 given Too many arguments: Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 3 given Ok - Class InvalidClassName does not exist -Ok - The parameter class is expected to be either a string or an object +Ok - ReflectionMethod::__construct(): Argument #1 ($class_or_method) must be of type object|string, array given Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 2 given diff --git a/ext/reflection/tests/ReflectionObject_isSubclassOf.002.phpt b/ext/reflection/tests/ReflectionObject_isSubclassOf.002.phpt index 5cebd43016..30f769aa3d 100644 --- a/ext/reflection/tests/ReflectionObject_isSubclassOf.002.phpt +++ b/ext/reflection/tests/ReflectionObject_isSubclassOf.002.phpt @@ -39,6 +39,6 @@ try { Test bad arguments: ReflectionClass::isSubclassOf() expects exactly 1 parameter, 0 given ReflectionClass::isSubclassOf() expects exactly 1 parameter, 2 given -Parameter one must either be a string or a ReflectionClass object +ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, null given Class ThisClassDoesNotExist does not exist -Parameter one must either be a string or a ReflectionClass object +ReflectionClass::isSubclassOf(): Argument #1 ($class) must be of type ReflectionClass|string, int given diff --git a/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt b/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt index ee6ae86cba..fd83e30aaa 100644 --- a/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt +++ b/ext/reflection/tests/ReflectionParameter_invalidMethodInConstructor.phpt @@ -44,5 +44,5 @@ Class A does not exist Method C::b() does not exist Method C::b() does not exist Ok - ReflectionParameter::__construct() expects exactly 2 parameters, 1 given -Ok - The parameter class is expected to be either a string, an array(class, method) or a callable object +Ok - ReflectionParameter::__construct(): Argument #1 ($function) must be either a string, an array(class, method) or a callable object, int given Done. diff --git a/ext/reflection/tests/ReflectionProperty_constructor_error.phpt b/ext/reflection/tests/ReflectionProperty_constructor_error.phpt index 38a34681db..a2df954309 100644 --- a/ext/reflection/tests/ReflectionProperty_constructor_error.phpt +++ b/ext/reflection/tests/ReflectionProperty_constructor_error.phpt @@ -38,7 +38,7 @@ Non-existent class: Class NonExistentClass does not exist Wrong property parameter type: -The parameter class is expected to be either a string or an object +ReflectionProperty::__construct(): Argument #1 ($class) must be of type object|string, int given Non-existent property: Property TestClass::$nonExistentProperty does not exist diff --git a/ext/reflection/tests/ReflectionReference_errors.phpt b/ext/reflection/tests/ReflectionReference_errors.phpt index 33154ba22e..88244b7940 100644 --- a/ext/reflection/tests/ReflectionReference_errors.phpt +++ b/ext/reflection/tests/ReflectionReference_errors.phpt @@ -42,7 +42,7 @@ var_dump(unserialize('O:19:"ReflectionReference":0:{}')); --EXPECTF-- Call to private ReflectionReference::__construct() from invalid context ReflectionReference::fromArrayElement(): Argument #1 ($array) must be of type array, object given -Key must be array or string +ReflectionReference::fromArrayElement(): Argument #2 ($key) must be of type string|int, float given Array key not found Serialization of 'ReflectionReference' is not allowed -- 2.50.1