Fixed ReflectionFunction, ReflectionMethod and ReflectionParameter.

This commit is contained in:
Danack 2015-03-01 13:06:58 +00:00
parent b00cd68d74
commit 043a02605f
6 changed files with 133 additions and 54 deletions

View File

@ -18,7 +18,7 @@ catch(PDOException $pe) {
}
}
catch(\Exception $e) {
echo "Failed to ";
echo "Exception throw was not of type PDOException instead was ".get_class($e).PHP_EOL;
}
?>

View File

@ -1576,6 +1576,8 @@ ZEND_METHOD(reflection_function, __construct)
zend_function *fptr;
char *name_str;
size_t name_len;
int rv;
zend_error_handling zeh;
object = getThis();
intern = Z_REFLECTION_P(object);
@ -1586,27 +1588,32 @@ ZEND_METHOD(reflection_function, __construct)
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "O", &closure, zend_ce_closure) == SUCCESS) {
fptr = (zend_function*)zend_get_closure_method_def(closure);
Z_ADDREF_P(closure);
} else if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == SUCCESS) {
char *nsname;
} else {
zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC);
rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len);
zend_restore_error_handling(&zeh TSRMLS_CC);
if (rv == SUCCESS) {
char *nsname;
lcname = zend_str_tolower_dup(name_str, name_len);
lcname = zend_str_tolower_dup(name_str, name_len);
/* Ignore leading "\" */
nsname = lcname;
if (lcname[0] == '\\') {
nsname = &lcname[1];
name_len--;
}
/* Ignore leading "\" */
nsname = lcname;
if (lcname[0] == '\\') {
nsname = &lcname[1];
name_len--;
}
if ((fptr = zend_hash_str_find_ptr(EG(function_table), nsname, name_len)) == NULL) {
if ((fptr = zend_hash_str_find_ptr(EG(function_table), nsname, name_len)) == NULL) {
efree(lcname);
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Function %s() does not exist", name_str);
return;
}
efree(lcname);
zend_throw_exception_ex(reflection_exception_ptr, 0,
"Function %s() does not exist", name_str);
} else {
//Exception has been thrown.
return;
}
efree(lcname);
} else {
return;
}
ZVAL_STR_COPY(&name, fptr->common.function_name);
@ -2128,10 +2135,14 @@ ZEND_METHOD(reflection_parameter, __construct)
zend_class_entry *ce = NULL;
zend_bool is_closure = 0;
zend_bool is_invoke = 0;
zend_error_handling zeh;
zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &reference, &parameter) == FAILURE) {
zend_restore_error_handling(&zeh TSRMLS_CC);
return;
}
zend_restore_error_handling(&zeh TSRMLS_CC);
object = getThis();
intern = Z_REFLECTION_P(object);
@ -2712,7 +2723,13 @@ ZEND_METHOD(reflection_method, __construct)
zval ztmp;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "zs", &classname, &name_str, &name_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len) == FAILURE) {
zend_error_handling zeh;
int rv;
zend_replace_error_handling(EH_THROW, reflection_exception_ptr, &zeh TSRMLS_CC);
rv = zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name_str, &name_len);
zend_restore_error_handling(&zeh TSRMLS_CC);
if (rv == FAILURE) {
return;
}
if ((tmp = strstr(name_str, "::")) == NULL) {

View File

@ -6,18 +6,41 @@ Steve Seear <stevseea@php.net>
--FILE--
<?php
$a = new ReflectionFunction(array(1, 2, 3));
try {
$a = new ReflectionFunction(array(1, 2, 3));
echo "exception not thrown.".PHP_EOL;
}
catch(ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
$a = new ReflectionFunction('nonExistentFunction');
} catch (Exception $e) {
echo $e->getMessage();
echo $e->getMessage().PHP_EOL;
}
$a = new ReflectionFunction();
$a = new ReflectionFunction(1, 2);
try {
$a = new ReflectionFunction();
}
catch(ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
$a = new ReflectionFunction(1, 2);
}
catch(ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
$a = new ReflectionFunction([]);
}
catch(ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
?>
--EXPECTF--
Warning: ReflectionFunction::__construct() expects parameter 1 to be string, array given in %s on line %d
Ok - ReflectionFunction::__construct() expects parameter 1 to be string, array given
Function nonExistentFunction() does not exist
Warning: ReflectionFunction::__construct() expects exactly 1 parameter, 0 given in %s on line %d
Warning: ReflectionFunction::__construct() expects exactly 1 parameter, 2 given in %s on line %d
Ok - ReflectionFunction::__construct() expects exactly 1 parameter, 0 given
Ok - ReflectionFunction::__construct() expects exactly 1 parameter, 2 given
Ok - ReflectionFunction::__construct() expects parameter 1 to be string, array given

View File

@ -6,8 +6,18 @@ Steve Seear <stevseea@php.net>
--FILE--
<?php
var_dump(new ReflectionMethod());
var_dump(new ReflectionMethod('a', 'b', 'c'));
try {
new ReflectionMethod();
}
catch(ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
new ReflectionMethod('a', 'b', 'c');
}
catch(ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
class C {
public function f() {}
@ -35,21 +45,8 @@ var_dump($rm->getName(1));
?>
--EXPECTF--
Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 0 given in %s on line %d
object(ReflectionMethod)#%d (2) {
["name"]=>
string(0) ""
["class"]=>
string(0) ""
}
Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 3 given in %s on line %d
object(ReflectionMethod)#%d (2) {
["name"]=>
string(0) ""
["class"]=>
string(0) ""
}
Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 0 given
Ok - ReflectionMethod::__construct() expects exactly 1 parameter, 3 given
Warning: ReflectionMethod::isFinal() expects exactly 0 parameters, 1 given in %s on line %d
NULL

View File

@ -16,22 +16,47 @@ class TestClass
try {
echo "Too few arguments:\n";
$methodInfo = new ReflectionMethod();
} catch (Exception $e) {
print $e->__toString();
} catch (ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
echo "\nToo many arguments:\n";
$methodInfo = new ReflectionMethod("TestClass", "foo", true);
} catch (Exception $e) {
print $e->__toString();
} catch (ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
//invalid class
$methodInfo = new ReflectionMethod("InvalidClassName", "foo");
} catch (ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try {
//invalid 1st param
$methodInfo = new ReflectionMethod([], "foo");
} catch (ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
try{
//invalid 2nd param
$methodInfo = new ReflectionMethod("TestClass", []);
} catch (ReflectionException $re) {
echo "Ok - ".$re->getMessage().PHP_EOL;
}
?>
--EXPECTF--
Too few arguments:
Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 0 given in %s on line 12
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() expects exactly 1 parameter, 2 given
Warning: ReflectionMethod::__construct() expects exactly 1 parameter, 3 given in %s on line 18

View File

@ -6,7 +6,7 @@ ReflectionParameter::__construct(): Invalid method as constructor
// Invalid class name
try {
new ReflectionParameter (array ('A', 'b'), 0);
} catch (ReflectionException $e) { echo $e->getMessage ()."\n"; }
} catch (ReflectionException $e) { echo $e->getMessage()."\n"; }
// Invalid class method
try {
@ -18,14 +18,31 @@ try {
new ReflectionParameter (array (new C, 'b'), 0);
} catch (ReflectionException $e) { echo $e->getMessage ()."\n"; }
echo "Done.\n";
class C {
}
try {
new ReflectionParameter(array ('A', 'b'));
}
catch(ReflectionException $e) {
printf( "Ok - %s\n", $e->getMessage());
}
try {
new ReflectionParameter(0, 0);
}
catch(ReflectionException $e) {
printf( "Ok - %s\n", $e->getMessage());
}
echo "Done.\n";
?>
--EXPECTF--
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
Done.