@@ -18,35 +18,35 @@ |
||
18 | 18 | { |
19 | 19 | parent::__construct(...func_get_args()); |
20 | 20 | |
21 | - $this->rewindStackWhile( function($backtrace, $level) { |
|
21 | + $this->rewindStackWhile(function($backtrace, $level) { |
|
22 | 22 | // Finds the closest caller |
23 | 23 | // TODO get the class of the caller instead of comparing |
24 | 24 | // with UsageException |
25 | - return isset($backtrace[ $level ]['class']) |
|
26 | - && $backtrace[ $level ]['class'] == __CLASS__; |
|
27 | - }, 0 ); |
|
25 | + return isset($backtrace[$level]['class']) |
|
26 | + && $backtrace[$level]['class'] == __CLASS__; |
|
27 | + }, 0); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | /** |
31 | 31 | */ |
32 | 32 | public function setStackLocationHere() |
33 | 33 | { |
34 | - $this->rewindStackWhile( function($backtrace, $level) { |
|
34 | + $this->rewindStackWhile(function($backtrace, $level) { |
|
35 | 35 | // Finds the closest caller |
36 | 36 | return $level < 2; |
37 | - }, 4 ); |
|
37 | + }, 4); |
|
38 | 38 | |
39 | 39 | return $this; |
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
43 | 43 | */ |
44 | - protected function rewindStackWhile(callable $scope_checker, $stack_max_depth=20) |
|
44 | + protected function rewindStackWhile(callable $scope_checker, $stack_max_depth = 20) |
|
45 | 45 | { |
46 | - $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $stack_max_depth); |
|
46 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $stack_max_depth); |
|
47 | 47 | $i = 1; |
48 | 48 | $caller = $backtrace[$i]; |
49 | - while ( $scope_checker( $backtrace, $i ) ) { |
|
49 | + while ($scope_checker($backtrace, $i)) { |
|
50 | 50 | $i++; |
51 | 51 | $caller = $backtrace[$i]; |
52 | 52 | // TODO remove the previous levels of the stack? |
@@ -135,16 +135,16 @@ discard block |
||
135 | 135 | public static function mergeRecursiveCustom( |
136 | 136 | $existing_row, |
137 | 137 | $conflict_row, |
138 | - callable $merge_resolver=null, |
|
139 | - $max_depth=null |
|
140 | - ){ |
|
138 | + callable $merge_resolver = null, |
|
139 | + $max_depth = null |
|
140 | + ) { |
|
141 | 141 | static::mustBeCountable($existing_row); |
142 | 142 | static::mustBeCountable($conflict_row); |
143 | 143 | |
144 | 144 | foreach ($conflict_row as $column => $conflict_value) { |
145 | 145 | |
146 | 146 | // not existing in first array |
147 | - if (!isset($existing_row[$column])) { |
|
147 | + if ( ! isset($existing_row[$column])) { |
|
148 | 148 | $existing_row[$column] = $conflict_value; |
149 | 149 | continue; |
150 | 150 | } |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | } |
178 | 178 | else { |
179 | 179 | // same resolution as array_merge_recursive |
180 | - if (!is_array($existing_value)) { |
|
180 | + if ( ! is_array($existing_value)) { |
|
181 | 181 | $existing_row[$column] = [$existing_value]; |
182 | 182 | } |
183 | 183 | |
@@ -200,14 +200,14 @@ discard block |
||
200 | 200 | public static function mergePreservingDistincts( |
201 | 201 | $existing_row, |
202 | 202 | $conflict_row |
203 | - ){ |
|
203 | + ) { |
|
204 | 204 | static::mustBeCountable($existing_row); |
205 | 205 | static::mustBeCountable($conflict_row); |
206 | 206 | |
207 | 207 | $merge = static::mergeRecursiveCustom( |
208 | 208 | $existing_row, |
209 | 209 | $conflict_row, |
210 | - function ($existing_value, $conflict_value, $column) { |
|
210 | + function($existing_value, $conflict_value, $column) { |
|
211 | 211 | |
212 | 212 | if ( ! $existing_value instanceof MergeBucket) { |
213 | 213 | $existing_value = MergeBucket::from()->push($existing_value); |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | * @param array|Countable $row |
237 | 237 | * @param array $options : 'excluded_columns' |
238 | 238 | */ |
239 | - public static function cleanMergeDuplicates($row, array $options=[]) |
|
239 | + public static function cleanMergeDuplicates($row, array $options = []) |
|
240 | 240 | { |
241 | 241 | static::mustBeCountable($row); |
242 | 242 | |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | * @see mergePreservingDistincts() |
270 | 270 | * @see cleanMergeDuplicates() |
271 | 271 | */ |
272 | - public static function cleanMergeBuckets($row, array $options=[]) |
|
272 | + public static function cleanMergeBuckets($row, array $options = []) |
|
273 | 273 | { |
274 | 274 | static::mustBeCountable($row); |
275 | 275 | |
@@ -310,13 +310,13 @@ discard block |
||
310 | 310 | $id = serialize($value); |
311 | 311 | } |
312 | 312 | |
313 | - if (isset($ids[ $id ])) { |
|
314 | - unset($array[ $key ]); |
|
315 | - $ids[ $id ][] = $key; |
|
313 | + if (isset($ids[$id])) { |
|
314 | + unset($array[$key]); |
|
315 | + $ids[$id][] = $key; |
|
316 | 316 | continue; |
317 | 317 | } |
318 | 318 | |
319 | - $ids[ $id ] = [$key]; |
|
319 | + $ids[$id] = [$key]; |
|
320 | 320 | } |
321 | 321 | |
322 | 322 | return $array; |
@@ -336,7 +336,7 @@ discard block |
||
336 | 336 | } |
337 | 337 | else { |
338 | 338 | throw new \InvalidArgumentException( |
339 | - "keyExists() method missing on :\n". var_export($array, true) |
|
339 | + "keyExists() method missing on :\n".var_export($array, true) |
|
340 | 340 | ); |
341 | 341 | } |
342 | 342 | |
@@ -412,16 +412,16 @@ discard block |
||
412 | 412 | throw new \InvalidArgumentException( |
413 | 413 | "Different number of " |
414 | 414 | ." values and weights for weight mean calculation: \n" |
415 | - .var_export($values, true)."\n\n" |
|
415 | + .var_export($values, true)."\n\n" |
|
416 | 416 | .var_export($weights, true) |
417 | 417 | ); |
418 | 418 | } |
419 | 419 | |
420 | - if (!$values) |
|
420 | + if ( ! $values) |
|
421 | 421 | return null; |
422 | 422 | |
423 | - $weights_sum = array_sum($weights); |
|
424 | - if (!$weights_sum) |
|
423 | + $weights_sum = array_sum($weights); |
|
424 | + if ( ! $weights_sum) |
|
425 | 425 | return 0; |
426 | 426 | |
427 | 427 | $weighted_sum = 0; |
@@ -470,8 +470,8 @@ discard block |
||
470 | 470 | ); |
471 | 471 | |
472 | 472 | // The true location of the throw is still available through the backtrace |
473 | - $trace_location = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
|
474 | - $reflectionClass = new \ReflectionClass( get_class($exception) ); |
|
473 | + $trace_location = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
|
474 | + $reflectionClass = new \ReflectionClass(get_class($exception)); |
|
475 | 475 | |
476 | 476 | // file |
477 | 477 | if (isset($trace_location['file'])) { |
@@ -508,8 +508,8 @@ discard block |
||
508 | 508 | ); |
509 | 509 | |
510 | 510 | // The true location of the throw is still available through the backtrace |
511 | - $trace_location = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
|
512 | - $reflectionClass = new \ReflectionClass( get_class($exception) ); |
|
511 | + $trace_location = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; |
|
512 | + $reflectionClass = new \ReflectionClass(get_class($exception)); |
|
513 | 513 | |
514 | 514 | // file |
515 | 515 | if (isset($trace_location['file'])) { |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | * |
550 | 550 | * @return string The unique identifier of the group |
551 | 551 | */ |
552 | - public static function generateGroupId($row, array $groups_definitions, array $options=[]) |
|
552 | + public static function generateGroupId($row, array $groups_definitions, array $options = []) |
|
553 | 553 | { |
554 | 554 | Arrays::mustBeCountable($row); |
555 | 555 | |
@@ -558,7 +558,7 @@ discard block |
||
558 | 558 | : ':' |
559 | 559 | ; |
560 | 560 | |
561 | - $groups_separator = ! empty($options['groups_separator']) |
|
561 | + $groups_separator = ! empty($options['groups_separator']) |
|
562 | 562 | ? $options['groups_separator'] |
563 | 563 | : '-' |
564 | 564 | ; |
@@ -572,32 +572,32 @@ discard block |
||
572 | 572 | } |
573 | 573 | |
574 | 574 | if (is_string($group_definition_value)) { |
575 | - if ( (is_array($row) && ! array_key_exists($group_definition_value, $row)) |
|
575 | + if ((is_array($row) && ! array_key_exists($group_definition_value, $row)) |
|
576 | 576 | || ($row instanceof \ArrayAcces && ! $row->offsetExists($group_definition_value)) |
577 | 577 | ) { |
578 | 578 | throw new UsageException( |
579 | 579 | 'Unset column for group id generation: ' |
580 | 580 | .var_export($group_definition_value, true) |
581 | - ."\n" . var_export($row, true) |
|
581 | + ."\n".var_export($row, true) |
|
582 | 582 | ); |
583 | 583 | } |
584 | 584 | |
585 | 585 | $part_name .= $group_definition_value; |
586 | - $group_result_value = $row[ $group_definition_value ]; |
|
586 | + $group_result_value = $row[$group_definition_value]; |
|
587 | 587 | } |
588 | 588 | elseif (is_int($group_definition_value)) { |
589 | - if ( (is_array($row) && ! array_key_exists($group_definition_value, $row)) |
|
589 | + if ((is_array($row) && ! array_key_exists($group_definition_value, $row)) |
|
590 | 590 | || ($row instanceof \ArrayAcces && ! $row->offsetExists($group_definition_value)) |
591 | 591 | ) { |
592 | 592 | throw new UsageException( |
593 | 593 | 'Unset column for group id generation: ' |
594 | 594 | .var_export($group_definition_value, true) |
595 | - ."\n" . var_export($row, true) |
|
595 | + ."\n".var_export($row, true) |
|
596 | 596 | ); |
597 | 597 | } |
598 | 598 | |
599 | - $part_name .= $group_definition_value ? : '0'; |
|
600 | - $group_result_value = $row[ $group_definition_value ]; |
|
599 | + $part_name .= $group_definition_value ?: '0'; |
|
600 | + $group_result_value = $row[$group_definition_value]; |
|
601 | 601 | } |
602 | 602 | elseif (is_callable($group_definition_value)) { |
603 | 603 | |
@@ -621,12 +621,12 @@ discard block |
||
621 | 621 | throw new UsageException( |
622 | 622 | 'Bad value provided for group id generation: ' |
623 | 623 | .var_export($group_definition_value, true) |
624 | - ."\n" . var_export($row, true) |
|
624 | + ."\n".var_export($row, true) |
|
625 | 625 | ); |
626 | 626 | } |
627 | 627 | |
628 | - if (!is_null($part_name)) |
|
629 | - $group_parts[ $part_name ] = $group_result_value; |
|
628 | + if ( ! is_null($part_name)) |
|
629 | + $group_parts[$part_name] = $group_result_value; |
|
630 | 630 | } |
631 | 631 | |
632 | 632 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -638,13 +638,13 @@ discard block |
||
638 | 638 | if (is_object($group_value)) { |
639 | 639 | $group_value = get_class($group_value) |
640 | 640 | . '_' |
641 | - . hash( 'crc32b', var_export($group_value, true) ); |
|
641 | + . hash('crc32b', var_export($group_value, true)); |
|
642 | 642 | } |
643 | 643 | elseif (is_array($group_value)) { |
644 | - $group_value = 'array_' . hash( 'crc32b', var_export($group_value, true) ); |
|
644 | + $group_value = 'array_'.hash('crc32b', var_export($group_value, true)); |
|
645 | 645 | } |
646 | 646 | |
647 | - $out[] = $group_name . $key_value_separator . $group_value; |
|
647 | + $out[] = $group_name.$key_value_separator.$group_value; |
|
648 | 648 | } |
649 | 649 | |
650 | 650 | return implode($groups_separator, $out); |
@@ -65,14 +65,12 @@ discard block |
||
65 | 65 | ) |
66 | 66 | { |
67 | 67 | $array1[$key] = self::merge($array1[$key], $value); |
68 | - } |
|
69 | - else |
|
68 | + } else |
|
70 | 69 | { |
71 | 70 | $array1[$key] = $value; |
72 | 71 | } |
73 | 72 | } |
74 | - } |
|
75 | - else |
|
73 | + } else |
|
76 | 74 | { |
77 | 75 | foreach ($array2 as $value) |
78 | 76 | { |
@@ -97,14 +95,12 @@ discard block |
||
97 | 95 | ) |
98 | 96 | { |
99 | 97 | $array1[$key] = self::merge($array1[$key], $value); |
100 | - } |
|
101 | - else |
|
98 | + } else |
|
102 | 99 | { |
103 | 100 | $array1[$key] = $value; |
104 | 101 | } |
105 | 102 | } |
106 | - } |
|
107 | - else |
|
103 | + } else |
|
108 | 104 | { |
109 | 105 | foreach ($array2 as $value) |
110 | 106 | { |
@@ -174,8 +170,7 @@ discard block |
||
174 | 170 | $column, |
175 | 171 | ] |
176 | 172 | ); |
177 | - } |
|
178 | - else { |
|
173 | + } else { |
|
179 | 174 | // same resolution as array_merge_recursive |
180 | 175 | if (!is_array($existing_value)) { |
181 | 176 | $existing_row[$column] = [$existing_value]; |
@@ -246,15 +241,18 @@ discard block |
||
246 | 241 | ; |
247 | 242 | |
248 | 243 | foreach ($row as $column => &$values) { |
249 | - if ( ! $values instanceof MergeBucket) |
|
250 | - continue; |
|
244 | + if ( ! $values instanceof MergeBucket) { |
|
245 | + continue; |
|
246 | + } |
|
251 | 247 | |
252 | - if (in_array($column, $excluded_columns)) |
|
253 | - continue; |
|
248 | + if (in_array($column, $excluded_columns)) { |
|
249 | + continue; |
|
250 | + } |
|
254 | 251 | |
255 | 252 | $values = Arrays::unique($values); |
256 | - if (count($values) == 1) |
|
257 | - $values = $values[0]; |
|
253 | + if (count($values) == 1) { |
|
254 | + $values = $values[0]; |
|
255 | + } |
|
258 | 256 | } |
259 | 257 | |
260 | 258 | return $row; |
@@ -279,11 +277,13 @@ discard block |
||
279 | 277 | ; |
280 | 278 | |
281 | 279 | foreach ($row as $column => &$values) { |
282 | - if (in_array($column, $excluded_columns)) |
|
283 | - continue; |
|
280 | + if (in_array($column, $excluded_columns)) { |
|
281 | + continue; |
|
282 | + } |
|
284 | 283 | |
285 | - if ($values instanceof MergeBucket) |
|
286 | - $values = $values->toArray(); |
|
284 | + if ($values instanceof MergeBucket) { |
|
285 | + $values = $values->toArray(); |
|
286 | + } |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | return $row; |
@@ -305,8 +305,7 @@ discard block |
||
305 | 305 | foreach ($array as $key => $value) { |
306 | 306 | if (is_scalar($value)) { |
307 | 307 | $id = $value; |
308 | - } |
|
309 | - else { |
|
308 | + } else { |
|
310 | 309 | $id = serialize($value); |
311 | 310 | } |
312 | 311 | |
@@ -330,11 +329,9 @@ discard block |
||
330 | 329 | |
331 | 330 | if (is_array($array)) { |
332 | 331 | return array_key_exists($key, $array); |
333 | - } |
|
334 | - elseif ($array instanceof ChainableArray || method_exists($array, 'keyExists')) { |
|
332 | + } elseif ($array instanceof ChainableArray || method_exists($array, 'keyExists')) { |
|
335 | 333 | return $array->keyExists($key); |
336 | - } |
|
337 | - else { |
|
334 | + } else { |
|
338 | 335 | throw new \InvalidArgumentException( |
339 | 336 | "keyExists() method missing on :\n". var_export($array, true) |
340 | 337 | ); |
@@ -360,16 +357,13 @@ discard block |
||
360 | 357 | foreach ($array as $key => &$value) { // &for optimization |
361 | 358 | if (is_scalar($value)) { |
362 | 359 | $sum += $value; |
363 | - } |
|
364 | - elseif (is_null($value)) { |
|
360 | + } elseif (is_null($value)) { |
|
365 | 361 | continue; |
366 | - } |
|
367 | - elseif (is_array($value)) { |
|
362 | + } elseif (is_array($value)) { |
|
368 | 363 | throw new \InvalidArgumentException( |
369 | 364 | "Trying to sum an array with '$sum': ".var_export($value, true) |
370 | 365 | ); |
371 | - } |
|
372 | - elseif (is_object($value)) { |
|
366 | + } elseif (is_object($value)) { |
|
373 | 367 | if ( ! method_exists($value, 'toNumber')) { |
374 | 368 | throw new \InvalidArgumentEXception( |
375 | 369 | "Trying to sum a ".get_class($value)." object which cannot be casted as a number. " |
@@ -396,17 +390,21 @@ discard block |
||
396 | 390 | */ |
397 | 391 | public static function weightedMean($values, $weights) |
398 | 392 | { |
399 | - if ($values instanceof ChainableArray) |
|
400 | - $values = $values->toArray(); |
|
393 | + if ($values instanceof ChainableArray) { |
|
394 | + $values = $values->toArray(); |
|
395 | + } |
|
401 | 396 | |
402 | - if ($weights instanceof ChainableArray) |
|
403 | - $weights = $weights->toArray(); |
|
397 | + if ($weights instanceof ChainableArray) { |
|
398 | + $weights = $weights->toArray(); |
|
399 | + } |
|
404 | 400 | |
405 | - if ( ! is_array($values)) |
|
406 | - $values = [$values]; |
|
401 | + if ( ! is_array($values)) { |
|
402 | + $values = [$values]; |
|
403 | + } |
|
407 | 404 | |
408 | - if ( ! is_array($weights)) |
|
409 | - $weights = [$weights]; |
|
405 | + if ( ! is_array($weights)) { |
|
406 | + $weights = [$weights]; |
|
407 | + } |
|
410 | 408 | |
411 | 409 | if (count($values) != count($weights)) { |
412 | 410 | throw new \InvalidArgumentException( |
@@ -417,12 +415,14 @@ discard block |
||
417 | 415 | ); |
418 | 416 | } |
419 | 417 | |
420 | - if (!$values) |
|
421 | - return null; |
|
418 | + if (!$values) { |
|
419 | + return null; |
|
420 | + } |
|
422 | 421 | |
423 | 422 | $weights_sum = array_sum($weights); |
424 | - if (!$weights_sum) |
|
425 | - return 0; |
|
423 | + if (!$weights_sum) { |
|
424 | + return 0; |
|
425 | + } |
|
426 | 426 | |
427 | 427 | $weighted_sum = 0; |
428 | 428 | foreach ($values as $i => $value) { |
@@ -461,8 +461,9 @@ discard block |
||
461 | 461 | */ |
462 | 462 | public static function mustBeCountable($value) |
463 | 463 | { |
464 | - if (static::isCountable($value)) |
|
465 | - return true; |
|
464 | + if (static::isCountable($value)) { |
|
465 | + return true; |
|
466 | + } |
|
466 | 467 | |
467 | 468 | $exception = new \InvalidArgumentException( |
468 | 469 | "A value must be Countable instead of: \n" |
@@ -499,8 +500,9 @@ discard block |
||
499 | 500 | */ |
500 | 501 | public static function mustBeTraversable($value) |
501 | 502 | { |
502 | - if (static::isTraversable($value)) |
|
503 | - return true; |
|
503 | + if (static::isTraversable($value)) { |
|
504 | + return true; |
|
505 | + } |
|
504 | 506 | |
505 | 507 | $exception = new \InvalidArgumentException( |
506 | 508 | "A value must be Traversable instead of: \n" |
@@ -584,8 +586,7 @@ discard block |
||
584 | 586 | |
585 | 587 | $part_name .= $group_definition_value; |
586 | 588 | $group_result_value = $row[ $group_definition_value ]; |
587 | - } |
|
588 | - elseif (is_int($group_definition_value)) { |
|
589 | + } elseif (is_int($group_definition_value)) { |
|
589 | 590 | if ( (is_array($row) && ! array_key_exists($group_definition_value, $row)) |
590 | 591 | || ($row instanceof \ArrayAcces && ! $row->offsetExists($group_definition_value)) |
591 | 592 | ) { |
@@ -598,8 +599,7 @@ discard block |
||
598 | 599 | |
599 | 600 | $part_name .= $group_definition_value ? : '0'; |
600 | 601 | $group_result_value = $row[ $group_definition_value ]; |
601 | - } |
|
602 | - elseif (is_callable($group_definition_value)) { |
|
602 | + } elseif (is_callable($group_definition_value)) { |
|
603 | 603 | |
604 | 604 | if (is_string($group_definition_value)) { |
605 | 605 | $part_name .= $group_definition_value; |
@@ -608,16 +608,14 @@ discard block |
||
608 | 608 | elseif (is_object($group_definition_value) && ($group_definition_value instanceof \Closure)) { |
609 | 609 | $part_name .= 'unnamed-closure-' |
610 | 610 | . hash('crc32b', var_export($group_definition_value, true)); |
611 | - } |
|
612 | - elseif (is_array($group_definition_value)) { |
|
611 | + } elseif (is_array($group_definition_value)) { |
|
613 | 612 | $part_name .= implode('::', $group_definition_value); |
614 | 613 | } |
615 | 614 | |
616 | 615 | $group_result_value = call_user_func_array($group_definition_value, [ |
617 | 616 | $row, &$part_name |
618 | 617 | ]); |
619 | - } |
|
620 | - else { |
|
618 | + } else { |
|
621 | 619 | throw new UsageException( |
622 | 620 | 'Bad value provided for group id generation: ' |
623 | 621 | .var_export($group_definition_value, true) |
@@ -625,8 +623,9 @@ discard block |
||
625 | 623 | ); |
626 | 624 | } |
627 | 625 | |
628 | - if (!is_null($part_name)) |
|
629 | - $group_parts[ $part_name ] = $group_result_value; |
|
626 | + if (!is_null($part_name)) { |
|
627 | + $group_parts[ $part_name ] = $group_result_value; |
|
628 | + } |
|
630 | 629 | } |
631 | 630 | |
632 | 631 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -639,8 +638,7 @@ discard block |
||
639 | 638 | $group_value = get_class($group_value) |
640 | 639 | . '_' |
641 | 640 | . hash( 'crc32b', var_export($group_value, true) ); |
642 | - } |
|
643 | - elseif (is_array($group_value)) { |
|
641 | + } elseif (is_array($group_value)) { |
|
644 | 642 | $group_value = 'array_' . hash( 'crc32b', var_export($group_value, true) ); |
645 | 643 | } |
646 | 644 |