Skip to content

Permit Type-Hinting from extensions #107

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
wants to merge 2 commits into from
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
3 changes: 0 additions & 3 deletions Zend/zend_execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,6 @@ static inline int zend_verify_arg_type(zend_function *zf, zend_uint arg_num, zva
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be callable", "", zend_zval_type_name(arg), "" TSRMLS_CC);
}
break;

default:
zend_error(E_ERROR, "Unknown typehint");
}
}
return 1;
Expand Down
30 changes: 25 additions & 5 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2390,6 +2390,25 @@ ZEND_METHOD(reflection_parameter, getClass)
}
/* }}} */

/* {{{ proto public string|NULL ReflectionParameter::getTypeHint()
Returns the type hint, or NULL if there is none */
ZEND_METHOD(reflection_parameter, getTypeHint)
{
reflection_object *intern;
parameter_reference *param;

if (zend_parse_parameters_none() == FAILURE) {
return;
}
GET_REFLECTION_OBJECT_PTR(param);

if (!param->arg_info->type_hint) {
RETVAL_NULL();
} else {
RETVAL_STRING(zend_get_type_by_const(param->arg_info->type_hint), 1);
}
}

/* {{{ proto public bool ReflectionParameter::isArray()
Returns whether parameter MUST be an array */
ZEND_METHOD(reflection_parameter, isArray)
Expand Down Expand Up @@ -5876,14 +5895,14 @@ static const zend_function_entry reflection_property_functions[] = {
};

ZEND_BEGIN_ARG_INFO_EX(arginfo_reflection_parameter_export, 0, 0, 2)
ZEND_ARG_INFO(0, function)
ZEND_ARG_INFO(0, parameter)
ZEND_ARG_INFO(0, return)
ZEND_ARG_TYPE_INFO(0, function, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, parameter, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, return, IS_BOOL, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_reflection_parameter___construct, 0)
ZEND_ARG_INFO(0, function)
ZEND_ARG_INFO(0, parameter)
ZEND_ARG_TYPE_INFO(0, function, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, parameter, IS_STRING, 0)
ZEND_END_ARG_INFO()

static const zend_function_entry reflection_parameter_functions[] = {
Expand All @@ -5897,6 +5916,7 @@ static const zend_function_entry reflection_parameter_functions[] = {
ZEND_ME(reflection_parameter, getDeclaringFunction, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getDeclaringClass, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getClass, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, getTypeHint, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, isArray, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, isCallable, arginfo_reflection__void, 0)
ZEND_ME(reflection_parameter, allowsNull, arginfo_reflection__void, 0)
Expand Down
35 changes: 35 additions & 0 deletions tests/classes/type_hinting_006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Getting the right type hinting information
--FILE--
<?php
$c = new ReflectionClass("ReflectionParameter");

// Method with type info
$m = $c->getMethod("export");
$p = $m->getParameters();
foreach ($p as $i)
var_dump($i->getTypeHint());
echo "\n";

// Other method with type info
$m = $c->getMethod("__construct");
$p = $m->getParameters();
foreach ($p as $i)
var_dump($i->getTypeHint());
echo "\n";

// Function with missing type info (at least for now)
$f = new ReflectionFunction("urlencode");
$p = $f->getParameters();
foreach ($p as $i)
var_dump($i->getTypeHint());
?>
--EXPECT--
string(6) "string"
string(6) "string"
string(7) "boolean"

string(6) "string"
string(6) "string"

NULL