Skip to content

Commit d352793

Browse files
committed
Add ReflectionParameter::hasTypehint() and ...::getTypehintText()
1 parent f4a235c commit d352793

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

ext/reflection/php_reflection.c

+44
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,48 @@ ZEND_METHOD(reflection_parameter, getClass)
24342434
}
24352435
/* }}} */
24362436

2437+
/* {{{ proto public bool ReflectionParameter::hasTypehint()
2438+
Rethern whether parameter has a type hint */
2439+
ZEND_METHOD(reflection_parameter, hasTypehint)
2440+
{
2441+
reflection_object *intern;
2442+
parameter_reference *param;
2443+
2444+
if (zend_parse_parameters_none() == FAILURE) {
2445+
return;
2446+
}
2447+
GET_REFLECTION_OBJECT_PTR(param);
2448+
2449+
RETVAL_BOOL(param->arg_info->type_hint != 0);
2450+
}
2451+
/* }}} */
2452+
2453+
/* {{{ proto public string ReflectionParameter::getTypehintText()
2454+
Returns the typehint associated with the parameter */
2455+
ZEND_METHOD(reflection_parameter, getTypehintText)
2456+
{
2457+
reflection_object *intern;
2458+
parameter_reference *param;
2459+
2460+
if (zend_parse_parameters_none() == FAILURE) {
2461+
return;
2462+
}
2463+
GET_REFLECTION_OBJECT_PTR(param);
2464+
2465+
switch (param->arg_info->type_hint) {
2466+
case IS_ARRAY:
2467+
RETURN_STRINGL("array", sizeof("array") - 1, 1);
2468+
case IS_CALLABLE:
2469+
RETURN_STRINGL("callable", sizeof("callable") - 1, 1);
2470+
case IS_OBJECT:
2471+
RETURN_STRINGL(param->arg_info->class_name,
2472+
param->arg_info->class_name_len, 1);
2473+
default:
2474+
RETURN_EMPTY_STRING();
2475+
}
2476+
}
2477+
/* }}} */
2478+
24372479
/* {{{ proto public bool ReflectionParameter::isArray()
24382480
Returns whether parameter MUST be an array */
24392481
ZEND_METHOD(reflection_parameter, isArray)
@@ -6026,6 +6068,8 @@ static const zend_function_entry reflection_parameter_functions[] = {
60266068
ZEND_ME(reflection_parameter, getDeclaringFunction, arginfo_reflection__void, 0)
60276069
ZEND_ME(reflection_parameter, getDeclaringClass, arginfo_reflection__void, 0)
60286070
ZEND_ME(reflection_parameter, getClass, arginfo_reflection__void, 0)
6071+
ZEND_ME(reflection_parameter, hasTypehint, arginfo_reflection__void, 0)
6072+
ZEND_ME(reflection_parameter, getTypehintText, arginfo_reflection__void, 0)
60296073
ZEND_ME(reflection_parameter, isArray, arginfo_reflection__void, 0)
60306074
ZEND_ME(reflection_parameter, isCallable, arginfo_reflection__void, 0)
60316075
ZEND_ME(reflection_parameter, allowsNull, arginfo_reflection__void, 0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
ReflectionParameter::hasTypehint() / getTypehintText()
3+
--FILE--
4+
<?php
5+
function foo(stdClass $a, array $b, callable $c, $d) { }
6+
7+
$rf = new ReflectionFunction('foo');
8+
foreach ($rf->getParameters() as $idx => $rp) {
9+
echo "** Parameter $idx\n";
10+
var_dump($rp->hasTypehint());
11+
var_dump($rp->getTypehintText());
12+
}
13+
--EXPECT--
14+
** Parameter 0
15+
bool(true)
16+
string(8) "stdClass"
17+
** Parameter 1
18+
bool(true)
19+
string(5) "array"
20+
** Parameter 2
21+
bool(true)
22+
string(8) "callable"
23+
** Parameter 3
24+
bool(false)
25+
string(0) ""

0 commit comments

Comments
 (0)