Skip to content

Commit 0f9c680

Browse files
TysonAndrenikic
authored andcommitted
Normalize representation of parenthesized concat in php 7.4
Fixes nikic#123
1 parent 1c71634 commit 0f9c680

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

ast.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,13 @@ static void ast_to_zval(zval *zv, zend_ast *ast, ast_state_info_t *state) {
752752
return;
753753
}
754754
break;
755+
#ifdef ZEND_PARENTHESIZED_CONCAT
756+
case ZEND_AST_BINARY_OP:
757+
if (ast->attr == ZEND_PARENTHESIZED_CONCAT) {
758+
ast->attr = ZEND_CONCAT;
759+
}
760+
break;
761+
#endif
755762
#else
756763
case ZEND_AST_CLASS_CONST:
757764
if (state->version >= 70) {

tests/concat.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
AST_CONCAT with/without parenthesis
3+
--FILE--
4+
<?php
5+
// PHP 7.4 changed the internal representation of concatenation operations.
6+
// This tests that php-ast consistently exposes the AST in PHP 7.0-7.4
7+
// For https://github.com/nikic/php-ast/issues/123
8+
require __DIR__ . '/../util.php';
9+
10+
$code = <<<'PHP'
11+
<?php
12+
require_once __DIR__ . '/first.php';
13+
require_once(__DIR__ . '/second.php');
14+
PHP;
15+
16+
echo ast_dump(ast\parse_code($code, $version=70)), "\n";
17+
18+
?>
19+
--EXPECTF--
20+
AST_STMT_LIST
21+
0: AST_INCLUDE_OR_EVAL
22+
flags: EXEC_REQUIRE_ONCE (%d)
23+
expr: AST_BINARY_OP
24+
flags: BINARY_CONCAT (%d)
25+
left: AST_MAGIC_CONST
26+
flags: MAGIC_DIR (%d)
27+
right: "/first.php"
28+
1: AST_INCLUDE_OR_EVAL
29+
flags: EXEC_REQUIRE_ONCE (%d)
30+
expr: AST_BINARY_OP
31+
flags: BINARY_CONCAT (%d)
32+
left: AST_MAGIC_CONST
33+
flags: MAGIC_DIR (%d)
34+
right: "/second.php"

0 commit comments

Comments
 (0)