1.2.30. Always Positive Comparison

Some PHP native functions, such as count(), strlen(), or abs() only returns positive or null values.

When comparing their result to 0, the following expressions are always true and should be avoided.

<?php

$a = [1, 2, 3];

// OK, count() may return 0 or more
var_dump(count($a) >= 0);

// This is always false
var_dump(count($a) < 0);

?>

1.2.30.1. Suggestions

  • Compare count() to non-zero values

  • Use empty()

1.2.30.2. Specs

Short name

Structures/NeverNegative

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Instant (5 mins)

Precision

Very high

Examples

Magento

Available in

Entreprise Edition, Community Edition, Exakat Cloud