Skip to content
Merged
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
15 changes: 4 additions & 11 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
ArrayLiteral,
BinaryExpression,
Block,
CallableNewExpression,
Comment,
Expression,
InstanceExpression,
Expand Down Expand Up @@ -1150,12 +1149,14 @@ protected function emitCallable($result, $callable) {

protected function emitCallableNew($result, $callable) {
$t= $result->temp();
$result->out->write("fn(...{$t}) => ");
$result->out->write("(fn(...{$t}) => ");

// See https://externals.io/message/129329
$callable->type->arguments= [new UnpackExpression(new Variable(substr($t, 1)), $callable->line)];
$this->emitOne($result, $callable->type);
$callable->type->arguments= null;

$result->out->write(')');
}

protected function emitInvoke($result, $invoke) {
Expand Down Expand Up @@ -1214,15 +1215,7 @@ protected function emitNullsafeInstance($result, $instance) {
protected function emitPipe($result, $pipe) {
$this->emitOne($result, $pipe->expression);
$result->out->write('|>');

// `fn() => ...` on the right-hand side of pipe operator must be parenthesized
if ($pipe->target instanceof CallableNewExpression) {
$result->out->write('(');
$this->emitOne($result, $pipe->target);
$result->out->write(')');
} else {
$this->emitOne($result, $pipe->target);
}
$this->emitOne($result, $pipe->target);
}

protected function emitNullsafePipe($result, $pipe) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/php/lang/ast/unittest/emit/PipelinesTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,15 @@ public function run() {

Assert::equals("bool(false)\n", $r);
}

#[Test, Values([null, '2026-09-05'])]
public function nullable_pipe_to_callable_new($value) {
$r= $this->run('class %T {
public function run($value) {
return $value ?|> new \util\Date(...);
}
}', $value);

Assert::equals($value, $r ? $r->toString('Y-m-d') : null);
}
}
Loading