Skip to content

Latest commit

 

History

History
26 lines (22 loc) · 687 Bytes

ForeachArrayMergeSniff.md

File metadata and controls

26 lines (22 loc) · 687 Bytes

Rule: array_merge(...) is used in a loop and is a resources greedy construction

Reason

Merging arrays in a loop is slow and causes high CPU usage.

How to Fix

Typical example when array_merge is being used in the loop:

    $options = [];
    foreach ($configurationSources as $source) {
        // code here
        $options = array_merge($options, $source->getOptions());
    }

In order to reduce execution time array_merge can be called only once:

    $options = [[]];
    foreach ($configurationSources as $source) {
        // code here
        $options[] = $source->getOptions();
    }

    // PHP 5.6+
    $options = array_merge(...$options);