Skip to content

Commit 782d13d

Browse files
committed
zend_test: support grouped use statements in compile_to_ast
While writing a test for it, I discovered the logic for printing the AST representation of use statements was broken. That has been fixed and incorporated into the test.
1 parent a1975b1 commit 782d13d

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎Zend/zend_ast.c‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,9 +2046,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
20462046
break;
20472047
case ZEND_AST_USE:
20482048
smart_str_appends(str, "use ");
2049-
if (ast->attr == T_FUNCTION) {
2049+
if (ast->attr == ZEND_SYMBOL_FUNCTION) {
20502050
smart_str_appends(str, "function ");
2051-
} else if (ast->attr == T_CONST) {
2051+
} else if (ast->attr == ZEND_SYMBOL_CONST) {
20522052
smart_str_appends(str, "const ");
20532053
}
20542054
goto simple_list;
@@ -2489,6 +2489,19 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
24892489
}
24902490
break;
24912491
case ZEND_AST_USE_ELEM:
2492+
if (ast->attr == ZEND_SYMBOL_FUNCTION) {
2493+
smart_str_appends(str, "function ");
2494+
} else if (ast->attr == ZEND_SYMBOL_CONST) {
2495+
smart_str_appends(str, "const ");
2496+
}
2497+
2498+
zend_ast_export_name(str, ast->child[0], 0, indent);
2499+
2500+
if (ast->child[1]) {
2501+
smart_str_appends(str, " as ");
2502+
zend_ast_export_name(str, ast->child[1], 0, indent);
2503+
}
2504+
break;
24922505
case ZEND_AST_TRAIT_ALIAS:
24932506
zend_ast_export_name(str, ast->child[0], 0, indent);
24942507
if (ast->attr & ZEND_ACC_PUBLIC) {
@@ -2510,6 +2523,18 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
25102523
smart_str_appends(str, ": ");
25112524
ast = ast->child[1];
25122525
goto tail_call;
2526+
case ZEND_AST_GROUP_USE:
2527+
smart_str_appends(str, "use ");
2528+
if (ast->attr == ZEND_SYMBOL_FUNCTION) {
2529+
smart_str_appends(str, "function ");
2530+
} else if (ast->attr == ZEND_SYMBOL_CONST) {
2531+
smart_str_appends(str, "const ");
2532+
}
2533+
smart_str_append(str, zend_ast_get_str(ast->child[0]));
2534+
smart_str_appends(str, "\\{");
2535+
zend_ast_export_list(str, zend_ast_get_list(ast->child[1]), true, 20, indent);
2536+
smart_str_appends(str, "}");
2537+
break;
25132538

25142539
/* 3 child nodes */
25152540
case ZEND_AST_METHOD_CALL:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
AST can be recreated (namespace)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
namespace Foo {
9+
class FooTest {
10+
}
11+
12+
function fn_foo() {
13+
}
14+
15+
function fn_bar() {
16+
}
17+
}
18+
19+
namespace Bar {
20+
class BarTest {
21+
}
22+
23+
function fn_bar() {
24+
}
25+
}
26+
27+
namespace {
28+
use function Bar\fn_bar;
29+
use Foo\{function fn_foo, FooTest, function fn_bar as fn_foobar};
30+
31+
new FooTest();
32+
fn_bar();
33+
fn_foobar();
34+
35+
echo zend_test_compile_to_ast(file_get_contents( __FILE__ ));
36+
}
37+
38+
?>
39+
--EXPECT--
40+
namespace Foo {
41+
class FooTest {
42+
}
43+
44+
function fn_foo() {
45+
}
46+
47+
function fn_bar() {
48+
}
49+
50+
}
51+
52+
namespace Bar {
53+
class BarTest {
54+
}
55+
56+
function fn_bar() {
57+
}
58+
59+
}
60+
61+
namespace {
62+
use function Bar\fn_bar;
63+
use Foo\{function fn_foo, FooTest, function fn_bar as fn_foobar};
64+
new FooTest();
65+
fn_bar();
66+
fn_foobar();
67+
echo zend_test_compile_to_ast(file_get_contents(__FILE__));
68+
}

0 commit comments

Comments
 (0)