From fb5cff1272d17264cd59683b7d3e96bd92a4e805 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 20 Oct 2021 15:12:40 +0200 Subject: [PATCH] Print array defaults in reflection As a followup to f34114b1fb68302ad51929890d3fb2d9cbba024b 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. --- ext/reflection/php_reflection.c | 28 ++++++++++++++++++++++++++-- ext/reflection/tests/bug60357.phpt | 23 ++++++++++++++++++----- sapi/cli/tests/005.phpt | 4 ++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 7cb7a44b51d..11024b9aee8 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -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), ""); diff --git a/ext/reflection/tests/bug60357.phpt b/ext/reflection/tests/bug60357.phpt index 731e25996f8..59051a94dae 100644 --- a/ext/reflection/tests/bug60357.phpt +++ b/ext/reflection/tests/bug60357.phpt @@ -2,9 +2,22 @@ Bug #60357 (__toString() method triggers E_NOTICE "Array to string conversion") --FILE-- __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 [ array $x = Array ] +--EXPECTF-- +Function [ function foo ] { + @@ %s + + - Parameters [4] { + Parameter #0 [ array $x = ['a', 'b'] ] + Parameter #1 [ array $y = ['x' => 'y'] ] + Parameter #2 [ array $z = [0 => 0, 2 => -2] ] + Parameter #3 [ array $a = [[], [1], [2, 3]] ] + } +} diff --git a/sapi/cli/tests/005.phpt b/sapi/cli/tests/005.phpt index 640bc32c20e..871b64a5ba5 100644 --- a/sapi/cli/tests/005.phpt +++ b/sapi/cli/tests/005.phpt @@ -37,7 +37,7 @@ string(183) "Class [ class stdClass ] { } " -string(2235) "Class [ class Exception implements Throwable, Stringable ] { +string(2232) "Class [ class Exception implements Throwable, Stringable ] { - Constants [0] { } @@ -54,7 +54,7 @@ string(2235) "Class [ 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 ] }