-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Fix ast export in array #4324
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test case:
<?php
$var = 'test';
$str = "$var, $var[1], {$var}[], {$var[1]}[], ${var}[], ${var[1]}[]"
Expected result:
$var = 'test';
$str = "$var, {$var[1]}, {$var}[], {$var[1]}[], {$var}[], {$var[1]}[]"
Actual result:
$var = 'test';
$str = "$var, {$var[1]}, $var[], {$var[1]}[], $var[], {$var[1]}[]"
- If
ZEND_AST_VAR
's next list starts with[
,ZEND_AST_VAR
should be{ZEND_AST_VAR}
.$var[]
-->{$var}[]
- If not, it occurs
PHP Parse error: syntax error, unexpected ']', expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
@sunnyeo Can you please add these as actual tests? Zend/tests/assert/expect_015.phpt can be used for this. |
Zend/zend_ast.c
Outdated
!zend_ast_valid_var_char( | ||
*Z_STRVAL_P( | ||
zend_ast_get_zval(list->child[i + 1]))))) { | ||
zend_ast_get_zval(list->child[i + 1])))))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is becoming quite hard to read. Can you maybe extract a function zend_ast_var_needs_braces()
that performs these checks?
@@ -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)) |
There was a problem hiding this comment.
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:
if (ch != '[' && !zend_ast_valid_var_char(ch)) | |
return ch != '[' && !zend_ast_valid_var_char(ch); |
zend_ast_export function has a bug when export an array with reference.
It misses reference sign (&).
Test case:
Expected result:
Actual result: