Merge branch 'PHP-8.1'

* PHP-8.1:
  Print array defaults in reflection
This commit is contained in:
Nikita Popov 2021-10-20 15:17:08 +02:00
commit a1285978d4
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 ]
}