Completed
Push — master ( 51d4a3...e0bbf9 )
by Pol
01:46
created

ArrayUtils::ensureArray()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10.0107

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 20
cts 21
cp 0.9524
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 1
crap 10.0107

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 drupol\htmltag;
4
5
/**
6
 * Class ArrayUtils
7
 */
8
class ArrayUtils
9
{
10
    /**
11
     * Normalize a value.
12
     *
13
     * @param mixed $values
14
     *  The value to normalize.
15
     *
16
     * @return array
17
     *   The value normalized.
18
     */
19 29
    public static function normalizeValue($values)
20
    {
21
        /** @var Callable $callable */
22 29
        $callable = 'self::ensureArray';
23
24 29
        $values = array_map(
25 29
            $callable,
26 29
            self::ensureFlatArray((array) $values)
27
        );
28
29 29
        return array_reduce(
30 29
            $values,
31
            function ($carry, $item) {
32 24
                return array_merge($carry, $item);
33 29
            },
34 29
            []
35
        );
36
    }
37
38
    /**
39
     * Todo.
40
     *
41
     * @param array $arr
42
     *   Todo.
43
     *
44
     * @return mixed[]
45
     *   Todo.
46
     */
47 29
    public static function ensureFlatArray(array $arr)
48
    {
49 29
        return array_reduce(
50 29
            $arr,
51
            function ($c, $a) {
52 24
                return is_array($a) ?
53 3
                    array_merge($c, self::ensureFlatArray($a)) :
54 24
                    array_merge($c, [$a]);
55 29
            },
56 29
            []
57
        );
58
    }
59
60
    /**
61
     * Todo.
62
     *
63
     * @param mixed $value
64
     *   Todo.
65
     *
66
     * @return array
67
     *   Todo.
68
     */
69 24
    public static function ensureArray($value)
70
    {
71 24
        switch (gettype($value)) {
72 24
            case 'string':
73 23
                $return = explode(' ', $value);
74 23
                break;
75
76 8
            case 'array':
77 8
            case 'double':
78 8
            case 'integer':
79 2
                $return = array((string) $value);
80 2
                break;
81 8
            case 'object':
82 1
                $return = array();
83 1
                if (method_exists($value, '__toString')) {
84
                    $return = array((string) $value);
85
                }
86 1
                break;
87 8
            case 'boolean':
88 8
            case 'resource':
89 8
            case 'NULL':
90
            default:
91 8
                $return = array();
92 8
                break;
93
        }
94
95 24
        return $return;
96
    }
97
}
98