|
| 1 | +<?php |
| 2 | + |
| 3 | +function simpleLongArray($x) { |
| 4 | + return array( |
| 5 | + /* testLongArrayArrowSimple */ |
| 6 | + 0 => 'Zero', |
| 7 | + ); |
| 8 | +} |
| 9 | + |
| 10 | +function simpleShortArray($x) { |
| 11 | + return [ |
| 12 | + /* testShortArrayArrowSimple */ |
| 13 | + 0 => 'Zero', |
| 14 | + ]; |
| 15 | +} |
| 16 | + |
| 17 | +function simpleLongList($x) { |
| 18 | + list( |
| 19 | + /* testLongListArrowSimple */ |
| 20 | + 0 => $a, |
| 21 | + ) = $x; |
| 22 | +} |
| 23 | + |
| 24 | +function simpleShortList($x) { |
| 25 | + [ |
| 26 | + /* testShortListArrowSimple */ |
| 27 | + 0 => $a, |
| 28 | + ] = $x; |
| 29 | +} |
| 30 | + |
| 31 | +function simpleYield($x) { |
| 32 | + $i = 0; |
| 33 | + foreach (explode("\n", $x) as $line) { |
| 34 | + /* testYieldArrowSimple */ |
| 35 | + yield ++$i => $line; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function simpleForeach($x) { |
| 40 | + /* testForeachArrowSimple */ |
| 41 | + foreach ($x as $k => $value) {} |
| 42 | +} |
| 43 | + |
| 44 | +function simpleMatch($x) { |
| 45 | + return match ($x) { |
| 46 | + /* testMatchArrowSimpleSingleCase */ |
| 47 | + 0 => 'Zero', |
| 48 | + /* testMatchArrowSimpleMultiCase */ |
| 49 | + 2, 4, 6 => 'Zero', |
| 50 | + /* testMatchArrowSimpleSingleCaseWithTrailingComma */ |
| 51 | + 1, => 'Zero', |
| 52 | + /* testMatchArrowSimpleMultiCaseWithTrailingComma */ |
| 53 | + 3, 5, => 'Zero', |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function simpleArrowFunction($y) { |
| 58 | + /* testFnArrowSimple */ |
| 59 | + return fn ($y) => callMe($y); |
| 60 | +} |
| 61 | + |
| 62 | +function matchNestedInMatch() { |
| 63 | + $x = match ($y) { |
| 64 | + /* testMatchArrowNestedMatchOuter */ |
| 65 | + default, => match ($z) { |
| 66 | + /* testMatchArrowNestedMatchInner */ |
| 67 | + 1 => 1 |
| 68 | + }, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +function matchNestedInLongArrayValue() { |
| 73 | + $array = array( |
| 74 | + /* testLongArrayArrowWithNestedMatchValue1 */ |
| 75 | + 'a' => match ($test) { |
| 76 | + /* testMatchArrowInLongArrayValue1 */ |
| 77 | + 1 => 'a', |
| 78 | + /* testMatchArrowInLongArrayValue2 */ |
| 79 | + 2 => 'b' |
| 80 | + }, |
| 81 | + /* testLongArrayArrowWithNestedMatchValue2 */ |
| 82 | + $i => match ($test) { |
| 83 | + /* testMatchArrowInLongArrayValue3 */ |
| 84 | + 1 => 'a', |
| 85 | + }, |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +function matchNestedInShortArrayValue() { |
| 90 | + $array = [ |
| 91 | + /* testShortArrayArrowWithNestedMatchValue1 */ |
| 92 | + 'a' => match ($test) { |
| 93 | + /* testMatchArrowInShortArrayValue1 */ |
| 94 | + 1 => 'a', |
| 95 | + /* testMatchArrowInShortArrayValue2 */ |
| 96 | + 2 => 'b' |
| 97 | + }, |
| 98 | + /* testShortArrayArrowWithNestedMatchValue2 */ |
| 99 | + $i => match ($test) { |
| 100 | + /* testMatchArrowInShortArrayValue3 */ |
| 101 | + 1 => 'a', |
| 102 | + }, |
| 103 | + ]; |
| 104 | +} |
| 105 | + |
| 106 | +function matchNestedInLongArrayKey() { |
| 107 | + $array = array( |
| 108 | + match ($test) { /* testMatchArrowInLongArrayKey1 */ 1 => 'a', /* testMatchArrowInLongArrayKey2 */ 2 => 'b' } |
| 109 | + /* testLongArrayArrowWithMatchKey */ |
| 110 | + => 'dynamic keys, woho!', |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +function matchNestedInShortArrayKey() { |
| 115 | + $array = [ |
| 116 | + match ($test) { /* testMatchArrowInShortArrayKey1 */ 1 => 'a', /* testMatchArrowInShortArrayKey2 */ 2 => 'b' } |
| 117 | + /* testShortArrayArrowWithMatchKey */ |
| 118 | + => 'dynamic keys, woho!', |
| 119 | + ]; |
| 120 | +} |
| 121 | + |
| 122 | +function arraysNestedInMatch() { |
| 123 | + $matcher = match ($x) { |
| 124 | + /* testMatchArrowWithLongArrayBodyWithKeys */ |
| 125 | + 0 => array( |
| 126 | + /* testLongArrayArrowInMatchBody1 */ |
| 127 | + 0 => 1, |
| 128 | + /* testLongArrayArrowInMatchBody2 */ |
| 129 | + 'a' => 2, |
| 130 | + /* testLongArrayArrowInMatchBody3 */ |
| 131 | + 'b' => 3 |
| 132 | + ), |
| 133 | + /* testMatchArrowWithShortArrayBodyWithoutKeys */ |
| 134 | + 1 => [1, 2, 3], |
| 135 | + /* testMatchArrowWithLongArrayBodyWithoutKeys */ |
| 136 | + 2 => array( 1, [1, 2, 3], 2, 3), |
| 137 | + /* testMatchArrowWithShortArrayBodyWithKeys */ |
| 138 | + 3 => [ |
| 139 | + /* testShortArrayArrowInMatchBody1 */ |
| 140 | + 0 => 1, |
| 141 | + /* testShortArrayArrowInMatchBody2 */ |
| 142 | + 'a' => array(1, 2, 3), |
| 143 | + /* testShortArrayArrowInMatchBody3 */ |
| 144 | + 'b' => 2, |
| 145 | + 3 |
| 146 | + ], |
| 147 | + /* testShortArrayArrowinMatchCase1 */ |
| 148 | + [4 => 'a', /* testShortArrayArrowinMatchCase2 */ 5 => 6] |
| 149 | + /* testMatchArrowWithShortArrayWithKeysAsCase */ |
| 150 | + => 'match with array as case value', |
| 151 | + /* testShortArrayArrowinMatchCase3 */ |
| 152 | + [4 => 'a'], /* testLongArrayArrowinMatchCase4 */ array(5 => 6), |
| 153 | + /* testMatchArrowWithMultipleArraysWithKeysAsCase */ |
| 154 | + => 'match with multiple arrays as case value', |
| 155 | + }; |
| 156 | +} |
| 157 | + |
| 158 | +function matchNestedInArrowFunction($x) { |
| 159 | + /* testFnArrowWithMatchInValue */ |
| 160 | + $fn = fn($x) => match(true) { |
| 161 | + /* testMatchArrowInFnBody1 */ |
| 162 | + 1, 2, 3, 4, 5 => 'foo', |
| 163 | + /* testMatchArrowInFnBody2 */ |
| 164 | + default => 'bar', |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +function arrowFunctionsNestedInMatch($x) { |
| 169 | + return match ($x) { |
| 170 | + /* testMatchArrowWithFnBody1 */ |
| 171 | + 1 => /* testFnArrowInMatchBody1 */ fn($y) => callMe($y), |
| 172 | + /* testMatchArrowWithFnBody2 */ |
| 173 | + default => /* testFnArrowInMatchBody2 */ fn($y) => callThem($y) |
| 174 | + }; |
| 175 | +} |
| 176 | + |
| 177 | +function matchShortArrayMismash() { |
| 178 | + $array = [ |
| 179 | + match ($test) { |
| 180 | + /* testMatchArrowInComplexShortArrayKey1 */ |
| 181 | + 1 => [ /* testShortArrayArrowInComplexMatchValueinShortArrayKey */ 1 => 'a'], |
| 182 | + /* testMatchArrowInComplexShortArrayKey2 */ |
| 183 | + 2 => 'b' |
| 184 | + /* testShortArrayArrowInComplexMatchArrayMismash */ |
| 185 | + } => match ($test) { |
| 186 | + /* testMatchArrowInComplexShortArrayValue1 */ |
| 187 | + 1 => [ /* testShortArrayArrowInComplexMatchValueinShortArrayValue */ 1 => 'a'], |
| 188 | + /* testMatchArrowInComplexShortArrayValue1 */ |
| 189 | + 2 => /* testFnArrowInComplexMatchValueInShortArrayValue */ fn($y) => callMe($y) |
| 190 | + }, |
| 191 | + ]; |
| 192 | +} |
| 193 | + |
| 194 | + |
| 195 | +function longListInMatch($x, $y) { |
| 196 | + return match($x) { |
| 197 | + /* testMatchArrowWithLongListBody */ |
| 198 | + 1 => list('a' => $a, /* testLongListArrowInMatchBody */ 'b' => $b, 'c' => list('d' => $c)) = $y, |
| 199 | + /* testLongListArrowInMatchCase */ |
| 200 | + list('a' => $a, 'b' => $b) = $y /* testMatchArrowWithLongListInCase */ => 'something' |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +function shortListInMatch($x, $y) { |
| 205 | + return match($x) { |
| 206 | + /* testMatchArrowWithShortListBody */ |
| 207 | + 1 => ['a' => $a, 'b' => $b, 'c' => /* testShortListArrowInMatchBody */ ['d' => $c]] = $y, |
| 208 | + /* testShortListArrowInMatchCase */ |
| 209 | + ['a' => $a, 'b' => $b] = $y /* testMatchArrowWithShortListInCase */ => 'something' |
| 210 | + }; |
| 211 | +} |
| 212 | + |
| 213 | +function matchInLongList() { |
| 214 | + /* testMatchArrowInLongListKey */ |
| 215 | + list(match($x) {1 => 1, 2 => 2} /* testLongListArrowWithMatchInKey */ => $a) = $array; |
| 216 | +} |
| 217 | + |
| 218 | +function matchInShortList() { |
| 219 | + /* testMatchArrowInShortListKey */ |
| 220 | + [match($x) {1 => 1, 2 => 2} /* testShortListArrowWithMatchInKey */ => $a] = $array; |
| 221 | +} |
0 commit comments