No Double Operators
ID |
php.no_double_operators |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
reliability, suspicious-operator |
Description
Reports two operator shapes that are almost always typing mistakes: a doubled unary
operator (!!$x or ~~$x), and a plain assignment whose value begins with an immediately
adjacent unary + or - ($a =+ $b, $a =- $b), which is the classic mistype of the
augmented operators += and -=.
Rationale
!!$x applies logical negation twice, returning the original boolean value, and ~~$x
applies bitwise complement twice, returning the original integer — both are no-ops that read
as if they do something. Worse, $a =+ $b looks like the augmented add $a += $b but
actually parses as $a = (+$b), silently overwriting $a instead of incrementing it. These
patterns are surface typos with no legitimate use.
<?php
$flag = !!$value; // FLAW — double negation, equivalent to (bool) $value written by mistake
$mask = ~~$bits; // FLAW — double complement, a no-op
$total =+ $amount; // FLAW — meant += but overwrites $total with +$amount
$total += $amount; // OK — augmented addition
$delta = -$amount; // OK — assignment of a negated value (note the space)
$flag = !$value; // OK — single negation
Remediation
For a doubled !, use an explicit (bool) cast if a boolean is intended, or remove the
redundant operator. For =+ / =-, decide whether an augmented operation (+= / -=) or a
spaced assignment of a signed value (= -$x) was meant, and write it explicitly.