Completed
Push — master ( dab7b5...3dae04 )
by Vladimir
02:49
created

ArrayUtilities   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
ccs 25
cts 30
cp 0.8333
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 15
                {
19
                    $item = array_merge($item, $value);
20
                    $valueMerged = true;
21
22
                    break;
23
                }
24 15
                else if ($mergedKey == $key)
25 15
                {
26 15
                    if (is_numeric($mergedKey))
27 15
                    {
28
                        $merged[] = $value;
29
                    }
30 15
                    else if (is_array($item))
31 15
                    {
32 15
                        $item = array_unique(array_merge($item, $value));
33 15
                    }
34
                    else
35
                    {
36 15
                        $item = $value;
37
                    }
38
39 15
                    $valueMerged = true;
40
41 15
                    break;
42
                }
43 15
            }
44
45 15
            if (!$valueMerged)
46 15
            {
47 15
                $merged[$key] = $value;
48 15
            }
49 15
        }
50
51 15
        return $merged;
52
    }
53
}