Error Identifier: pipe.byRef
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
function modify(string &$s): void
{
$s = strtoupper($s);
}
$result = 'hello' |> modify(...);
Why is it reported? #
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.
How to fix it #
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(...);
How to ignore this error #
You can use the identifier pipe.byRef to ignore this error using a comment:
// @phpstan-ignore pipe.byRef
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: pipe.byRef
Rules that report this error #
- PHPStan\Rules\Operators\PipeOperatorRule [1]