Skip to content

Commit f88eb41

Browse files
committed
Renamed public reflection struct and method.
Added tests for anonymous classes, callables and closures.
1 parent bdafc3f commit f88eb41

13 files changed

+75
-22
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Tests passing default to anonymous class methods
3+
--FILE--
4+
<?php
5+
6+
$C = new class {
7+
function __invoke($V = 'Alfa') {}
8+
function F($V = 'Bravo') {}
9+
};
10+
$C($D = default);
11+
var_dump($D);
12+
13+
$C->F($D = default);
14+
var_dump($D);
15+
?>
16+
--EXPECT--
17+
string(4) "Alfa"
18+
string(5) "Bravo"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Tests passing default to a callable object
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
function __invoke($V = 'Alfa') {}
8+
}
9+
new C()($D = default);
10+
var_dump($D);
11+
?>
12+
--EXPECT--
13+
string(4) "Alfa"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Tests passing default to closures
3+
--FILE--
4+
<?php
5+
6+
$F = function ($X = 1, $Y = 2) {
7+
return $X + $Y;
8+
};
9+
var_dump($F(default, $V = default));
10+
var_dump($V);
11+
12+
$F2 = fn ($P = 'Alfa') => $P;
13+
var_dump($F2($V = default));
14+
var_dump($V);
15+
?>
16+
--EXPECT--
17+
int(3)
18+
int(2)
19+
string(4) "Alfa"
20+
string(4) "Alfa"

Zend/tests/default_expression/default_expressions.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Tests an exhaustive list of valid expressions containing the default keyword
33
--FILE--
44
<?php
5+
56
function F($V = 2) { return $V; }
67

78
var_dump(F(default + 1));

Zend/tests/default_expression/multiple_arguments.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test passing multiple default arguments to global user function parameters
2+
Tests passing multiple default arguments to global user function parameters
33
--FILE--
44
<?php
55

Zend/tests/default_expression/nested_calls.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
--TEST--
2-
Test passing default in a parent argument position that is less than the child call's total number of arguments
2+
Tests passing default in a parent argument position that is less than the child call's total number of arguments
33
--FILE--
44
<?php
55

6-
function F($x = 1, $y = 2) {
7-
return $x + $y;
6+
function F($X = 1, $Y = 2) {
7+
return $X + $Y;
88
}
99
var_dump(F(F(0, 1) + default));
1010
?>

Zend/tests/default_expression/new_class_parameter.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test passing default to a parameter with a new class as the default
2+
Tests passing default to a parameter with a new class as the default
33
--FILE--
44
<?php
55

Zend/zend_vm_def.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9818,7 +9818,7 @@ ZEND_VM_HANDLER(210, ZEND_FETCH_DEFAULT_ARG, UNUSED|NUM, UNUSED)
98189818

98199819
zend_function *called_func = EX(call)->func;
98209820

9821-
parameter_reference param;
9821+
reflection_parameter_reference param;
98229822
param.offset = opline->op1.num - 1;
98239823
param.required = param.offset < called_func->common.required_num_args;
98249824
param.arg_info = &called_func->common.arg_info[param.offset];
@@ -9835,13 +9835,12 @@ ZEND_VM_HANDLER(210, ZEND_FETCH_DEFAULT_ARG, UNUSED|NUM, UNUSED)
98359835
}
98369836

98379837
zval default_value;
9838-
get_parameter_default(&default_value, &param);
9838+
reflection_get_parameter_default(&default_value, &param);
98399839

98409840
// Evaluate AST value, e.g. new class.
9841-
if (Z_TYPE(default_value) == IS_CONSTANT_AST) {
9842-
if (UNEXPECTED(zval_update_constant_ex(&default_value, called_func->common.scope) != SUCCESS)) {
9841+
if (Z_TYPE(default_value) == IS_CONSTANT_AST
9842+
&& UNEXPECTED(zval_update_constant_ex(&default_value, called_func->common.scope) != SUCCESS)) {
98439843
HANDLE_EXCEPTION();
9844-
}
98459844
}
98469845

98479846
ZVAL_COPY_VALUE(EX_VAR(opline->result.var), &default_value);

Zend/zend_vm_execute.h

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/reflection/php_reflection.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ typedef struct _property_reference {
130130
zend_string *unmangled_name;
131131
} property_reference;
132132

133+
/* Struct for parameters */
134+
typedef reflection_parameter_reference parameter_reference;
135+
133136
/* Struct for type hints */
134137
typedef struct _type_reference {
135138
zend_type type;
@@ -1528,7 +1531,7 @@ static void reflection_enum_case_factory(zend_class_entry *ce, zend_string *name
15281531
ZVAL_STR_COPY(reflection_prop_class(object), constant->ce->name);
15291532
}
15301533

1531-
PHPAPI int get_parameter_default(zval *result, parameter_reference *param) {
1534+
PHPAPI int reflection_get_parameter_default(zval *result, parameter_reference *param) {
15321535
if (param->fptr->type == ZEND_INTERNAL_FUNCTION) {
15331536
if (param->fptr->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
15341537
/* We don't have a way to determine the default value for this case right now. */
@@ -2925,7 +2928,7 @@ ZEND_METHOD(ReflectionParameter, getDefaultValue)
29252928

29262929
GET_REFLECTION_OBJECT_PTR(param);
29272930

2928-
if (get_parameter_default(return_value, param) == FAILURE) {
2931+
if (reflection_get_parameter_default(return_value, param) == FAILURE) {
29292932
zend_throw_exception_ex(reflection_exception_ptr, 0,
29302933
"Internal error: Failed to retrieve the default value");
29312934
RETURN_THROWS();
@@ -2950,7 +2953,7 @@ ZEND_METHOD(ReflectionParameter, isDefaultValueConstant)
29502953
GET_REFLECTION_OBJECT_PTR(param);
29512954

29522955
zval default_value;
2953-
if (get_parameter_default(&default_value, param) == FAILURE) {
2956+
if (reflection_get_parameter_default(&default_value, param) == FAILURE) {
29542957
zend_throw_exception_ex(reflection_exception_ptr, 0,
29552958
"Internal error: Failed to retrieve the default value");
29562959
RETURN_THROWS();
@@ -2982,7 +2985,7 @@ ZEND_METHOD(ReflectionParameter, getDefaultValueConstantName)
29822985
GET_REFLECTION_OBJECT_PTR(param);
29832986

29842987
zval default_value;
2985-
if (get_parameter_default(&default_value, param) == FAILURE) {
2988+
if (reflection_get_parameter_default(&default_value, param) == FAILURE) {
29862989
zend_throw_exception_ex(reflection_exception_ptr, 0,
29872990
"Internal error: Failed to retrieve the default value");
29882991
RETURN_THROWS();

ext/reflection/php_reflection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ extern PHPAPI zend_class_entry *reflection_fiber_ptr;
5151
PHPAPI void zend_reflection_class_factory(zend_class_entry *ce, zval *object);
5252

5353
/* Struct for parameters */
54-
typedef struct _parameter_reference {
54+
typedef struct _reflection_parameter_reference {
5555
uint32_t offset;
5656
bool required;
5757
struct _zend_arg_info *arg_info;
5858
zend_function *fptr;
59-
} parameter_reference;
59+
} reflection_parameter_reference;
6060

61-
PHPAPI int get_parameter_default(zval *result, parameter_reference *param);
61+
PHPAPI int reflection_get_parameter_default(zval *result, reflection_parameter_reference *param);
6262

6363
END_EXTERN_C()
6464

0 commit comments

Comments
 (0)