1.2.843. No Spread For Hash

The spread operator ... used to work only on integer-indexed arrays. This limitation was removed in PHP 8.1 and more recent.

<?php

// This is valid, as ``"-33"`` is cast to integer by PHP automagically
var_dump(...[1,"-33" => 2, 3]);

// This is not valid
var_dump(...[1,"C" => 2, 3]);

?>

See also Variable-length argument lists.

1.2.843.2. Connex PHP features

1.2.843.2.1. Suggestions

  • Add a call to array_values() instead of the hash

  • Check the arguments beforehand with array_is_list()

  • Upgrade to PHP 8.1

1.2.843.2.2. Specs

Short name

Arrays/NoSpreadForHash

Rulesets

All, Analyze, Changed Behavior

Exakat since

1.9.3

PHP Version

With PHP 8.1 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud