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

ArrayUtilities   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 25
cts 30
cp 0.8333
wmc 13
lcom 0
cbo 0

2 Methods

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