Passed
Push — master ( c664d2...12b4e1 )
by Lynh
10:23
created

array_merge_recursive_unique()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 19
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 32
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Jenky\Hermes\Contracts\Hermes;
4
5
if (! function_exists('guzzle')) {
6
    /**
7
     * Get a guzzle client instance.
8
     *
9
     * @param  string|null  $channel
10
     * @param  array  $options
11
     * @return \Jenky\Hermes\Contracts\Hermes|\GuzzleHttp\Client
12
     */
13
    function guzzle($channel = null, array $options = [])
14
    {
15
        return $channel ? app(Hermes::class)->channel($channel, $options) : app(Hermes::class);
16
    }
17
}
18
19
if (! function_exists('array_merge_recursive_unique')) {
20
    /**
21
     * Merges any number of arrays / parameters recursively, using the left array as base, giving priority to the right array. Replacing entries with string keys with values from latter arrays.
22
     *
23
     * @return array
24
     */
25
    function array_merge_recursive_unique()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        $arrays = func_get_args();
28
29
        if (count($arrays) < 2) {
30
            if ($arrays === []) {
31
                return [];
32
            } else {
33
                return $arrays[0];
34
            }
35
        }
36
37
        $merged = array_shift($arrays);
38
39
        foreach ($arrays as $array) {
40
            foreach ($array as $key => $value) {
41
                if (is_array($value) && (isset($merged[$key]) && is_array($merged[$key]))) {
42
                    $merged[$key] = array_merge_recursive_unique($merged[$key], $value);
43
                } else {
44
                    if (is_numeric($key)) {
45
                        if (! in_array($value, $merged)) {
46
                            $merged[] = $value;
47
                        }
48
                    } else {
49
                        $merged[$key] = $value;
50
                    }
51
                }
52
            }
53
            unset($key, $value);
54
        }
55
56
        return $merged;
57
    }
58
}
59