1.2.1340. Use Same Types For Comparisons

Beware when using inequality operators that the type of the values are the same on both sites of the operators.

Different types may lead to PHP type juggling, where the values are first cast to one of the used types. Other comparisons are always failing, leading to unexpected behavior.

This applies to all inequality operators, as well as the spaceship operator.

This analysis skips comparisons between integers, floats and strings, as those are usually expected.

Thanks to Jordi Boggiano and Filippo Tessarotto.

<?php

// Both are wrong, while one should be true (depending on when you read this)
var_dump('1995-06-08' < new DateTimeImmutable());
var_dump('1995-06-08' > new DateTimeImmutable());

enum x : int {
    case A = 1;
    case B = 2;
}

// Both are false as objects are compared, not their integer value
var_dump(x::A < x::B);
var_dump(x::A > x::B);

var_dump(x::A->value < x::b->value);
var_dump(x::A->value > x::b->value);

?>

1.2.1340.1. Suggestions

  • Make sure that the same time

1.2.1340.2. Specs

Short name

Structures/UseSameTypesForComparisons

Rulesets

All, Analyze

Exakat since

2.4.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

inequality, enum-case, type-juggling

Available in

Entreprise Edition, Exakat Cloud