Error Identifier: assignOp.invalid
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);
$a = 'hello';
$a -= 5;
Why is it reported? #
The compound assignment operator (such as +=, -=, *=, .=, etc.) cannot be applied to the given combination of types. The operation between the left-hand variable and the right-hand value produces a type error.
In the example above, the -= operator is applied between a string and an int, which is not a valid binary operation.
How to fix it #
Ensure the variable and the value are compatible types for the operation:
<?php declare(strict_types = 1);
-$a = 'hello';
-$a -= 5;
+$a = 10;
+$a -= 5;
Or use the correct operator for the types involved:
<?php declare(strict_types = 1);
$a = 'hello';
-$a -= 5;
+$a .= ' world';
How to ignore this error #
You can use the identifier assignOp.invalid to ignore this error using a comment:
// @phpstan-ignore assignOp.invalid
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: assignOp.invalid
Rules that report this error #
- PHPStan\Rules\Operators\InvalidBinaryOperationRule [1]