1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Print array defaults in reflection

As a followup to f34114b1fb 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.
This commit is contained in:
Nikita Popov
2021-10-20 15:12:40 +02:00
parent 11d97ae00c
commit fb5cff1272
3 changed files with 46 additions and 9 deletions

View File

@@ -611,9 +611,33 @@ static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) {
static int format_default_value(smart_str *str, zval *value) {
if (Z_TYPE_P(value) <= IS_STRING) {
smart_str_append_scalar(str, value, 15);
smart_str_append_scalar(str, value, SIZE_MAX);
} else if (Z_TYPE_P(value) == IS_ARRAY) {
smart_str_appends(str, "Array");
zend_string *str_key;
zend_long num_key;
zval *zv;
bool is_list = zend_array_is_list(Z_ARRVAL_P(value));
bool first = true;
smart_str_appendc(str, '[');
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(value), num_key, str_key, zv) {
if (!first) {
smart_str_appends(str, ", ");
}
first = false;
if (!is_list) {
if (str_key) {
smart_str_appendc(str, '\'');
smart_str_append_escaped(str, ZSTR_VAL(str_key), ZSTR_LEN(str_key));
smart_str_appendc(str, '\'');
} else {
smart_str_append_long(str, num_key);
}
smart_str_appends(str, " => ");
}
format_default_value(str, zv);
} ZEND_HASH_FOREACH_END();
smart_str_appendc(str, ']');
} else {
ZEND_ASSERT(Z_TYPE_P(value) == IS_CONSTANT_AST);
zend_string *ast_str = zend_ast_export("", Z_ASTVAL_P(value), "");

View File

@@ -2,9 +2,22 @@
Bug #60357 (__toString() method triggers E_NOTICE "Array to string conversion")
--FILE--
<?php
function foo( array $x = array( 'a', 'b' ) ) {}
$r = new ReflectionParameter( 'foo', 0 );
echo $r->__toString();
function foo(
array $x = array('a', 'b'),
array $y = ['x' => 'y'],
array $z = [0 => 0, 2 => -2],
array $a = [[], [1], [2, 3]],
) {}
echo new ReflectionFunction('foo'), "\n";
?>
--EXPECT--
Parameter #0 [ <optional> array $x = Array ]
--EXPECTF--
Function [ <user> function foo ] {
@@ %s
- Parameters [4] {
Parameter #0 [ <optional> array $x = ['a', 'b'] ]
Parameter #1 [ <optional> array $y = ['x' => 'y'] ]
Parameter #2 [ <optional> array $z = [0 => 0, 2 => -2] ]
Parameter #3 [ <optional> array $a = [[], [1], [2, 3]] ]
}
}

View File

@@ -37,7 +37,7 @@ string(183) "Class [ <internal:Core> class stdClass ] {
}
"
string(2235) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
string(2232) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
- Constants [0] {
}
@@ -54,7 +54,7 @@ string(2235) "Class [ <internal:Core> class Exception implements Throwable, Stri
Property [ protected $code = 0 ]
Property [ protected string $file = '' ]
Property [ protected int $line = 0 ]
Property [ private array $trace = Array ]
Property [ private array $trace = [] ]
Property [ private ?Throwable $previous = NULL ]
}