1.2.1462. array_merge With Ellipsis

Ellipsis, or , returns a null when the operand array is empty. This doesn’t suit array_merge().

It is recommended to use a coalesce operator, to handle graciously an empty array : use an empty array as default value.

This applies to the following PHP functions :

<?php

// Correct usage of array_merge and ellipsis
$a = [ [1,2], [3,4]];
$b = array_merge(...$a);

// Notee the nested array
$a = [ ];
$b = array_merge(...$a ?: [[]] );

// Yield an error because $a is empty
$a = [ ];
$b = array_merge(...$a);

?>

1.2.1462.1. Suggestions

  • Use one of the coalesce operator to default to an empty array, avoiding a runtime warning.

  • Check the content of the expanded array before using it

1.2.1462.2. Specs

Short name

Structures/ArrayMergeWithEllipsis

Rulesets

All, CompatibilityPHP71, CompatibilityPHP72, CompatibilityPHP73

Exakat since

1.7.6

PHP Version

With PHP 7.4 and older

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud