Skip to content

Fix ast export in array #4324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Zend/tests/assert/expect_020.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
AST pretty-peinter
--INI--
zend.assertions=1
assert.exception=0
--FILE--
<?php
assert(0 && ($a = function () {
$var = 'test';
$str = "$var, $var[1], {$var}[], {$var[1]}[], ${var}[], ${var[1]}[]";
}));
?>
--EXPECTF--
Warning: assert(): assert(0 && ($a = function () {
$var = 'test';
$str = "$var, {$var[1]}, {$var}[], {$var[1]}[], {$var}[], {$var[1]}[]";
})) failed in %sexpect_020.php on line %d
11 changes: 10 additions & 1 deletion Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,13 @@ static ZEND_COLD int zend_ast_valid_var_name(const char *s, size_t len)
return 1;
}

static ZEND_COLD int zend_ast_var_needs_braces(char ch)
{
if (ch != '[' && !zend_ast_valid_var_char(ch))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if is not necessary here:

Suggested change
if (ch != '[' && !zend_ast_valid_var_char(ch))
return ch != '[' && !zend_ast_valid_var_char(ch);

return 0;
return 1;
}

static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int priority, int indent)
{
if (ast->kind == ZEND_AST_ZVAL) {
Expand Down Expand Up @@ -1109,7 +1116,7 @@ static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, ze
ast->child[0]->kind == ZEND_AST_ZVAL &&
(i + 1 == list->children ||
list->child[i + 1]->kind != ZEND_AST_ZVAL ||
!zend_ast_valid_var_char(
!zend_ast_var_needs_braces(
*Z_STRVAL_P(
zend_ast_get_zval(list->child[i + 1]))))) {
zend_ast_export_ex(str, ast, 0, indent);
Expand Down Expand Up @@ -1759,6 +1766,8 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
zend_ast_export_ex(str, ast->child[1], 80, indent);
smart_str_appends(str, " => ");
}
if (ast->attr)
smart_str_appendc(str, '&');
zend_ast_export_ex(str, ast->child[0], 80, indent);
break;
case ZEND_AST_NEW:
Expand Down