Total Complexity | 70 |
Total Lines | 452 |
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( |
||
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( |
||
197 | $existing_row, |
||
198 | $conflict_row |
||
199 | ){ |
||
200 | static::mustBeCountable($existing_row); |
||
201 | static::mustBeCountable($conflict_row); |
||
202 | |||
203 | $merge = static::mergeRecursiveCustom( |
||
204 | $existing_row, |
||
205 | $conflict_row, |
||
206 | function ($existing_value, $conflict_value, $column) { |
||
207 | |||
208 | if ( ! $existing_value instanceof MergeBucket) { |
||
209 | $existing_value = MergeBucket::from()->push($existing_value); |
||
210 | } |
||
211 | |||
212 | // We store the new value with their previous ones |
||
213 | if ( ! $conflict_value instanceof MergeBucket) { |
||
214 | $conflict_value = MergeBucket::from()->push($conflict_value); |
||
215 | } |
||
216 | |||
217 | foreach ($conflict_value->toArray() as $conflict_key => $conflict_entry) { |
||
218 | $existing_value->push($conflict_entry); |
||
219 | } |
||
220 | |||
221 | return $existing_value; |
||
222 | }, |
||
223 | 0 |
||
224 | ); |
||
225 | |||
226 | return $merge; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * This is the cleaning part of self::mergePreservingDistincts() |
||
231 | * |
||
232 | * @param array|Countable $row |
||
233 | * @param array $options : 'excluded_columns' |
||
234 | */ |
||
235 | public static function cleanMergeDuplicates($row, array $options=[]) |
||
236 | { |
||
237 | static::mustBeCountable($row); |
||
238 | |||
239 | $excluded_columns = isset($options['excluded_columns']) |
||
240 | ? $options['excluded_columns'] |
||
241 | : [] |
||
242 | ; |
||
243 | |||
244 | foreach ($row as $column => &$values) { |
||
245 | if ( ! $values instanceof MergeBucket) |
||
246 | continue; |
||
247 | |||
248 | if (in_array($column, $excluded_columns)) |
||
249 | continue; |
||
250 | |||
251 | $values = Arrays::unique($values); |
||
252 | if (count($values) == 1) |
||
253 | $values = $values[0]; |
||
254 | } |
||
255 | |||
256 | return $row; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * This is the cleaning last part of self::mergePreservingDistincts() |
||
261 | * |
||
262 | * @param array|Countable $row |
||
263 | * @param array $options : 'excluded_columns' |
||
264 | * |
||
265 | * @see mergePreservingDistincts() |
||
266 | * @see cleanMergeDuplicates() |
||
267 | */ |
||
268 | public static function cleanMergeBuckets($row, array $options=[]) |
||
269 | { |
||
270 | static::mustBeCountable($row); |
||
271 | |||
272 | $excluded_columns = isset($options['excluded_columns']) |
||
273 | ? $options['excluded_columns'] |
||
274 | : [] |
||
275 | ; |
||
276 | |||
277 | foreach ($row as $column => &$values) { |
||
278 | if (in_array($column, $excluded_columns)) |
||
279 | continue; |
||
280 | |||
281 | if ($values instanceof MergeBucket) |
||
282 | $values = $values->toArray(); |
||
283 | } |
||
284 | |||
285 | return $row; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Replacement of array_unique, keeping the first key. |
||
290 | * |
||
291 | * @param array|\Traversable $array |
||
292 | * @return array|\Traversable With unique values |
||
293 | * |
||
294 | * @todo Options to keep another key than the first one? |
||
295 | */ |
||
296 | public static function unique($array) |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Replacement of array_sum wich throws exceptions instead of skipping |
||
323 | * bad operands. |
||
324 | * |
||
325 | * @param array|\Traversable $array |
||
326 | * @return int|double The sum |
||
327 | * |
||
328 | * @todo Support options like 'strict', 'skip_non_scalars', 'native' |
||
329 | */ |
||
330 | public static function sum($array) |
||
331 | { |
||
332 | static::mustBeCountable($array); |
||
333 | |||
334 | $sum = 0; |
||
335 | foreach ($array as $key => &$value) { // &for optimization |
||
336 | if (is_scalar($value)) { |
||
337 | $sum += $value; |
||
338 | } |
||
339 | elseif (is_null($value)) { |
||
340 | continue; |
||
341 | } |
||
342 | elseif (is_array($value)) { |
||
343 | throw new \InvalidArgumentException( |
||
344 | "Trying to sum an array with '$sum': ".var_export($value, true) |
||
345 | ); |
||
346 | } |
||
347 | elseif (is_object($value)) { |
||
348 | if ( ! method_exists($value, 'toNumber')) { |
||
349 | throw new \InvalidArgumentEXception( |
||
350 | "Trying to sum a ".get_class($value)." object which cannot be casted as a number. " |
||
351 | ."Please add a toNumber() method." |
||
352 | ); |
||
353 | } |
||
354 | |||
355 | $sum += $value->toNumber(); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | return $sum; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * This method returns a classical mathemartic weighted mean. |
||
364 | * |
||
365 | * @todo It would ideally handled by a bridge with this fantastic math |
||
366 | * lib https://github.com/markrogoyski/math-php/ but we need the support |
||
367 | * of PHP 7 first. |
||
368 | * |
||
369 | * @see https://en.wikipedia.org/wiki/Weighted_arithmetic_mean |
||
370 | * @see https://github.com/markrogoyski/math-php/ |
||
371 | */ |
||
372 | public static function weightedMean($values, $weights) |
||
373 | { |
||
374 | if ($values instanceof ChainableArray) |
||
375 | $values = $values->toArray(); |
||
376 | |||
377 | if ($weights instanceof ChainableArray) |
||
378 | $weights = $weights->toArray(); |
||
379 | |||
380 | if ( ! is_array($values)) |
||
381 | $values = [$values]; |
||
382 | |||
383 | if ( ! is_array($weights)) |
||
384 | $weights = [$weights]; |
||
385 | |||
386 | if (count($values) != count($weights)) { |
||
387 | throw new \InvalidArgumentException( |
||
388 | "Different number of " |
||
389 | ." values and weights for weight mean calculation: \n" |
||
390 | .var_export($values, true)."\n\n" |
||
391 | .var_export($weights, true) |
||
392 | ); |
||
393 | } |
||
394 | |||
395 | if (!$values) |
||
396 | return null; |
||
397 | |||
398 | $weights_sum = array_sum($weights); |
||
399 | if (!$weights_sum) |
||
400 | return 0; |
||
401 | |||
402 | $weighted_sum = 0; |
||
403 | foreach ($values as $i => $value) { |
||
404 | $weighted_sum += $value * $weights[$i]; |
||
405 | } |
||
406 | |||
407 | return $weighted_sum / $weights_sum; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * This is not required anymore with PHP 7. |
||
412 | * |
||
413 | * @return bool |
||
414 | */ |
||
415 | public static function isTraversable($value) |
||
416 | { |
||
417 | return $value instanceof \Traversable || is_array($value); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * This is not required anymore with PHP 7. |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | public static function isCountable($value) |
||
426 | { |
||
427 | return $value instanceof \Countable || is_array($value); |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | */ |
||
432 | public static function mustBeCountable($value) |
||
459 | } |
||
460 | } |
||
461 | |||
462 | /**/ |
||
463 | } |
||
464 |