我有一些遺留的代碼,只是清晰,乾淨和簡潔。這些遺留邏輯大部分在於使用數組。像這樣的代碼在這裏:重構PHP傳統陣列
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']);
}
這只是這個特定方法的一小部分,它繼續下去。
我遇到了一些困難,如何使它更清晰,更容易使用。雖然這可能略有改善,但將多維數組移動到ArrayAccess類型對象並不容易(我認爲)。
有沒有一些通用的重構多維數組的方法,或者它是如何完成的一些例子?不只是在這個特定的問題,而是一個更通用的方式來處理PHP多維數組地獄?
核心應該是:1)*瞭解*代碼應該完成什麼。 2)以最熱門的方式重新執行該任務,取代舊的垃圾代碼。 - 當然,可能有辦法通過慢慢地用新代碼替換舊代碼的一部分來逐步地從那裏到那裏,但是如何在實踐中特別針對您的情況發揮出來對任何人都是不可能的。 – deceze
是的,謝謝。我有點希望得到一顆銀彈,但我想那裏沒有一個。 – Oli