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 increment(int &$value): void
{
$value++;
}
function doFoo(): void
{
increment(rand());
}
Why is it reported? #
When a function parameter is declared with & (pass by reference), PHP requires the caller to pass a variable, array element, or property – something that can be written back to. Passing an expression such as a function call result, a literal, or a null is not valid because there is no storage location for PHP to write the modified value into. Similarly, a readonly or @readonly property cannot be passed by reference because it must not be modified after initialisation.
How to fix it #
Store the value in a variable before passing it by reference:
<?php declare(strict_types = 1);
function increment(int &$value): void
{
$value++;
}
function doFoo(): void
{
- increment(rand());
+ $number = rand();
+ increment($number);
}
Or, if the function does not actually need to modify the argument, remove the & from the parameter declaration:
<?php declare(strict_types = 1);
-function increment(int &$value): void
+function increment(int $value): int
{
- $value++;
+ return $value + 1;
}
How to ignore this error #
You can use the identifier argument.byRef to ignore this error using a comment:
// @phpstan-ignore argument.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: argument.byRef
Rules that report this error #
- PHPStan\Rules\Classes\ClassAttributesRule [1] [2] [3]
- PHPStan\Rules\Classes\ClassConstantAttributesRule [1] [2] [3]
- PHPStan\Rules\Classes\InstantiationRule [1] [2] [3]
- PHPStan\Rules\Constants\ConstantAttributesRule [1] [2] [3]
- PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1] [2] [3]
- PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1] [2] [3]
- PHPStan\Rules\Functions\CallCallablesRule [1] [2] [3]
- PHPStan\Rules\Functions\CallToFunctionParametersRule [1] [2] [3]
- PHPStan\Rules\Functions\CallUserFuncRule [1] [2] [3]
- PHPStan\Rules\Functions\ClosureAttributesRule [1] [2] [3]
- PHPStan\Rules\Functions\FunctionAttributesRule [1] [2] [3]
- PHPStan\Rules\Functions\ParamAttributesRule [1] [2] [3]
- PHPStan\Rules\Methods\CallMethodsRule [1] [2] [3]
- PHPStan\Rules\Methods\CallStaticMethodsRule [1] [2] [3]
- PHPStan\Rules\Methods\MethodAttributesRule [1] [2] [3]
- PHPStan\Rules\Properties\PropertyAttributesRule [1] [2] [3]
- PHPStan\Rules\Properties\PropertyHookAttributesRule [1] [2] [3]
- PHPStan\Rules\Traits\TraitAttributesRule [1] [2] [3]