-
Notifications
You must be signed in to change notification settings - Fork 163
Closed
Labels
acceptedNew rule is acceptedNew rule is acceptednew ruleNew feature implementationNew feature implementationproposalNew rule proposalNew rule proposal
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>
sergy-gzh, himanshu8dev, larsroettig, buskamuza, navarr and 1 more
Metadata
Metadata
Assignees
Labels
acceptedNew rule is acceptedNew rule is acceptednew ruleNew feature implementationNew feature implementationproposalNew rule proposalNew rule proposal