Completed
Pull Request — master (#20)
by Vladimir
02:43
created

ArrayUtilities::array_merge_defaults()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.4632

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 13
nop 3
dl 0
loc 46
rs 4.983
c 0
b 0
f 0
ccs 25
cts 30
cp 0.8333
crap 10.4632

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
namespace allejo\stakx\Utilities;
4
5
abstract class ArrayUtilities
6
{
7 4
    public static function is_multidimensional (array &$array)
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 4
        foreach ($array as $element)
10
        {
11 4
            if (is_array($element))
12 4
                return true;
13 3
        }
14
15 3
        return false;
16
    }
17
    
18 16
    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...
19
    {
20 16
        $merged = $array1;
21
22 16
        foreach ($array2 as $key => &$value)
23
        {
24 16
            $valueMerged = false;
25
26 16
            foreach ($merged as $mergedKey => &$item)
27
            {
28 16
                if (is_array($item) && array_key_exists($keyField, $item) && $item[$keyField] == $value[$keyField])
29 16
                {
30
                    $item = array_merge($item, $value);
31
                    $valueMerged = true;
32
33
                    break;
34
                }
35 16
                else if ($mergedKey == $key)
36 16
                {
37 15
                    if (is_numeric($mergedKey))
38 15
                    {
39
                        $merged[] = $value;
40
                    }
41 15
                    else if (is_array($item))
42 15
                    {
43 15
                        $item = array_unique(array_merge($item, $value));
44 15
                    }
45
                    else
46
                    {
47 15
                        $item = $value;
48
                    }
49
50 15
                    $valueMerged = true;
51
52 15
                    break;
53
                }
54 16
            }
55
56 16
            if (!$valueMerged)
57 16
            {
58 16
                $merged[$key] = $value;
59 16
            }
60 16
        }
61
62 16
        return $merged;
63
    }
64
}