Completed
Push — master ( 5ac2e6...97d9d4 )
by Pol
01:25
created

AbstractBaseHtmlTagObject::ensureArray()   C

Complexity

Conditions 12
Paths 24

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 12.1769

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 25
cts 28
cp 0.8929
rs 6.9666
c 0
b 0
f 0
cc 12
nc 24
nop 1
crap 12.1769

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 AbstractBaseHtmlTagObject
7
 */
8
abstract class AbstractBaseHtmlTagObject
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 function normalizeValue($values)
20
    {
21 29
        return array_reduce(
22 29
            array_map(
23 29
                array($this, 'ensureArray'),
24 29
                $this->ensureFlatArray((array) $values)
25
            ),
26 29
            'array_merge',
27 29
            []
28
        );
29
    }
30
31
    /**
32
     * Transform a multidimensional array into a flat array.
33
     *
34
     * @param mixed[] $array
35
     *   The input array.
36
     *
37
     * @return mixed[]
38
     *   The array with only one dimension.
39
     */
40 29
    public function ensureFlatArray(array $array)
41
    {
42 29
        return array_reduce(
43 29
            $array,
44
            function ($carry, $item) {
45 24
                return is_array($item) ?
46 4
                    array_merge($carry, $this->ensureFlatArray($item)) :
47 24
                    array_merge($carry, [$item]);
48 29
            },
49 29
            []
50
        );
51
    }
52
53
    /**
54
     * Make sure the value is an array.
55
     *
56
     * @param mixed $value
57
     *   The input value.
58
     *
59
     * @return array
60
     *   The input value in an array.
61
     */
62 24
    public function ensureArray($value)
63
    {
64 24
        $return = null;
65
66 24
        switch (gettype($value)) {
67 24
            case 'string':
68 23
                $return = $value;
69 23
                break;
70 8
            case 'integer':
71 8
            case 'double':
72 2
                $return = (string) $value;
73 2
                break;
74 8
            case 'object':
75 1
                if (method_exists($value, '__toString')) {
76
                    $return = (string) $value;
77
                }
78 1
                break;
79 8
            case 'array':
80 8
            case 'boolean':
81 1
                if (true === $value) {
82 1
                    $return = '';
83
                }
84 1
                break;
85 8
            case 'resource':
86 8
            case 'NULL':
87 8
                $return = null;
88 8
                break;
89
            default:
90
                $return = null;
91
                break;
92
        }
93
94 24
        return $return === null ?
95 8
            []:
96 24
            explode(' ', preg_replace('!\s+!', ' ', trim($return)));
97
    }
98
}
99