-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Pipe operator, New AST node version #7214
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
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7e0a65e
Implement pipe operator.
Crell a8e2a39
Try using an AST node.
Crell f6d81c2
Have the AST compiler just map to another AST.
Crell 1b34793
Add test for pipe evaluation order.
Crell 4a78526
Playing around with alternate approaches.
Crell 2c56dd7
Fix compile step to enforce left to right evaluation.
Crell 552d31c
Remove comments, improve variable names.
Crell 0a901ad
Rebase fixes.
Crell 5515ddf
Flesh out by-reference tests for pipes.
Crell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Test that a pipe operator displays as a pipe operator when outputting syntax. | ||
--FILE-- | ||
<?php | ||
|
||
function _test(int $a): int { | ||
return $a + 1; | ||
} | ||
|
||
try { | ||
assert((5 |> '_test') == 99); | ||
} catch (AssertionError $e) { | ||
print $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
assert(5 |> '_test' == 99) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--TEST-- | ||
Pipe handles reference variables the same as normal functions. | ||
--FILE-- | ||
<?php | ||
|
||
function _modify(int &$a): string { | ||
$a += 1; | ||
return "foo"; | ||
} | ||
|
||
$a = 5; | ||
$res1 = $a |> '_modify'; | ||
|
||
var_dump($res1); | ||
var_dump($a); | ||
|
||
try { | ||
$res2 = 5 |> '_modify'; | ||
} catch (Error $e) { | ||
print $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
function &return_by_ref(string $s): string { | ||
$ret = $s . ' bar'; | ||
return $ret; | ||
} | ||
|
||
function receive_by_ref(string &$b): string { | ||
$b .= " baz"; | ||
print $b . PHP_EOL; | ||
return $b . ' beep'; | ||
} | ||
|
||
$a = 'foo'; | ||
$res2 = $a |> 'return_by_ref' |> 'receive_by_ref'; | ||
var_dump($res2); | ||
|
||
try { | ||
$not_defined |> '_modify'; | ||
} catch (Error $e) { | ||
print $e->getMessage() . PHP_EOL; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
string(3) "foo" | ||
int(6) | ||
_modify(): Argument #1 ($a) cannot be passed by reference | ||
foo bar baz | ||
string(16) "foo bar baz beep" | ||
_modify(): Argument #1 ($a) must be of type int, null given, called in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--TEST-- | ||
Pipe operator chains | ||
--FILE-- | ||
<?php | ||
|
||
function _test1(int $a): int { | ||
return $a + 1; | ||
} | ||
|
||
function _test2(int $a): int { | ||
return $a * 2; | ||
} | ||
|
||
$res1 = 5 |> '_test1' |> '_test2'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(12) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--TEST-- | ||
Function evaluation order | ||
--FILE-- | ||
<?php | ||
|
||
function _test1(int $a): int { | ||
print __FUNCTION__ . PHP_EOL; | ||
return $a; | ||
} | ||
|
||
function _test2(): callable { | ||
print __FUNCTION__ . PHP_EOL; | ||
return '\_test3'; | ||
} | ||
|
||
function _test3(int $a): int { | ||
print __FUNCTION__ . PHP_EOL; | ||
return $a; | ||
} | ||
|
||
function _test4(int $a): int { | ||
print __FUNCTION__ . PHP_EOL; | ||
return $a; | ||
} | ||
|
||
$res1 = 5 |> '_test1' |> _test2() |> '_test4'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
_test1 | ||
_test2 | ||
_test3 | ||
_test4 | ||
int(5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
Pipe operator throws normally on missing function | ||
--FILE-- | ||
<?php | ||
|
||
try { | ||
$res1 = 5 |> '_test'; | ||
} | ||
catch (Throwable $e) { | ||
printf("Expected %s thrown, got %s", Error::class, get_class($e)); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Expected Error thrown, got Error |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--TEST-- | ||
Pipe operator handles all callable styles | ||
--FILE-- | ||
<?php | ||
|
||
function _add(int $x, int $y): int { | ||
return $x + $y; | ||
} | ||
|
||
function _area(int $x, int $y): int { | ||
return $x * $y; | ||
} | ||
|
||
class _Test | ||
{ | ||
public function message(string $which): string | ||
{ | ||
if ($which == 1) { | ||
return "Hello"; | ||
} | ||
else if ($which == 2) { | ||
return "Goodbye"; | ||
} | ||
else { | ||
return "World"; | ||
} | ||
} | ||
} | ||
|
||
$test = new _Test(); | ||
|
||
$add3 = fn($x) => _add($x, 3); | ||
|
||
$res1 = 2 | ||
|> [$test, 'message'] | ||
|> 'strlen' | ||
|> $add3 | ||
|> fn($x) => _area($x, 2) | ||
; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(20) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
Pipe operator accepts optional-parameter functions | ||
--FILE-- | ||
<?php | ||
|
||
function _test(int $a, int $b = 3) { | ||
return $a + $b; | ||
} | ||
|
||
$res1 = 5 |> '_test'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Pipe binds lower than addition | ||
--FILE-- | ||
<?php | ||
|
||
function _test1(int $a): int { | ||
return $a + 1; | ||
} | ||
|
||
$bad_func = null; | ||
|
||
$res1 = 5 + 2 |> '_test1'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(8) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
Pipe binds lower than coalesce | ||
--FILE-- | ||
<?php | ||
|
||
function _test1(int $a): int { | ||
return $a * 2; | ||
} | ||
|
||
$bad_func = null; | ||
|
||
$res1 = 5 |> $bad_func ?? '_test1'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Pipe binds lower than ternary | ||
--FILE-- | ||
<?php | ||
|
||
function _test1(int $a): int { | ||
return $a + 1; | ||
} | ||
|
||
function _test2(int $a): int { | ||
return $a * 2; | ||
} | ||
|
||
$bad_func = null; | ||
|
||
$res1 = 5 |> $bad_func ? '_test1' : '_test2'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(10) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--TEST-- | ||
Pipe operator supports built-in functions | ||
--FILE-- | ||
<?php | ||
|
||
$res1 = "Hello" |> 'strlen'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--TEST-- | ||
Pipe operator supports user-defined functions | ||
--FILE-- | ||
<?php | ||
|
||
function _test(int $a): int { | ||
return $a + 1; | ||
} | ||
|
||
$res1 = 5 |> '_test'; | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(6) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Pipe operator fails on multi-parameter functions | ||
--FILE-- | ||
<?php | ||
|
||
function _test(int $a, int $b) { | ||
return $a + $b; | ||
} | ||
|
||
|
||
try { | ||
$res1 = 5 |> '_test'; | ||
} | ||
catch (Throwable $e) { | ||
printf("Expected %s thrown, got %s", ArgumentCountError::class, get_class($e)); | ||
} | ||
|
||
|
||
?> | ||
--EXPECT-- | ||
Expected ArgumentCountError thrown, got ArgumentCountError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Pipe operator respects types | ||
--FILE-- | ||
<?php | ||
|
||
function _test(int $a, int $b) { | ||
return $a + $b; | ||
} | ||
|
||
try { | ||
$res1 = "Hello" |> '_test'; | ||
var_dump($res1); | ||
} | ||
catch (Throwable $e) { | ||
printf("Expected %s thrown, got %s", TypeError::class, get_class($e)); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Expected TypeError thrown, got TypeError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Pipe operator fails void return chaining in strict mode | ||
--FILE-- | ||
<?php | ||
declare(strict_types=1); | ||
|
||
function nonReturnFunction($bar): void {} | ||
|
||
try { | ||
$result = "Hello World" | ||
|> 'nonReturnFunction' | ||
|> 'strlen'; | ||
var_dump($result); | ||
} | ||
catch (Throwable $e) { | ||
printf("Expected %s thrown, got %s", TypeError::class, get_class($e)); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Expected TypeError thrown, got TypeError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Pipe operator chains saved as a closure | ||
--FILE-- | ||
<?php | ||
|
||
function _test1(int $a): int { | ||
return $a + 1; | ||
} | ||
|
||
function _test2(int $a): int { | ||
return $a * 2; | ||
} | ||
|
||
$func = fn($x) => $x |> '_test1' |> '_test2'; | ||
|
||
$res1 = $func(5); | ||
|
||
var_dump($res1); | ||
?> | ||
--EXPECT-- | ||
int(12) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.