1.2.96. Can’t Count Non-Countable

Count() emits an error when it tries to count scalars or objects what don’t implement Countable interface.

<?php

// Normal usage
$a = array(1,2,3,4);
echo count($a)." items\n";

// Error emiting usage
$a = '1234';
echo count($a)." chars\n";

// Error emiting usage
echo count($unsetVar)." elements\n";

?>

See also Warn when counting non-countable types.

1.2.96.1. Suggestions

  • Add a check before using count such as a type check

1.2.96.2. Specs

Short name

Structures/CanCountNonCountable

Rulesets

All, CompatibilityPHP72

Exakat since

1.0.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

countable

Available in

Entreprise Edition, Exakat Cloud