Error Identifier: binaryOp.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';
$b = new \stdClass();
$result = $a - $b;
Why is it reported? #
A binary operator (such as +, -, *, /, ., etc.) is used with operands whose types are not compatible for that operation. The operation would result in a TypeError at runtime.
In the example above, the - operator is applied between a string and an stdClass object, which is not a valid arithmetic operation.
How to fix it #
Ensure both operands have types compatible with the operator:
<?php declare(strict_types = 1);
-$a = 'hello';
-$b = new \stdClass();
+$a = 10;
+$b = 5;
$result = $a - $b;
Or use the correct operator for the types involved:
<?php declare(strict_types = 1);
$a = 'hello';
-$b = new \stdClass();
+$b = ' world';
-$result = $a - $b;
+$result = $a . $b;
How to ignore this error #
You can use the identifier binaryOp.invalid to ignore this error using a comment:
// @phpstan-ignore binaryOp.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: binaryOp.invalid
Rules that report this error #
- PHPStan\Rules\Operators\InvalidBinaryOperationRule [1]