Completed
Push — master ( adff0c...fcdb68 )
by Vladimir
02:58
created

ArrayUtilities   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 0
loc 49
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D array_merge_defaults() 0 46 10
1
<?php
2
3
namespace allejo\stakx\Utilities;
4
5
abstract class ArrayUtilities
6
{
7 15
    public static function array_merge_defaults (array &$array1, array &$array2, $keyField)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
8
    {
9 15
        $merged = $array1;
10
11 15
        foreach ($array2 as $key => &$value)
12
        {
13 15
            $valueMerged = false;
14
15 15
            foreach ($merged as $mergedKey => &$item)
16
            {
17 15
                if (is_array($item) && array_key_exists($keyField, $item) && $item[$keyField] == $value[$keyField])
18
                {
19
                    $item = array_merge($item, $value);
20
                    $valueMerged = true;
21
22
                    break;
23
                }
24 15
                else if ($mergedKey == $key)
25
                {
26 15
                    if (is_numeric($mergedKey))
27
                    {
28
                        $merged[] = $value;
29
                    }
30 15
                    else if (is_array($item))
31
                    {
32 15
                        $item = array_unique(array_merge($item, $value));
33
                    }
34
                    else
35
                    {
36 15
                        $item = $value;
37
                    }
38
39 15
                    $valueMerged = true;
40
41 15
                    break;
42
                }
43
            }
44
45 15
            if (!$valueMerged)
46
            {
47 15
                $merged[$key] = $value;
48
            }
49
        }
50
51 15
        return $merged;
52
    }
53
}