Skip to content

[RFC] Match expression #5371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Zend/tests/match/001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Basic match expression functionality test
--FILE--
<?php

function wordify($x) {
return match ($x) {
0 => 'Zero',
1 => 'One',
2 => 'Two',
3 => 'Three',
4 => 'Four',
5 => 'Five',
6 => 'Six',
7 => 'Seven',
8 => 'Eight',
9 => 'Nine',
};
}

for ($i = 0; $i <= 9; $i++) {
print wordify($i) . "\n";
}

?>
--EXPECT--
Zero
One
Two
Three
Four
Five
Six
Seven
Eight
Nine
19 changes: 19 additions & 0 deletions Zend/tests/match/002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Match expression omit trailing comma
--FILE--
<?php

function print_bool($bool) {
echo match ($bool) {
true => "true\n",
false => "false\n"
};
}

print_bool(true);
print_bool(false);

?>
--EXPECT--
true
false
24 changes: 24 additions & 0 deletions Zend/tests/match/003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Match expression default case
--FILE--
<?php

function get_value($i) {
return match ($i) {
1 => 1,
2 => 2,
default => 'default',
};
}

echo get_value(0) . "\n";
echo get_value(1) . "\n";
echo get_value(2) . "\n";
echo get_value(3) . "\n";

?>
--EXPECT--
default
1
2
default
31 changes: 31 additions & 0 deletions Zend/tests/match/004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Match expression with true as expression
--FILE--
<?php

function get_range($i) {
return match (true) {
$i >= 50 => '50+',
$i >= 40 => '40-50',
$i >= 30 => '30-40',
$i >= 20 => '20-30',
$i >= 10 => '10-20',
default => '0-10',
};
}

echo get_range(22) . "\n";
echo get_range(0) . "\n";
echo get_range(59) . "\n";
echo get_range(13) . "\n";
echo get_range(39) . "\n";
echo get_range(40) . "\n";

?>
--EXPECT--
20-30
0-10
50+
10-20
30-40
40-50
12 changes: 12 additions & 0 deletions Zend/tests/match/005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Match expression discarding result
--FILE--
<?php

match (1) {
1 => print "Executed\n",
};

?>
--EXPECT--
Executed
13 changes: 13 additions & 0 deletions Zend/tests/match/006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--TEST--
Match expression with no cases
--FILE--
<?php

$x = match (true) {};

?>
--EXPECTF--
Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type bool in %s
Stack trace:
#0 {main}
thrown in %s on line %d
26 changes: 26 additions & 0 deletions Zend/tests/match/007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Match expression exception on unhandled case
--FILE--
<?php

function get_value($i) {
return match ($i) {
1 => 1,
2 => 2,
};
}

echo get_value(1) . "\n";
echo get_value(2) . "\n";
echo get_value(3) . "\n";

?>
--EXPECTF--
1
2

Fatal error: Uncaught UnhandledMatchError: Unhandled match value of type int in %s
Stack trace:
#0 %s: get_value(3)
#1 {main}
thrown in %s on line %d
25 changes: 25 additions & 0 deletions Zend/tests/match/008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Match expression multiple conditions per case
--FILE--
<?php

function is_working_day($day) {
return match ($day) {
1, 7 => false,
2, 3, 4, 5, 6 => true,
};
}

for ($i = 1; $i <= 7; $i++) {
var_dump(is_working_day($i));
}

?>
--EXPECT--
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
22 changes: 22 additions & 0 deletions Zend/tests/match/009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Pretty printing for match expression
--FILE--
<?php

assert((function () {
match ('foo') {
'foo', 'bar' => false,
'baz' => 'a',
default => 'b',
};
})());

?>
--EXPECTF--
Warning: assert(): assert(function () {
match ('foo') {
'foo', 'bar' => false,
'baz' => 'a',
default => 'b',
};
}()) failed in %s on line %d
19 changes: 19 additions & 0 deletions Zend/tests/match/011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Implicit break in match expression
--FILE--
<?php

function dump_and_return($string) {
var_dump($string);
return $string;
}

var_dump(match ('foo') {
'foo' => dump_and_return('foo'),
'bar' => dump_and_return('bar'),
});

?>
--EXPECT--
string(3) "foo"
string(3) "foo"
36 changes: 36 additions & 0 deletions Zend/tests/match/012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Strict comparison in match expression
--FILE--
<?php

function wrong() {
throw new Exception();
}

var_dump(match (0) {
null => wrong(),
false => wrong(),
0.0 => wrong(),
[] => wrong(),
'' => wrong(),
0 => 'int',
});

function get_value() {
return 0;
}

var_dump(match (get_value()) {
null => wrong(),
false => wrong(),
0.0 => wrong(),
[] => wrong(),
'' => wrong(),
0 => 'int',
default => 'default',
});

?>
--EXPECT--
string(3) "int"
string(3) "int"
92 changes: 92 additions & 0 deletions Zend/tests/match/017.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
--TEST--
Test strict comparison with match expression jump table
--FILE--
<?php

function wrong() {
throw new Exception();
}

function test_int($char) {
return match ($char) {
0 => wrong(),
1 => wrong(),
2 => wrong(),
3 => wrong(),
4 => wrong(),
5 => wrong(),
6 => wrong(),
7 => wrong(),
8 => wrong(),
9 => wrong(),
default => 'Not matched',
};
}

foreach (range(0, 9) as $int) {
var_dump((string) $int);
var_dump(test_int((string) $int));
}

function test_string($int) {
return match ($int) {
'0' => wrong(),
'1' => wrong(),
'2' => wrong(),
'3' => wrong(),
'4' => wrong(),
'5' => wrong(),
'6' => wrong(),
'7' => wrong(),
'8' => wrong(),
'9' => wrong(),
default => 'Not matched',
};
}

foreach (range(0, 9) as $int) {
var_dump($int);
var_dump(test_string($int));
}

--EXPECT--
string(1) "0"
string(11) "Not matched"
string(1) "1"
string(11) "Not matched"
string(1) "2"
string(11) "Not matched"
string(1) "3"
string(11) "Not matched"
string(1) "4"
string(11) "Not matched"
string(1) "5"
string(11) "Not matched"
string(1) "6"
string(11) "Not matched"
string(1) "7"
string(11) "Not matched"
string(1) "8"
string(11) "Not matched"
string(1) "9"
string(11) "Not matched"
int(0)
string(11) "Not matched"
int(1)
string(11) "Not matched"
int(2)
string(11) "Not matched"
int(3)
string(11) "Not matched"
int(4)
string(11) "Not matched"
int(5)
string(11) "Not matched"
int(6)
string(11) "Not matched"
int(7)
string(11) "Not matched"
int(8)
string(11) "Not matched"
int(9)
string(11) "Not matched"
20 changes: 20 additions & 0 deletions Zend/tests/match/023.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Test match strict comparison with true expression
--FILE--
<?php

function wrong() {
throw new Exception();
}

echo match (true) {
'truthy' => wrong(),
['truthy'] => wrong(),
new stdClass() => wrong(),
1 => wrong(),
1.0 => wrong(),
true => "true\n",
};

--EXPECT--
true
Loading