Skip to content

Initial support for PHP 8.5 Pipe Operator #1164

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/lexer/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ module.exports = {
} else if (nchar === "|") {
this.input();
return this.tok.T_BOOLEAN_OR;
} else if (nchar === ">") {
this.input();
return this.tok.T_PIPE;
}
return "|";
},
Expand Down
8 changes: 8 additions & 0 deletions src/parser/expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ module.exports = {
if (this.token === this.tok.T_COALESCE) {
return result("bin", "??", expr, this.next().read_expr());
}
// extra operations :
// $a = "Hi" |> strtoupper(...);
if (this.token === this.tok.T_PIPE) {
Comment on lines +122 to +124
Copy link
Preview

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment placement is inconsistent with the existing code structure. This comment should be moved to line 124 to directly precede the pipe operator implementation, matching the pattern used for other operators like the coalesce operator.

Suggested change
// extra operations :
// $a = "Hi" |> strtoupper(...);
if (this.token === this.tok.T_PIPE) {
if (this.token === this.tok.T_PIPE) {
// extra operations :
// $a = "Hi" |> strtoupper(...);

Copilot uses AI. Check for mistakes.

if (this.version < 805) {
this.raiseError("PHP 8.5+ is required to use pipe operator");
}
return result("bin", "|>", expr, this.next().read_expr());
}

// extra operations :
// $username = $_GET['user'] ? true : false;
Expand Down
1 change: 1 addition & 0 deletions src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const TokenNames = {
T_NAME_RELATIVE: 241,
T_NAME_QUALIFIED: 242,
T_NAME_FULLY_QUALIFIED: 243,
T_PIPE: 244,
};

/**
Expand Down
106 changes: 106 additions & 0 deletions test/snapshot/__snapshots__/pipe.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`Parse Pipeline Operator can parse basic pipeline operator task 1`] = `
Program {
"children": [
ExpressionStatement {
"expression": Assign {
"kind": "assign",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "c",
},
"operator": "=",
"right": Bin {
"kind": "bin",
"left": Variable {
"curly": false,
"kind": "variable",
"name": "a",
},
"right": Closure {
"arguments": [
Parameter {
"attrGroups": [],
"byref": false,
"flags": 0,
"kind": "parameter",
"name": Identifier {
"kind": "identifier",
"name": "s",
},
"nullable": false,
"readonly": false,
"type": null,
"value": null,
"variadic": false,
},
],
"attrGroups": [],
"body": Bin {
"kind": "bin",
"left": Call {
"arguments": [
String {
"isDoubleQuote": true,
"kind": "string",
"raw": "","",
"unicode": false,
"value": ",",
},
Variable {
"curly": false,
"kind": "variable",
"name": "s",
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "explode",
"resolution": "uqn",
},
},
"right": Bin {
"kind": "bin",
"left": Call {
"arguments": [
VariadicPlaceholder {
"kind": "variadicplaceholder",
},
],
"kind": "call",
"what": Name {
"kind": "name",
"name": "array_filter",
"resolution": "uqn",
},
},
"right": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'array_last'",
"unicode": false,
"value": "array_last",
},
"type": "|>",
},
"type": "|>",
},
"byref": false,
"isStatic": false,
"kind": "arrowfunc",
"nullable": false,
"type": null,
},
"type": "|>",
},
},
"kind": "expressionstatement",
},
],
"errors": [],
"kind": "program",
}
`;
18 changes: 18 additions & 0 deletions test/snapshot/pipe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const parser = require("../main");

describe("Parse Pipeline Operator", () => {
it("can parse basic pipeline operator task", () => {
const parser8_5 = parser.create({
parser: {
version: "8.5",
},
});
expect(
parser8_5.parseEval(`
$c = $a
|> fn ($s) => explode(",", $s)
|> array_filter(...) |> 'array_last' ;
`),
).toMatchSnapshot();
});
});
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ declare module "php-parser" {
T_NAME_RELATIVE = 241,
T_NAME_QUALIFIED = 242,
T_NAME_FULLY_QUALIFIED = 243,
T_PIPE = 244,
}
/**
* PHP AST Tokens
Expand Down