Completed
Pull Request — master (#42)
by Vladimir
02:39
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 23
CRAP Score 10.5696

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 13
nop 3
dl 0
loc 46
ccs 23
cts 28
cp 0.8214
crap 10.5696
rs 4.983
c 0
b 0
f 0

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
/**
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
0 ignored issues
show
Coding Style introduced by
ArrayUtilities does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
11
{
12 6
    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...
Coding Style introduced by
function is_multidimensional() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
13
    {
14 6
        foreach ($array as $element)
15
        {
16 6
            if (is_array($element))
17 6
            {
18 1
                return true;
19
            }
20 5
        }
21
22 5
        return false;
23
    }
24
25 45
    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...
Coding Style introduced by
function array_merge_defaults() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
26
    {
27 45
        $merged = $array1;
28
29 45
        foreach ($array2 as $key => &$value)
30
        {
31 32
            $valueMerged = false;
32
33 32
            foreach ($merged as $mergedKey => &$item)
34
            {
35 32
                if (is_array($item) && array_key_exists($keyField, $item) && $item[$keyField] == $value[$keyField])
36 32
                {
37
                    $item = array_merge($item, $value);
38
                    $valueMerged = true;
39
40
                    break;
41
                }
42 32
                elseif ($mergedKey == $key)
43
                {
44 32
                    if (is_numeric($mergedKey))
45 32
                    {
46
                        $merged[] = $value;
47
                    }
48 32
                    elseif (is_array($item))
49
                    {
50 32
                        $item = array_unique(array_merge($item, $value));
51 32
                    }
52
                    else
53
                    {
54 32
                        $item = $value;
55
                    }
56
57 32
                    $valueMerged = true;
58
59 32
                    break;
60
                }
61 32
            }
62
63 32
            if (!$valueMerged)
64 32
            {
65 32
                $merged[$key] = $value;
66 32
            }
67 45
        }
68
69 45
        return $merged;
70
    }
71
72
    /**
73
     * @param  string $key
74
     * @param  array  $array
75
     * @param  bool   $considerOffset
76
     *
77
     * @return array
78
     */
79 7
    public static function associative_array_split($key, array &$array, $considerOffset = true)
0 ignored issues
show
Coding Style introduced by
function associative_array_split() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
80
    {
81 7
        $offset = array_search($key, array_keys($array)) + (int)$considerOffset;
82 7
        $result = array();
83
84 7
        $result[0] = array_slice($array, 0 , $offset, true);
85 7
        $result[1] = array_slice($array, $offset, null, true);
86
87 7
        return $result;
88
    }
89
}
90