Skip to content

Commit fb5cff1

Browse files
committed
Print array defaults in reflection
As a followup to f34114b print the contents of arrays rather than just a generic "Array" marker. Also drop the truncation on strings. As we no longer resolve constants, there should be less concerns about printing very large strings here. If someone thought it was a good idea to use a 10k character strings as a default value in code, then it should be fine for us to print it in reflection as well.
1 parent 11d97ae commit fb5cff1

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

ext/reflection/php_reflection.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,33 @@ static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) {
611611

612612
static int format_default_value(smart_str *str, zval *value) {
613613
if (Z_TYPE_P(value) <= IS_STRING) {
614-
smart_str_append_scalar(str, value, 15);
614+
smart_str_append_scalar(str, value, SIZE_MAX);
615615
} else if (Z_TYPE_P(value) == IS_ARRAY) {
616-
smart_str_appends(str, "Array");
616+
zend_string *str_key;
617+
zend_long num_key;
618+
zval *zv;
619+
bool is_list = zend_array_is_list(Z_ARRVAL_P(value));
620+
bool first = true;
621+
smart_str_appendc(str, '[');
622+
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(value), num_key, str_key, zv) {
623+
if (!first) {
624+
smart_str_appends(str, ", ");
625+
}
626+
first = false;
627+
628+
if (!is_list) {
629+
if (str_key) {
630+
smart_str_appendc(str, '\'');
631+
smart_str_append_escaped(str, ZSTR_VAL(str_key), ZSTR_LEN(str_key));
632+
smart_str_appendc(str, '\'');
633+
} else {
634+
smart_str_append_long(str, num_key);
635+
}
636+
smart_str_appends(str, " => ");
637+
}
638+
format_default_value(str, zv);
639+
} ZEND_HASH_FOREACH_END();
640+
smart_str_appendc(str, ']');
617641
} else {
618642
ZEND_ASSERT(Z_TYPE_P(value) == IS_CONSTANT_AST);
619643
zend_string *ast_str = zend_ast_export("", Z_ASTVAL_P(value), "");

ext/reflection/tests/bug60357.phpt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22
Bug #60357 (__toString() method triggers E_NOTICE "Array to string conversion")
33
--FILE--
44
<?php
5-
function foo( array $x = array( 'a', 'b' ) ) {}
6-
$r = new ReflectionParameter( 'foo', 0 );
7-
echo $r->__toString();
5+
function foo(
6+
array $x = array('a', 'b'),
7+
array $y = ['x' => 'y'],
8+
array $z = [0 => 0, 2 => -2],
9+
array $a = [[], [1], [2, 3]],
10+
) {}
11+
echo new ReflectionFunction('foo'), "\n";
812
?>
9-
--EXPECT--
10-
Parameter #0 [ <optional> array $x = Array ]
13+
--EXPECTF--
14+
Function [ <user> function foo ] {
15+
@@ %s
16+
17+
- Parameters [4] {
18+
Parameter #0 [ <optional> array $x = ['a', 'b'] ]
19+
Parameter #1 [ <optional> array $y = ['x' => 'y'] ]
20+
Parameter #2 [ <optional> array $z = [0 => 0, 2 => -2] ]
21+
Parameter #3 [ <optional> array $a = [[], [1], [2, 3]] ]
22+
}
23+
}

sapi/cli/tests/005.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ string(183) "Class [ <internal:Core> class stdClass ] {
3737
}
3838

3939
"
40-
string(2235) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
40+
string(2232) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
4141

4242
- Constants [0] {
4343
}
@@ -54,7 +54,7 @@ string(2235) "Class [ <internal:Core> class Exception implements Throwable, Stri
5454
Property [ protected $code = 0 ]
5555
Property [ protected string $file = '' ]
5656
Property [ protected int $line = 0 ]
57-
Property [ private array $trace = Array ]
57+
Property [ private array $trace = [] ]
5858
Property [ private ?Throwable $previous = NULL ]
5959
}
6060

0 commit comments

Comments
 (0)