website/errors/pipe.byRef.md
<?php declare(strict_types = 1);
function modify(string &$s): void
{
$s = strtoupper($s);
}
$result = 'hello' |> modify(...);
The pipe operator |> passes the left-hand side as the first argument to the callable on the right. This value is passed by value, not by reference. When the callable declares its first parameter as pass-by-reference (&), the pipe operator cannot fulfil that contract, so PHPStan reports an error.
Change the callable to accept its first parameter by value instead of by reference, and return the result:
<?php declare(strict_types = 1);
-function modify(string &$s): void
+function modify(string $s): string
{
- $s = strtoupper($s);
+ return strtoupper($s);
}
$result = 'hello' |> modify(...);