Skip to content

Commit 9fa1d13

Browse files
committed
Implement match expression
RFC: https://wiki.php.net/rfc/match_expression_v2 Closes GH-5371.
1 parent c60d0dc commit 9fa1d13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2442
-619
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ PHP 8.0 UPGRADE NOTES
571571
. Added support for constructor property promotion (declaring properties in
572572
the constructor signature).
573573
RFC: https://wiki.php.net/rfc/constructor_promotion
574+
. Added support for `match` expression.
575+
RFC: https://wiki.php.net/rfc/match_expression_v2
574576

575577
- Date:
576578
. Added DateTime::createFromInterface() and

Zend/tests/match/001.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
Basic match expression functionality test
3+
--FILE--
4+
<?php
5+
6+
function wordify($x) {
7+
return match ($x) {
8+
0 => 'Zero',
9+
1 => 'One',
10+
2 => 'Two',
11+
3 => 'Three',
12+
4 => 'Four',
13+
5 => 'Five',
14+
6 => 'Six',
15+
7 => 'Seven',
16+
8 => 'Eight',
17+
9 => 'Nine',
18+
};
19+
}
20+
21+
for ($i = 0; $i <= 9; $i++) {
22+
print wordify($i) . "\n";
23+
}
24+
25+
?>
26+
--EXPECT--
27+
Zero
28+
One
29+
Two
30+
Three
31+
Four
32+
Five
33+
Six
34+
Seven
35+
Eight
36+
Nine

Zend/tests/match/002.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Match expression omit trailing comma
3+
--FILE--
4+
<?php
5+
6+
function print_bool($bool) {
7+
echo match ($bool) {
8+
true => "true\n",
9+
false => "false\n"
10+
};
11+
}
12+
13+
print_bool(true);
14+
print_bool(false);
15+
16+
?>
17+
--EXPECT--
18+
true
19+
false

Zend/tests/match/003.phpt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Match expression default case
3+
--FILE--
4+
<?php
5+
6+
function get_value($i) {
7+
return match ($i) {
8+
1 => 1,
9+
2 => 2,
10+
default => 'default',
11+
};
12+
}
13+
14+
echo get_value(0) . "\n";
15+
echo get_value(1) . "\n";
16+
echo get_value(2) . "\n";
17+
echo get_value(3) . "\n";
18+
19+
?>
20+
--EXPECT--
21+
default
22+
1
23+
2
24+
default

Zend/tests/match/004.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Match expression with true as expression
3+
--FILE--
4+
<?php
5+
6+
function get_range($i) {
7+
return match (true) {
8+
$i >= 50 => '50+',
9+
$i >= 40 => '40-50',
10+
$i >= 30 => '30-40',
11+
$i >= 20 => '20-30',
12+
$i >= 10 => '10-20',
13+
default => '0-10',
14+
};
15+
}
16+
17+
echo get_range(22) . "\n";
18+
echo get_range(0) . "\n";
19+
echo get_range(59) . "\n";
20+
echo get_range(13) . "\n";
21+
echo get_range(39) . "\n";
22+
echo get_range(40) . "\n";
23+
24+
?>
25+
--EXPECT--
26+
20-30
27+
0-10
28+
50+
29+
10-20
30+
30-40
31+
40-50

Zend/tests/match/005.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Match expression discarding result
3+
--FILE--
4+
<?php
5+
6+
match (1) {
7+
1 => print "Executed\n",
8+
};
9+
10+
?>
11+
--EXPECT--
12+
Executed

Zend/tests/match/006.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Match expression with no cases
3+
--FILE--
4+
<?php
5+
6+
$x = match (true) {};
7+
8+
?>
9+
--EXPECTF--
10+
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type bool in %s
11+
Stack trace:
12+
#0 {main}
13+
thrown in %s on line %d

Zend/tests/match/007.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Match expression exception on unhandled case
3+
--FILE--
4+
<?php
5+
6+
function get_value($i) {
7+
return match ($i) {
8+
1 => 1,
9+
2 => 2,
10+
};
11+
}
12+
13+
echo get_value(1) . "\n";
14+
echo get_value(2) . "\n";
15+
echo get_value(3) . "\n";
16+
17+
?>
18+
--EXPECTF--
19+
1
20+
2
21+
22+
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in %s
23+
Stack trace:
24+
#0 %s: get_value(3)
25+
#1 {main}
26+
thrown in %s on line %d

Zend/tests/match/008.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Match expression multiple conditions per case
3+
--FILE--
4+
<?php
5+
6+
function is_working_day($day) {
7+
return match ($day) {
8+
1, 7 => false,
9+
2, 3, 4, 5, 6 => true,
10+
};
11+
}
12+
13+
for ($i = 1; $i <= 7; $i++) {
14+
var_dump(is_working_day($i));
15+
}
16+
17+
?>
18+
--EXPECT--
19+
bool(false)
20+
bool(true)
21+
bool(true)
22+
bool(true)
23+
bool(true)
24+
bool(true)
25+
bool(false)

Zend/tests/match/009.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Pretty printing for match expression
3+
--FILE--
4+
<?php
5+
6+
assert((function () {
7+
match ('foo') {
8+
'foo', 'bar' => false,
9+
'baz' => 'a',
10+
default => 'b',
11+
};
12+
})());
13+
14+
?>
15+
--EXPECTF--
16+
Warning: assert(): assert(function () {
17+
match ('foo') {
18+
'foo', 'bar' => false,
19+
'baz' => 'a',
20+
default => 'b',
21+
};
22+
}()) failed in %s on line %d

0 commit comments

Comments
 (0)