Skip to content

Improve error messages of ext/reflection #5277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down
6 changes: 3 additions & 3 deletions ext/reflection/tests/008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions ext/reflection/tests/ReflectionClass_isSubclassOf_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions ext/reflection/tests/ReflectionObject_isSubclassOf.002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion ext/reflection/tests/ReflectionReference_errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down