1.2.1205. Too Many Extractions

Using a loop to extract all the values from an array or an object, but failing to use them all later.

This means too much work was applied to the extraction, and it could be shorten by choosing the actual values.

<?php

function bar($array) {
     foreach(source() as $k => $v) {
             $data[$k] = $v;
     }

     // returning the whole array, so all can be useful
     return $data;
}

function foo($array) {
     foreach(source() as $k => $v) {
             $data[$k] = $v;
     }

     // only using one value, the rest is wasted
     echo $data['foo'];
}

?>

1.2.1205.1. Suggestions

  • Filter data before extracting them

  • Do not use a loop to extract all, but cherry pick the one that are needed

1.2.1205.2. Specs

Short name

Performances/TooManyExtractions

Rulesets

All, Changed Behavior, Performances

Exakat since

2.5.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud