Completed
Pull Request — master (#41)
by Vladimir
04:25
created

ArrayUtilities::is_multidimensional()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Utilities;
9
10
abstract class ArrayUtilities
11
{
12 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...
13
    {
14 4
        foreach ($array as $element)
15
        {
16 4
            if (is_array($element))
17 4
            {
18 1
                return true;
19
            }
20 3
        }
21
22 3
        return false;
23
    }
24
25 22
    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...
26
    {
27 22
        $merged = $array1;
28
29 22
        foreach ($array2 as $key => &$value)
30
        {
31 17
            $valueMerged = false;
32
33 17
            foreach ($merged as $mergedKey => &$item)
34
            {
35 17
                if (is_array($item) && array_key_exists($keyField, $item) && $item[$keyField] == $value[$keyField])
36 17
                {
37
                    $item = array_merge($item, $value);
38
                    $valueMerged = true;
39
40
                    break;
41
                }
42 17
                elseif ($mergedKey == $key)
43
                {
44 17
                    if (is_numeric($mergedKey))
45 17
                    {
46
                        $merged[] = $value;
47
                    }
48 17
                    elseif (is_array($item))
49
                    {
50 17
                        $item = array_unique(array_merge($item, $value));
51 17
                    }
52
                    else
53
                    {
54 17
                        $item = $value;
55
                    }
56
57 17
                    $valueMerged = true;
58
59 17
                    break;
60
                }
61 17
            }
62
63 17
            if (!$valueMerged)
64 17
            {
65 17
                $merged[$key] = $value;
66 17
            }
67 22
        }
68
69 22
        return $merged;
70
    }
71
}
72