1.2.835. No Spread For Hash

The spread operator ... only works on integer-indexed arrays.

<?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.835.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.835.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

Features

ellipsis, array, array-spread

Available in

Entreprise Edition, Exakat Cloud