Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 28 | class ArrayUtils { |
||
| 29 | /** |
||
| 30 | * Sort the given array in a pseudo-random order which depends only on the |
||
| 31 | * given key and each element value in $array. This is typically used for load |
||
| 32 | * balancing between servers each with a local cache. |
||
| 33 | * |
||
| 34 | * Keys are preserved. The input array is modified in place. |
||
| 35 | * |
||
| 36 | * Note: Benchmarking on PHP 5.3 and 5.4 indicates that for small |
||
| 37 | * strings, md5() is only 10% slower than hash('joaat',...) etc., |
||
| 38 | * since the function call overhead dominates. So there's not much |
||
| 39 | * justification for breaking compatibility with installations |
||
| 40 | * compiled with ./configure --disable-hash. |
||
| 41 | * |
||
| 42 | * @param array $array Array to sort |
||
| 43 | * @param string $key |
||
| 44 | * @param string $separator A separator used to delimit the array elements and the |
||
| 45 | * key. This can be chosen to provide backwards compatibility with |
||
| 46 | * various consistent hash implementations that existed before this |
||
| 47 | * function was introduced. |
||
| 48 | */ |
||
| 49 | public static function consistentHashSort( &$array, $key, $separator = "\000" ) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Given an array of non-normalised probabilities, this function will select |
||
| 61 | * an element and return the appropriate key |
||
| 62 | * |
||
| 63 | * @param array $weights |
||
| 64 | * @return bool|int|string |
||
| 65 | */ |
||
| 66 | public static function pickRandom( $weights ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Do a binary search, and return the index of the largest item that sorts |
||
| 97 | * less than or equal to the target value. |
||
| 98 | * |
||
| 99 | * @since 1.23 |
||
| 100 | * |
||
| 101 | * @param callable $valueCallback A function to call to get the value with |
||
| 102 | * a given array index. |
||
| 103 | * @param int $valueCount The number of items accessible via $valueCallback, |
||
| 104 | * indexed from 0 to $valueCount - 1 |
||
| 105 | * @param callable $comparisonCallback A callback to compare two values, returning |
||
| 106 | * -1, 0 or 1 in the style of strcmp(). |
||
| 107 | * @param string $target The target value to find. |
||
| 108 | * |
||
| 109 | * @return int|bool The item index of the lower bound, or false if the target value |
||
| 110 | * sorts before all items. |
||
| 111 | */ |
||
| 112 | public static function findLowerBound( $valueCallback, $valueCount, |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Do array_diff_assoc() on multi-dimensional arrays. |
||
| 148 | * |
||
| 149 | * Note: empty arrays are removed. |
||
| 150 | * |
||
| 151 | * @since 1.23 |
||
| 152 | * |
||
| 153 | * @param array $array1 The array to compare from |
||
| 154 | * @param array $array2,... More arrays to compare against |
||
| 155 | * @return array An array containing all the values from array1 |
||
| 156 | * that are not present in any of the other arrays. |
||
| 157 | */ |
||
| 158 | public static function arrayDiffAssocRecursive( $array1 ) { |
||
| 187 | } |
||
| 188 |
It seems like you are relying on a variable being defined by an iteration: