Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 673 Bytes

ForeachArrayMergeSniff.md

File metadata and controls

25 lines (21 loc) · 673 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();
    }

    $options = array_merge([], ...$options);