Skip to content

MIR0213

PossiblyInvalidOperand

An operator is applied to a value whose type is a union, and at least one member of that union is not compatible with the operator. At runtime the operation may succeed or fail depending on which branch of the union is actually present.

<?php
function double(int|string $value): int|float {
return $value * 2; // string is not a valid operand for *
}

Narrow the type before applying the operator, or ensure all union members are compatible:

<?php
function double(int|string $value): int|float {
if (!is_int($value)) {
throw new \InvalidArgumentException('Expected int');
}
return $value * 2;
}