1.2.1058. Scalar Are Not Arrays

It is wrong to use a scalar as an array, a Warning is emitted. PHP 7.4 emits a Warning in such situations.

Typehinted argument with a scalar are reported by this analysis. Also, nullable arguments, both with typehint and return type hint.

<?php

// Here, $x may be null, and in that case, the echo will fail.
function foo(?A $x) {
    echo $x[2];
}

?>

See also E_WARNING for invalid container read array-access.

1.2.1058.1. Suggestions

  • Update type hints to avoid scalar values

  • Remove the array syntax in the code using the results

  • Cast to string type, so the array notation is accessible

1.2.1058.2. Specs

Short name

Php/ScalarAreNotArrays

Rulesets

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

Exakat since

1.9.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

array, array-object

Available in

Entreprise Edition, Community Edition, Exakat Cloud