Skip to content

InvalidOperandType ​

error native on by default PHP ≥ 8.0

Group: Probable bugs

Report an arithmetic or bitwise operation on an array or an object that the code itself shows cannot take part in it. PHP throws a TypeError.

Example ​

php
<?php
function bump(array $counts): int
{
    return $counts * 2;
}

Reported:

  • line 4: Use a number here, not array.

Detection ​

  • An operand of +, -, *, /, %, **, &, |, ^, << or >>, or of the compound assignment of one of them.
  • The type of the operand comes from native declarations only, and every member of it is an array, or every member of it is a class whose whole hierarchy is known.

Exceptions (no report) ​

  • + between arrays, which merges them: an array operand of + is reported only when the other operand is known and cannot be an array.
  • An operand that may also be null, a scalar or an unresolved class.
  • An object of a class that takes part in arithmetic: GMP, BcMath\Number, FFI\CData, SimpleXMLElement and their subclasses.
  • A type that comes only from PHPDoc, which DocumentedOperandType covers.

Fix ​

None.

PHP versions ​

Active from PHP 8.0, where these operations throw a TypeError.

Configure ​

In custos.json:

json
{
  "rules": {
    "InvalidOperandType": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore InvalidOperandType

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.