|
1 | 1 | You have been given a piece of code (look for `a-match-made-in-heaven.php` in your working directory) which is full of bugs.
|
2 |
| -The code has been implemented badly, using a `switch` statement. Your job is to fix the code, by using the newly introduced |
3 |
| -`match` expression in PHP 8. |
| 2 | +The code has been implemented badly, using a `switch` statement. Your job is to fix the code, by using the newly introduced `match` expression in PHP 8. |
4 | 3 |
|
5 | 4 | The piece of code is supposed to take a string representing a keyboard keypress and convert it to its equivalent ANSI decimal code.
|
6 | 5 |
|
7 | 6 | There are only four key presses supported at the minute (enter, up, down & escape). It should stay like that for now.
|
8 | 7 |
|
9 | 8 | Focus on converting the switch statement to a match expression.
|
10 | 9 |
|
11 |
| -The key presses will be provided as strings via command line arguments. Only one keypress will be passed on each program invocation |
12 |
| -but it will be randomly picked from the four supported key presses. |
| 10 | +The key presses will be provided as strings via command line arguments. Only one keypress will be passed on each program invocation but it will be randomly picked from the four supported key presses. |
13 | 11 |
|
14 | 12 | ### The advantages of match
|
15 | 13 |
|
16 | 14 | * Match uses strict equality, unlike switch which uses weak comparison and can lead to subtle bugs.
|
17 | 15 | * Each match arm does not fall through without a break statement, unlike switch.
|
18 |
| -* Match expressions must be exhaustive, if there is no default arm specified, and no arm matches the given value, an `UnhandledMatchError` is thrown |
| 16 | +* Match expressions must be exhaustive, if there is no default arm specified, and no arm matches the given value, an `UnhandledMatchError` is thrown. |
19 | 17 | * Match is an expression and thus returns a value, reducing unnecessary variables and reducing the risk of accessing undefined variables.
|
20 | 18 |
|
21 | 19 |
|
|
0 commit comments