| Total Complexity | 70 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Arrays often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Arrays, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Arrays |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Taken from Kohana's Arr class. |
||
| 11 | * |
||
| 12 | * Tests if an array is associative or not. |
||
| 13 | * |
||
| 14 | * // Returns TRUE |
||
| 15 | * Arr::isAssoc(array('username' => 'john.doe')); |
||
| 16 | * |
||
| 17 | * // Returns FALSE |
||
| 18 | * Arr::isAssoc('foo', 'bar'); |
||
| 19 | * |
||
| 20 | * @param array $array array to check |
||
| 21 | * @return boolean |
||
| 22 | */ |
||
| 23 | public static function isAssociative(array $array) |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Taken from Kohana's Arr class. |
||
| 35 | * |
||
| 36 | * Recursively merge two or more arrays. Values in an associative array |
||
| 37 | * overwrite previous values with the same key. Values in an indexed array |
||
| 38 | * are appended, but only when they do not already exist in the result. |
||
| 39 | * |
||
| 40 | * Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)! |
||
| 41 | * |
||
| 42 | * $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane')); |
||
| 43 | * $mary = array('name' => 'mary', 'children' => array('jane')); |
||
| 44 | * |
||
| 45 | * // John and Mary are married, merge them together |
||
| 46 | * $john = Arr::merge($john, $mary); |
||
| 47 | * |
||
| 48 | * // The output of $john will now be: |
||
| 49 | * array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane')) |
||
| 50 | * |
||
| 51 | * @param array $array1 initial array |
||
| 52 | * @param array $array2,... array to merge |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | public static function merge($array1, $array2) |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Equivalent of array_merge_recursive with more options. |
||
| 124 | * |
||
| 125 | * @param array $existing_row |
||
| 126 | * @param array $conflict_row |
||
| 127 | * @param callable|null $merge_resolver |
||
| 128 | * @param int $max_depth |
||
| 129 | * |
||
| 130 | * + If exist only in conflict row => add |
||
| 131 | * + If same continue |
||
| 132 | * + If different merge as array |
||
| 133 | */ |
||
| 134 | public static function mergeRecursiveCustom( |
||
| 135 | array $existing_row, |
||
| 136 | array $conflict_row, |
||
| 137 | callable $merge_resolver=null, |
||
| 138 | $max_depth=null |
||
| 139 | ){ |
||
| 140 | foreach ($conflict_row as $column => $conflict_value) { |
||
| 141 | |||
| 142 | // not existing in first array |
||
| 143 | if (!isset($existing_row[$column])) { |
||
| 144 | $existing_row[$column] = $conflict_value; |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $existing_value = $existing_row[$column]; |
||
| 149 | |||
| 150 | // two arrays so we recurse |
||
| 151 | if (is_array($existing_value) && is_array($conflict_value)) { |
||
| 152 | |||
| 153 | if ($max_depth === null || $max_depth > 0) { |
||
| 154 | $existing_row[$column] = static::mergeRecursiveCustom( |
||
| 155 | $existing_value, |
||
| 156 | $conflict_value, |
||
| 157 | $merge_resolver, |
||
| 158 | $max_depth - 1 |
||
| 159 | ); |
||
| 160 | continue; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($merge_resolver) { |
||
| 165 | $existing_row[$column] = call_user_func_array( |
||
| 166 | $merge_resolver, |
||
| 167 | [ |
||
| 168 | $existing_value, |
||
| 169 | $conflict_value, |
||
| 170 | $column, |
||
| 171 | ] |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | else { |
||
| 175 | // same resolution as array_merge_recursive |
||
| 176 | if (!is_array($existing_value)) { |
||
| 177 | $existing_row[$column] = [$existing_value]; |
||
| 178 | } |
||
| 179 | |||
| 180 | // We store the new value with their previous ones |
||
| 181 | $existing_row[$column][] = $conflict_value; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | return $existing_row; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Merges two rows |
||
| 190 | * |
||
| 191 | * @param array $existing_row |
||
| 192 | * @param array $conflict_row |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public static function mergePreservingDistincts( |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * This is the cleaning part of self::mergePreservingDistincts() |
||
| 228 | * |
||
| 229 | * @param array|Traversable $row |
||
| 230 | * @param array $options : 'excluded_columns' |
||
| 231 | */ |
||
| 232 | public static function cleanMergeDuplicates($row, array $options=[]) |
||
| 233 | { |
||
| 234 | if ( ! is_array($row) && ! $row instanceof \Traversable) { |
||
| 235 | throw new \InvalidArgumentException( |
||
| 236 | "\$row must be an array or a \Traversable instead of: \n" |
||
| 237 | .var_export($row, true) |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | $excluded_columns = isset($options['excluded_columns']) |
||
| 242 | ? $options['excluded_columns'] |
||
| 243 | : [] |
||
| 244 | ; |
||
| 245 | |||
| 246 | foreach ($row as $column => &$values) { |
||
| 247 | if ( ! $values instanceof MergeBucket) |
||
| 248 | continue; |
||
| 249 | |||
| 250 | if (in_array($column, $excluded_columns)) |
||
| 251 | continue; |
||
| 252 | |||
| 253 | $values = Arrays::unique($values); |
||
| 254 | if (count($values) == 1) |
||
| 255 | $values = $values[0]; |
||
| 256 | } |
||
| 257 | |||
| 258 | return $row; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * This is the cleaning part of self::mergePreservingDistincts() |
||
| 263 | * |
||
| 264 | * @param array|Traversable $row |
||
| 265 | * @param array $options : 'excluded_columns' |
||
| 266 | * |
||
| 267 | * @see mergePreservingDistincts() |
||
| 268 | */ |
||
| 269 | public static function cleanMergeBuckets($row, array $options=[]) |
||
| 270 | { |
||
| 271 | if ( ! is_array($row) && ! $row instanceof \Traversable) { |
||
| 272 | throw new \InvalidArgumentException( |
||
| 273 | "\$row must be an array or a \Traversable instead of: \n" |
||
| 274 | .var_export($row, true) |
||
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | $excluded_columns = isset($options['excluded_columns']) |
||
| 279 | ? $options['excluded_columns'] |
||
| 280 | : [] |
||
| 281 | ; |
||
| 282 | |||
| 283 | foreach ($row as $column => &$values) { |
||
| 284 | if (in_array($column, $excluded_columns)) |
||
| 285 | continue; |
||
| 286 | |||
| 287 | if ($values instanceof MergeBucket) |
||
| 288 | $values = $values->toArray(); |
||
| 289 | } |
||
| 290 | |||
| 291 | return $row; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Replacement of array_unique, keeping the first key. |
||
| 296 | * |
||
| 297 | * @param array|\Traversable $array |
||
| 298 | * @return array|\Traversable With unique values |
||
| 299 | * |
||
| 300 | * @todo Options to keep another key than the first one? |
||
| 301 | */ |
||
| 302 | public static function unique($array) |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Replacement of array_sum wich throws exceptions instead of skipping |
||
| 334 | * bad operands. |
||
| 335 | * |
||
| 336 | * @param array|\Traversable $array |
||
| 337 | * @return int|double The sum |
||
| 338 | * |
||
| 339 | * @todo Support options like 'strict', 'skip_non_scalars', 'native' |
||
| 340 | */ |
||
| 341 | public static function sum($array) |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * This method returns a classical mathemartic weighted mean. |
||
| 380 | * |
||
| 381 | * @todo It would ideally handled by a bridge with this fantastic math |
||
| 382 | * lib https://github.com/markrogoyski/math-php/ but we need the support |
||
| 383 | * of PHP 7 first. |
||
| 384 | * |
||
| 385 | * @see https://en.wikipedia.org/wiki/Weighted_arithmetic_mean |
||
| 386 | * @see https://github.com/markrogoyski/math-php/ |
||
| 387 | */ |
||
| 388 | public static function weightedMean($values, $weights) |
||
| 424 | } |
||
| 425 | |||
| 426 | /**/ |
||
| 428 |