Completed
Push — master ( 630c64...f14c7d )
by Pol
18:54 queued 02:19
created

AbstractBaseHtmlTagObject::ensureArray()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.0082

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 22
cts 23
cp 0.9565
rs 7.6666
c 0
b 0
f 0
cc 10
nc 17
nop 1
crap 10.0082

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