match.test 5.1 KB

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