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

ArrayUtilities   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 60
ccs 31
cts 36
cp 0.8611
rs 10
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 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
}