Skip to content

Commit 83449b4

Browse files
Merge branch 'add-const-name' of git://github.com/reeze/php-src into PHP-5.4
2 parents 0ad53bf + 6712d0d commit 83449b4

5 files changed

+193
-16
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ PHP NEWS
171171
bytes). (Nikita Popov)
172172

173173
- Reflection:
174+
. Implemented FR #61602 (Allow access to the name of constant
175+
used as function/method parameter's default value). (reeze.xia@gmail.com)
174176
. Fixed bug #60968 (Late static binding doesn't work with
175177
ReflectionMethod::invokeArgs()). (Laruence)
176178

ext/reflection/php_reflection.c

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,49 @@ static void _reflection_export(INTERNAL_FUNCTION_PARAMETERS, zend_class_entry *c
14571457
}
14581458
/* }}} */
14591459

1460+
/* {{{ _reflection_param_get_default_param */
1461+
static parameter_reference *_reflection_param_get_default_param(INTERNAL_FUNCTION_PARAMETERS)
1462+
{
1463+
reflection_object *intern;
1464+
parameter_reference *param;
1465+
1466+
GET_REFLECTION_OBJECT_PTR(param);
1467+
1468+
if (param->fptr->type != ZEND_USER_FUNCTION)
1469+
{
1470+
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Cannot determine default value for internal functions");
1471+
return NULL;
1472+
}
1473+
1474+
if (param->offset < param->required) {
1475+
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Parameter is not optional");
1476+
return NULL;
1477+
}
1478+
1479+
return param;
1480+
}
1481+
/* }}} */
1482+
1483+
/* {{{ _reflection_param_get_default_precv */
1484+
static zend_op *_reflection_param_get_default_precv(INTERNAL_FUNCTION_PARAMETERS, parameter_reference *param)
1485+
{
1486+
zend_op *precv;
1487+
1488+
param = param ? param : _reflection_param_get_default_param(INTERNAL_FUNCTION_PARAM_PASSTHRU);
1489+
if (!param) {
1490+
return NULL;
1491+
}
1492+
1493+
precv = _get_recv_op((zend_op_array*)param->fptr, param->offset);
1494+
if (!precv || precv->opcode != ZEND_RECV_INIT || precv->op2_type == IS_UNUSED) {
1495+
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Internal error");
1496+
return NULL;
1497+
}
1498+
1499+
return precv;
1500+
}
1501+
/* }}} */
1502+
14601503
/* {{{ Preventing __clone from being called */
14611504
ZEND_METHOD(reflection, __clone)
14621505
{
@@ -2535,27 +2578,14 @@ ZEND_METHOD(reflection_parameter, isDefaultValueAvailable)
25352578
Returns the default value of this parameter or throws an exception */
25362579
ZEND_METHOD(reflection_parameter, getDefaultValue)
25372580
{
2538-
reflection_object *intern;
2539-
parameter_reference *param;
2540-
zend_op *precv;
2581+
parameter_reference *param = _reflection_param_get_default_param(INTERNAL_FUNCTION_PARAM_PASSTHRU);
2582+
zend_op *precv = _reflection_param_get_default_precv(INTERNAL_FUNCTION_PARAM_PASSTHRU, param);
25412583

25422584
if (zend_parse_parameters_none() == FAILURE) {
25432585
return;
25442586
}
2545-
GET_REFLECTION_OBJECT_PTR(param);
25462587

2547-
if (param->fptr->type != ZEND_USER_FUNCTION)
2548-
{
2549-
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Cannot determine default value for internal functions");
2550-
return;
2551-
}
2552-
if (param->offset < param->required) {
2553-
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Parameter is not optional");
2554-
return;
2555-
}
2556-
precv = _get_recv_op((zend_op_array*)param->fptr, param->offset);
2557-
if (!precv || precv->opcode != ZEND_RECV_INIT || precv->op2_type == IS_UNUSED) {
2558-
zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Internal error");
2588+
if (!(param && precv)) {
25592589
return;
25602590
}
25612591

@@ -2568,6 +2598,42 @@ ZEND_METHOD(reflection_parameter, getDefaultValue)
25682598
}
25692599
/* }}} */
25702600

2601+
/* {{{ proto public bool ReflectionParameter::isDefaultValueConstant()
2602+
Returns whether the default value of this parameter is constant */
2603+
ZEND_METHOD(reflection_parameter, isDefaultValueConstant)
2604+
{
2605+
zend_op *precv = _reflection_param_get_default_precv(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
2606+
2607+
if (zend_parse_parameters_none() == FAILURE) {
2608+
return;
2609+
}
2610+
2611+
if (precv && (Z_TYPE_P(precv->op2.zv) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
2612+
RETURN_TRUE;
2613+
}
2614+
2615+
RETURN_FALSE;
2616+
}
2617+
/* }}} */
2618+
2619+
/* {{{ proto public mixed ReflectionParameter::getDefaultValueConstantName()
2620+
Returns the default value's constant name if default value is constant or false */
2621+
ZEND_METHOD(reflection_parameter, getDefaultValueConstantName)
2622+
{
2623+
zend_op *precv = _reflection_param_get_default_precv(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
2624+
2625+
if (zend_parse_parameters_none() == FAILURE) {
2626+
return;
2627+
}
2628+
2629+
if (precv && (Z_TYPE_P(precv->op2.zv) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
2630+
RETURN_STRING(Z_STRVAL_P(precv->op2.zv), 1);
2631+
}
2632+
2633+
RETURN_FALSE;
2634+
}
2635+
/* }}} */
2636+
25712637
/* {{{ proto public static mixed ReflectionMethod::export(mixed class, string name [, bool return]) throws ReflectionException
25722638
Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
25732639
ZEND_METHOD(reflection_method, export)
@@ -5903,6 +5969,8 @@ static const zend_function_entry reflection_parameter_functions[] = {
59035969
ZEND_ME(reflection_parameter, isOptional, arginfo_reflection__void, 0)
59045970
ZEND_ME(reflection_parameter, isDefaultValueAvailable, arginfo_reflection__void, 0)
59055971
ZEND_ME(reflection_parameter, getDefaultValue, arginfo_reflection__void, 0)
5972+
ZEND_ME(reflection_parameter, isDefaultValueConstant, arginfo_reflection__void, 0)
5973+
ZEND_ME(reflection_parameter, getDefaultValueConstantName, arginfo_reflection__void, 0)
59065974
PHP_FE_END
59075975
};
59085976

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
ReflectionParameter::isDefaultValueConstant() && getDefaultValueConstantName()
3+
--FILE--
4+
<?php
5+
6+
define("CONST_TEST_1", "const1");
7+
8+
function ReflectionParameterTest($test1=array(), $test2 = CONST_TEST_1) {
9+
echo $test;
10+
}
11+
$reflect = new ReflectionFunction('ReflectionParameterTest');
12+
foreach($reflect->getParameters() as $param) {
13+
if($param->getName() == 'test1') {
14+
var_dump($param->isDefaultValueConstant());
15+
}
16+
if($param->getName() == 'test2') {
17+
var_dump($param->isDefaultValueConstant());
18+
}
19+
if($param->isDefaultValueAvailable() && $param->isDefaultValueConstant()) {
20+
var_dump($param->getDefaultValueConstantName());
21+
}
22+
}
23+
24+
class Foo2 {
25+
const bar = 'Foo2::bar';
26+
}
27+
28+
class Foo {
29+
const bar = 'Foo::bar';
30+
31+
public function baz($param1 = self::bar, $param2=Foo2::bar, $param3=CONST_TEST_1) {
32+
}
33+
}
34+
35+
$method = new ReflectionMethod('Foo', 'baz');
36+
$params = $method->getParameters();
37+
38+
foreach ($params as $param) {
39+
if ($param->isDefaultValueConstant()) {
40+
var_dump($param->getDefaultValueConstantName());
41+
}
42+
}
43+
?>
44+
==DONE==
45+
--EXPECT--
46+
bool(false)
47+
bool(true)
48+
string(12) "CONST_TEST_1"
49+
string(9) "self::bar"
50+
string(9) "Foo2::bar"
51+
string(12) "CONST_TEST_1"
52+
==DONE==
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
ReflectionParameter::isDefaultValueConstant() && getDefaultValueConstantName() for namespace
3+
--FILE--
4+
<?php
5+
6+
namespace ReflectionTestNamespace {
7+
CONST TEST_CONST_1 = "Test Const 1";
8+
9+
class TestClass {
10+
const TEST_CONST_2 = "Test Const 2 in class";
11+
}
12+
}
13+
14+
namespace {
15+
function ReflectionParameterTest($test=ReflectionTestNamespace\TestClass::TEST_CONST_2, $test2 = ReflectionTestNamespace\CONST_TEST_1) {
16+
echo $test;
17+
}
18+
$reflect = new ReflectionFunction('ReflectionParameterTest');
19+
foreach($reflect->getParameters() as $param) {
20+
if($param->isDefaultValueAvailable() && $param->isDefaultValueConstant()) {
21+
echo $param->getDefaultValueConstantName() . "\n";
22+
}
23+
}
24+
echo "==DONE==";
25+
}
26+
?>
27+
--EXPECT--
28+
ReflectionTestNamespace\TestClass::TEST_CONST_2
29+
ReflectionTestNamespace\CONST_TEST_1
30+
==DONE==
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
ReflectionParameter::getDefaultValueConstant() should raise exception on non optional parameter
3+
--FILE--
4+
<?php
5+
6+
define("CONST_TEST_1", "const1");
7+
8+
function ReflectionParameterTest($test, $test2 = CONST_TEST_1) {
9+
echo $test;
10+
}
11+
$reflect = new ReflectionFunction('ReflectionParameterTest');
12+
foreach($reflect->getParameters() as $param) {
13+
try {
14+
echo $param->getDefaultValueConstantName() . "\n";
15+
}
16+
catch(ReflectionException $e) {
17+
echo $e->getMessage() . "\n";
18+
}
19+
}
20+
?>
21+
==DONE==
22+
--EXPECT--
23+
Parameter is not optional
24+
CONST_TEST_1
25+
==DONE==

0 commit comments

Comments
 (0)