Passed
Push — master ( aed439...4d7571 )
by Stephen
01:06 queued 29s
created

ArrayHelpers   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 9
Bugs 2 Features 0
Metric Value
eloc 11
dl 0
loc 45
rs 10
c 9
b 2
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 3 1
A fromMerge() 0 3 1
A sum() 0 15 4
1
<?php
2
3
namespace Sfneal\Helpers\Arrays;
4
5
use Sfneal\Helpers\Arrays\Utils\ArrayUtility;
6
7
class ArrayHelpers
8
{
9
    /**
10
     * Instantiate a `ArrayUtility` instance by passing an $array to the constructor.
11
     *
12
     * @param array $array
13
     * @return ArrayUtility
14
     */
15
    public static function from(array $array): ArrayUtility
16
    {
17
        return new ArrayUtility($array);
18
    }
19
20
    /**
21
     * Instantiate a `ArrayUtility` instance by passing multiple $arrays to the constructor to be merged.
22
     *
23
     * @param mixed ...$arrays
24
     * @return ArrayUtility
25
     */
26
    public static function fromMerge(...$arrays): ArrayUtility
27
    {
28
        return new ArrayUtility(array_merge(...$arrays));
29
    }
30
31
    /**
32
     * Sum the values of two arrays.
33
     *
34
     * @param array $arrays
35
     * @return array
36
     */
37
    public static function sum(...$arrays): array
38
    {
39
        $sum = [];
40
41
        foreach ($arrays as $array) {
42
            foreach ($array as $index => $value) {
43
                if (! array_key_exists($index, $sum)) {
44
                    $sum[$index] = $value;
45
                } else {
46
                    $sum[$index] += $value;
47
                }
48
            }
49
        }
50
51
        return $sum;
52
    }
53
}
54