xref: /PHP-Parser/test/code/parser/expr/match.test (revision 4e27a17c)
1Match
2-----
3<?php
4
5echo match (1) {
6    0 => 'Foo',
7    1 => 'Bar',
8};
9-----
10array(
11    0: Stmt_Echo(
12        exprs: array(
13            0: Expr_Match(
14                cond: Scalar_Int(
15                    value: 1
16                )
17                arms: array(
18                    0: MatchArm(
19                        conds: array(
20                            0: Scalar_Int(
21                                value: 0
22                            )
23                        )
24                        body: Scalar_String(
25                            value: Foo
26                        )
27                    )
28                    1: MatchArm(
29                        conds: array(
30                            0: Scalar_Int(
31                                value: 1
32                            )
33                        )
34                        body: Scalar_String(
35                            value: Bar
36                        )
37                    )
38                )
39            )
40        )
41    )
42)
43-----
44<?php
45
46$value = match (1) {
47    // list of conditions
48    0, 1 => 'Foo',
49};
50-----
51array(
52    0: Stmt_Expression(
53        expr: Expr_Assign(
54            var: Expr_Variable(
55                name: value
56            )
57            expr: Expr_Match(
58                cond: Scalar_Int(
59                    value: 1
60                )
61                arms: array(
62                    0: MatchArm(
63                        conds: array(
64                            0: Scalar_Int(
65                                value: 0
66                            )
67                            1: Scalar_Int(
68                                value: 1
69                            )
70                        )
71                        body: Scalar_String(
72                            value: Foo
73                        )
74                        comments: array(
75                            0: // list of conditions
76                        )
77                    )
78                )
79            )
80        )
81    )
82)
83-----
84<?php
85
86$result = match ($operator) {
87    BinaryOperator::ADD => $lhs + $rhs,
88};
89-----
90array(
91    0: Stmt_Expression(
92        expr: Expr_Assign(
93            var: Expr_Variable(
94                name: result
95            )
96            expr: Expr_Match(
97                cond: Expr_Variable(
98                    name: operator
99                )
100                arms: array(
101                    0: MatchArm(
102                        conds: array(
103                            0: Expr_ClassConstFetch(
104                                class: Name(
105                                    name: BinaryOperator
106                                )
107                                name: Identifier(
108                                    name: ADD
109                                )
110                            )
111                        )
112                        body: Expr_BinaryOp_Plus(
113                            left: Expr_Variable(
114                                name: lhs
115                            )
116                            right: Expr_Variable(
117                                name: rhs
118                            )
119                        )
120                    )
121                )
122            )
123        )
124    )
125)
126-----
127<?php
128
129$value = match ($char) {
130    1 => '1',
131    default => 'default'
132};
133-----
134array(
135    0: Stmt_Expression(
136        expr: Expr_Assign(
137            var: Expr_Variable(
138                name: value
139            )
140            expr: Expr_Match(
141                cond: Expr_Variable(
142                    name: char
143                )
144                arms: array(
145                    0: MatchArm(
146                        conds: array(
147                            0: Scalar_Int(
148                                value: 1
149                            )
150                        )
151                        body: Scalar_String(
152                            value: 1
153                        )
154                    )
155                    1: MatchArm(
156                        conds: null
157                        body: Scalar_String(
158                            value: default
159                        )
160                    )
161                )
162            )
163        )
164    )
165)
166-----
167<?php
168
169$value = match (1) {
170    0, 1, => 'Foo',
171    default, => 'Bar',
172};
173-----
174array(
175    0: Stmt_Expression(
176        expr: Expr_Assign(
177            var: Expr_Variable(
178                name: value
179            )
180            expr: Expr_Match(
181                cond: Scalar_Int(
182                    value: 1
183                )
184                arms: array(
185                    0: MatchArm(
186                        conds: array(
187                            0: Scalar_Int(
188                                value: 0
189                            )
190                            1: Scalar_Int(
191                                value: 1
192                            )
193                        )
194                        body: Scalar_String(
195                            value: Foo
196                        )
197                    )
198                    1: MatchArm(
199                        conds: null
200                        body: Scalar_String(
201                            value: Bar
202                        )
203                    )
204                )
205            )
206        )
207    )
208)
209