Refactor PHP legacy arrays -


i have legacy code clear, clean , concise. of legacy logic lies in using arrays. code here:

if (isset($statistics[$lowerinstance])) {         $statistics[$lowerinstance][$size]['count'] +=     $items[$lowesttype]['count'];         $statistics[$lowerinstance][$size]['amount'] += $items[$lowesttype]['amount'] + (($items[$highesttype]['amount'] / $items[$highesttype]['count']) * $items[$lowesttype]['count']);     } else {         $statistics[$lowerinstance][$size]['count'] = $items[$lowesttype]['count'];         $statistics[$lowerinstance][$size]['amount'] = $items[$lowesttype]['amount'] + (($items[$highesttype]['amount'] / $items[$highesttype]['count']) * $items[$lowesttype]['count']);     } if (isset($statistics[$higherinstance])) {         $statistics[$higherinstance][$size]['count'] += $items[$highesttype]['count'] - $items[$lowesttype]['count'];         $statistics[$higherinstance][$size]['amount'] += $items[$highesttype]['amount'] - (($items[$highesttype]['amount'] / $items[$highesttype]['count']) * $items[$lowesttype]['count']);     } else {         $statistics[$higherinstance][$size]['count'] = $items[$highesttype]['count'] - $items[$lowesttype]['count'];         $statistics[$higherinstance][$size]['amount'] = $items[$highesttype]['amount'] - (($items[$highesttype]['amount'] / $items[$highesttype]['count']) * $items[$lowesttype]['count']);     } 

this fraction of particular method, goes on , on.

i'm having difficulty how make clearer , easier work with. not easy (i think) move multidimensional array arrayaccess type object although might slight improvement.

is there generic way of refactoring multidimensional arrays, or examples of how might done? not on particular problem more generic way handle php multidimensional array hell?

i took martin fowler's book, refactoring: amazon.

replace array object

so read wrote , decided have go @ it. started create unit test method. simple 1 pass bunch of data, export outcome , use outcome in assertions. did before refactoring run unit test after each iteration know if had broken anything.

first order of business according fowler create class public array replace offending array:

class statistics {     public $data = []; } 

i created variable:

$statistics = new statistics(); 

then replaced instances of variable, $statistics became $statistics->data

so example:

$statistics[$higherinstance][$size]['count'] += $items[$highesttype]['count'] - $items[$lowesttype]['count']; 

would become:

$statistics->data[$higherinstance][$size]['count'] += $items[$highesttype]['count'] - $items[$lowesttype]['count']; 

this interesting because opened new possibilities. did next refactor out of code own methods:

protected function getinstancedata(statistics $statistics, $higherinstance, $size, $highesttype, $items) {     $statistics->data[$higherinstance][$size]['count'] += $items[$highesttype]['count'] - $items[$lowesttype]['count'];     ...     return $statistics->data; 

then move method statistics class itself:

public function getinstancedata($higherinstance, $size, $highesttype, $items) {     $this->data[$higherinstance][$size]['count'] += $items[$highesttype]['count'] - $items[$lowesttype]['count'];     ...     return $this->data; 

i did several chunks of code. didn't particularly parameters now, think improvement, code atleast little bit less noisy.

i removed return $this->data methods no longer necessary, array contained within statistics class.

this opened further refactorings, breaking array further , delegating of responsibilities new classes.

i think how deal these type of arrays on, isolate them classes , break them up.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -