xref: /php-ast/tests/php80_union_types.phpt (revision 260678d4)
1--TEST--
2Union types in PHP 8.0
3--SKIPIF--
4<?php if (PHP_VERSION_ID < 80000) die('skip PHP >= 8.0 only'); ?>
5--FILE--
6<?php
7
8require __DIR__ . '/../util.php';
9
10$code = <<<'PHP'
11<?php
12namespace NS;
13function test(OBJECT|array|float|int|String|null $a) : string|false {
14    return json_encode($a);
15}
16class Xyz {
17    public bool|stdClass $x;
18}
19function testClasses(iterable|\stdClass|Xyz $s) : namespace\Xyz|false|null {
20    return new X();
21}
22test([]);
23testClasses([2,3]);
24PHP;
25
26$node = ast\parse_code($code, $version=70);
27echo ast_dump($node), "\n";
28--EXPECTF--
29AST_STMT_LIST
30    0: AST_NAMESPACE
31        name: "NS"
32        stmts: null
33    1: AST_FUNC_DECL
34        name: "test"
35        docComment: null
36        params: AST_PARAM_LIST
37            0: AST_PARAM
38                type: AST_TYPE_UNION
39                    0: AST_TYPE
40                        flags: TYPE_OBJECT (%d)
41                    1: AST_TYPE
42                        flags: TYPE_ARRAY (%d)
43                    2: AST_TYPE
44                        flags: TYPE_DOUBLE (%d)
45                    3: AST_TYPE
46                        flags: TYPE_LONG (%d)
47                    4: AST_TYPE
48                        flags: TYPE_STRING (%d)
49                    5: AST_TYPE
50                        flags: TYPE_NULL (%d)
51                name: "a"
52                default: null
53        stmts: AST_STMT_LIST
54            0: AST_RETURN
55                expr: AST_CALL
56                    expr: AST_NAME
57                        flags: NAME_NOT_FQ (%d)
58                        name: "json_encode"
59                    args: AST_ARG_LIST
60                        0: AST_VAR
61                            name: "a"
62        returnType: AST_TYPE_UNION
63            0: AST_TYPE
64                flags: TYPE_STRING (%d)
65            1: AST_TYPE
66                flags: TYPE_FALSE (%d)
67        __declId: 0
68    2: AST_CLASS
69        name: "Xyz"
70        docComment: null
71        extends: null
72        implements: null
73        stmts: AST_STMT_LIST
74            0: AST_PROP_GROUP
75                flags: MODIFIER_PUBLIC (%d)
76                type: AST_TYPE_UNION
77                    0: AST_TYPE
78                        flags: TYPE_BOOL (%d)
79                    1: AST_NAME
80                        flags: NAME_NOT_FQ (%d)
81                        name: "stdClass"
82                props: AST_PROP_DECL
83                    0: AST_PROP_ELEM
84                        name: "x"
85                        default: null
86                        docComment: null
87        __declId: 1
88    3: AST_FUNC_DECL
89        name: "testClasses"
90        docComment: null
91        params: AST_PARAM_LIST
92            0: AST_PARAM
93                type: AST_TYPE_UNION
94                    0: AST_TYPE
95                        flags: TYPE_ITERABLE (%d)
96                    1: AST_NAME
97                        flags: NAME_FQ (%d)
98                        name: "stdClass"
99                    2: AST_NAME
100                        flags: NAME_NOT_FQ (%d)
101                        name: "Xyz"
102                name: "s"
103                default: null
104        stmts: AST_STMT_LIST
105            0: AST_RETURN
106                expr: AST_NEW
107                    class: AST_NAME
108                        flags: NAME_NOT_FQ (%d)
109                        name: "X"
110                    args: AST_ARG_LIST
111        returnType: AST_TYPE_UNION
112            0: AST_NAME
113                flags: NAME_RELATIVE (%d)
114                name: "Xyz"
115            1: AST_TYPE
116                flags: TYPE_FALSE (%d)
117            2: AST_TYPE
118                flags: TYPE_NULL (%d)
119        __declId: 2
120    4: AST_CALL
121        expr: AST_NAME
122            flags: NAME_NOT_FQ (%d)
123            name: "test"
124        args: AST_ARG_LIST
125            0: AST_ARRAY
126                flags: ARRAY_SYNTAX_SHORT (%d)
127    5: AST_CALL
128        expr: AST_NAME
129            flags: NAME_NOT_FQ (%d)
130            name: "testClasses"
131        args: AST_ARG_LIST
132            0: AST_ARRAY
133                flags: ARRAY_SYNTAX_SHORT (%d)
134                0: AST_ARRAY_ELEM
135                    flags: 0
136                    value: 2
137                    key: null
138                1: AST_ARRAY_ELEM
139                    flags: 0
140                    value: 3
141                    key: null