Skip to content

Commit d7716ad

Browse files
committed
Implement pipe operator.
1 parent 4b1c3cf commit d7716ad

21 files changed

+321
-4
lines changed

Zend/tests/pipe_operator/ast.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that a pipe operator displays as a pipe operator when outputting syntax.
3+
--FILE--
4+
<?php
5+
6+
function _test(int $a): int {
7+
return $a + 1;
8+
}
9+
10+
try {
11+
assert((5 |> '_test') == 99);
12+
} catch (AssertionError $e) {
13+
print $e->getMessage();
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
assert(5 |> \_test == 99)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Pipe operator accepts by-reference functions
3+
--FILE--
4+
<?php
5+
6+
function _modify(int &$a): string {
7+
$a += 1;
8+
return "foo";
9+
}
10+
11+
//try {
12+
$a = 5;
13+
$res1 = $a |> '_modify';
14+
15+
var_dump($res1);
16+
var_dump($a);
17+
?>
18+
--EXPECT--
19+
string(3) "foo"
20+
int(6)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Pipe operator chains
3+
--FILE--
4+
<?php
5+
6+
function _test1(int $a): int {
7+
return $a + 1;
8+
}
9+
10+
function _test2(int $a): int {
11+
return $a * 2;
12+
}
13+
14+
$res1 = 5 |> '_test1' |> '_test2';
15+
16+
var_dump($res1);
17+
?>
18+
--EXPECT--
19+
int(12)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Pipe operator throws normally on missing function
3+
--FILE--
4+
<?php
5+
6+
try {
7+
$res1 = 5 |> '_test';
8+
}
9+
catch (Throwable $e) {
10+
printf("Expected %s thrown, got %s", Error::class, get_class($e));
11+
}
12+
13+
?>
14+
--EXPECT--
15+
Expected Error thrown, got Error
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
Pipe operator handles all callable styles
3+
--FILE--
4+
<?php
5+
6+
function _add(int $x, int $y): int {
7+
return $x + $y;
8+
}
9+
10+
function _area(int $x, int $y): int {
11+
return $x * $y;
12+
}
13+
14+
class _Test
15+
{
16+
public function message(string $which): string
17+
{
18+
if ($which == 1) {
19+
return "Hello";
20+
}
21+
else if ($which == 2) {
22+
return "Goodbye";
23+
}
24+
else {
25+
return "World";
26+
}
27+
}
28+
}
29+
30+
$test = new _Test();
31+
32+
$add3 = fn($x) => _add($x, 3);
33+
34+
$res1 = 2
35+
|> [$test, 'message']
36+
|> 'strlen'
37+
|> $add3
38+
|> fn($x) => _area($x, 2)
39+
;
40+
41+
var_dump($res1);
42+
?>
43+
--EXPECT--
44+
int(20)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Pipe operator accepts optional-parameter functions
3+
--FILE--
4+
<?php
5+
6+
function _test(int $a, int $b = 3) {
7+
return $a + $b;
8+
}
9+
10+
$res1 = 5 |> '_test';
11+
12+
var_dump($res1);
13+
?>
14+
--EXPECT--
15+
int(8)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Pipe binds lower than addition
3+
--FILE--
4+
<?php
5+
6+
function _test1(int $a): int {
7+
return $a + 1;
8+
}
9+
10+
$bad_func = null;
11+
12+
$res1 = 5 + 2 |> '_test1';
13+
14+
var_dump($res1);
15+
?>
16+
--EXPECT--
17+
int(8)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Pipe binds lower than coalesce
3+
--FILE--
4+
<?php
5+
6+
function _test1(int $a): int {
7+
return $a * 2;
8+
}
9+
10+
$bad_func = null;
11+
12+
$res1 = 5 |> $bad_func ?? '_test1';
13+
14+
var_dump($res1);
15+
?>
16+
--EXPECT--
17+
int(10)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Pipe binds lower than ternary
3+
--FILE--
4+
<?php
5+
6+
function _test1(int $a): int {
7+
return $a + 1;
8+
}
9+
10+
function _test2(int $a): int {
11+
return $a * 2;
12+
}
13+
14+
$bad_func = null;
15+
16+
$res1 = 5 |> $bad_func ? '_test1' : '_test2';
17+
18+
var_dump($res1);
19+
?>
20+
--EXPECT--
21+
int(10)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Pipe operator supports built-in functions
3+
--FILE--
4+
<?php
5+
6+
$res1 = "Hello" |> 'strlen';
7+
8+
var_dump($res1);
9+
?>
10+
--EXPECT--
11+
int(5)

0 commit comments

Comments
 (0)