Closed
Description
Rule
The use of slow array functions (array_merge
) in loop is discouraged.
Reason
Merging arrays in a loop is slow and causes high CPU usage. Some benchmarking.
Bad example:
$options = [];
foreach ($configurationSources as $source) {
// code here
$options = array_merge($options, $source->getOptions());
}
Good example:
$options = [];
foreach ($configurationSources as $source) {
// code here
$options[] = $source->getOptions();
}
// PHP 5.6+
$options = array_merge([], ...$options);
Implementation
- Subscribe to
T_STRING
token and check if it's content isarray_merge
. - Try to find outer loop.
- If loop is found rase a warning (need to discuss additional logic).
<severity>7</severity>
<type>warning</type>