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);
class Replacer
{
/**
* @pure-unless-parameter-passed $flag
*/
public function replaceByValue(string $subject, bool $flag = false): string // ERROR: Method Replacer::replaceByValue() is marked @pure-unless-parameter-passed for parameter $flag, but $flag is not passed by reference.
{
return $subject;
}
}
Why is it reported? #
The @pure-unless-parameter-passed tag marks a method as pure unless an argument is passed for the named parameter. It is meant for optional by-reference out-parameters: writing into the caller’s variable through such a parameter is the side effect that makes the call impure.
A parameter passed by value cannot produce a side effect just by being passed — the method only receives a copy. The tag therefore has no meaning on a by-value parameter. This mirrors pureFunction.parameterPassedNotByRef for functions.
How to fix it #
If the method has no side effects, mark it as pure instead:
/**
- * @pure-unless-parameter-passed $flag
+ * @phpstan-pure
*/
public function replaceByValue(string $subject, bool $flag = false): string
If the parameter is meant to be an out-parameter, declare it by reference and describe the written type with @param-out:
/**
+ * @param-out int $count
- * @pure-unless-parameter-passed $flag
+ * @pure-unless-parameter-passed $count
*/
- public function replaceByValue(string $subject, bool $flag = false): string
+ public function replace(string $subject, int &$count = 0): string
{
+ $count = 1;
+
return $subject;
}
How to ignore this error #
You can use the identifier pureMethod.parameterPassedNotByRef to ignore this error using a comment:
// @phpstan-ignore pureMethod.parameterPassedNotByRef
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: pureMethod.parameterPassedNotByRef