Skip to content

Commit

Permalink
Fix reflection getDefaultValue() with user arg info
Browse files Browse the repository at this point in the history
The default value is part of the op_array in that case, but we have
no way to access it. Fail gracefully.
  • Loading branch information
nikic committed Nov 12, 2020
1 parent 9acebe1 commit d033d5c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,10 @@ static void reflection_class_constant_factory(zend_string *name_str, zend_class_

static int get_parameter_default(zval *result, parameter_reference *param) {
if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
if (param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
/* We don't have a way to determine the default value for this case right now. */
return FAILURE;
}
return zend_get_default_from_internal_arg_info(
result, (zend_internal_arg_info *) param->arg_info);
} else {
Expand Down Expand Up @@ -2717,7 +2721,8 @@ ZEND_METHOD(ReflectionParameter, isDefaultValueAvailable)
GET_REFLECTION_OBJECT_PTR(param);

if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
RETURN_BOOL(((zend_internal_arg_info*) (param->arg_info))->default_value);
RETURN_BOOL(!(param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO)
&& ((zend_internal_arg_info*) (param->arg_info))->default_value);
} else {
zval *default_value = get_default_from_recv((zend_op_array *)param->fptr, param->offset);
RETURN_BOOL(default_value != NULL);
Expand Down
11 changes: 11 additions & 0 deletions ext/reflection/tests/default_value_internal_userland_arginfo.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ $closure = function ($b = 0) {};
$ro = new ReflectionObject($closure);
$rm = $ro->getMethod('__invoke');
echo $rm, "\n";

$rp = $rm->getParameters()[0];
var_dump($rp->isDefaultValueAvailable());
try {
var_dump($rp->getDefaultValue());
} catch (ReflectionException $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Method [ <internal> public method __invoke ] {
Expand All @@ -14,3 +22,6 @@ Method [ <internal> public method __invoke ] {
Parameter #0 [ <optional> $b = <default> ]
}
}

bool(false)
Internal error: Failed to retrieve the default value

0 comments on commit d033d5c

Please sign in to comment.