1.2.1280. Unsupported Types With Operators

Arrays, resources and objects are generally not accepted with unary and binary operators.

The operators are +, -, *, /, **, %, <<, >>, &, |, ^, ~, ++ and . In PHP 8.0, the rules have been made stricter and more consistent.

The only valid operator is +, combined with arrays in both operands. Other situations throw TypeError.

<?php

var_dump([] % [42]);
// int(0) in PHP 7.x
// TypeError in PHP 8.0 +

// Also impossible usage : index are string or int
$a = [];
$b = $c[$a];

?>

See also Stricter type checks for arithmetic/bitwise operators and TypeError.

1.2.1280.1. Suggestions

  • Do not use those values with those operators

  • Use a condition to skip this awkward situation

  • Add an extra step to turn this value into a valid type

1.2.1280.2. Specs

Short name

Structures/UnsupportedTypesWithOperators

Rulesets

All, Analyze, CE, CompatibilityPHP80

Exakat since

2.1.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

operator

Available in

Entreprise Edition, Community Edition, Exakat Cloud