1.2.672. Mass Creation Of Arrays

This rule reports literal creation of arrays. This happens by assigning several indices in a row.

It is recommended to federate all index assignations in one array() or [] call, rather than multiple successive instructions.

This applies to all constant and simple variable assignation. Anything more complex could still be done, afterward. That way, it reduces partially the load of the easy assignations.

<?php

$row['name'] = $name;
$row['last'] = $last;
$row['address'] = $address;
if (!isset($zipCode)) {
    $row['zipCode'] = $zipCode;
}

$row = [
    'name' => $name,
    'last' => $last,
    'address' => $address,
];
if (!isset($zipCode)) {
    $row['zipCode'] = $zipCode;
}

?>

1.2.672.1. Connex PHP features

1.2.672.1.1. Suggestions

  • Use compact() to collect variables to one array.

  • Replace assignations with one [] call.

1.2.672.1.2. Specs

Short name

Arrays/MassCreation

Rulesets

All, Changed Behavior

Exakat since

1.1.8

PHP Version

All

Severity

Minor

Time To Fix

Slow (1 hour)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud