Test Failed
Push — master ( f2bdc0...e2cac0 )
by Chris
29:08
created

ElementConstructorTrait::parseAttributesReal()   C

Complexity

Conditions 16
Paths 12

Size

Total Lines 58
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 28
nc 12
nop 2
dl 0
loc 58
rs 5.5666
c 0
b 0
f 0

How to fix   Long Method    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 WebTheory\Html\Traits;
4
5
use WebTheory\Html\Contracts\HtmlAttributeInterface;
6
use WebTheory\Html\TagSage;
7
8
/**
9
 *
10
 */
11
trait ElementConstructorTrait
12
{
13
    /**
14
     *
15
     */
16
    protected static function parseAttributes(array $attributes): string
17
    {
18
        return static::parseAttributesRealSwitch($attributes);
19
    }
20
21
    /**
22
     *
23
     */
24
    private static function parseAttributesReal(array $attrArr, string &$attrStr = ''): string
25
    {
26
        foreach ($attrArr as $attr => $val) {
27
28
            // don't add empty strings or null values
29
            if ('' === $val && 'value' !== $attr || null === $val) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ('' === $val && 'value' ...$attr) || null === $val, Probably Intended Meaning: '' === $val && ('value' ...$attr || null === $val)
Loading history...
30
                continue;
31
            }
32
33
            // simple attribute
34
            if (is_string($val) || is_numeric($val)) {
35
                $attrStr .= static::renderAttribute($attr, $val);
36
                continue;
37
            }
38
39
            // support interface for defining custom parsing schemes
40
            if ($val instanceof HtmlAttributeInterface) {
41
                $attrStr .= static::renderAttribute($attr, $val->parse());
42
                continue;
43
            }
44
45
            // boolean attribute
46
            if ($val === true) {
47
                $attrStr .= static::renderAttribute($attr, $attr);
48
                continue;
49
            }
50
51
            // treat numerical keys as boolean values
52
            if (is_int($attr)) {
53
                $attrStr .= static::renderAttribute($val, $val);
54
                continue;
55
            }
56
57
            // support for passing an array of boolean values
58
            if ('@boolean' === $attr) {
59
                foreach ((array) $val as $bool) {
60
                    $attrStr .= static::renderAttribute($bool, $bool);
61
                }
62
                continue;
63
            }
64
65
            // support for converting indexed array to DOMTokenList
66
            if (is_array($val) && isset($val[0])) {
67
                $val = implode(' ', array_filter($val));
68
                $attrStr .= static::renderAttribute($attr, $val);
69
                continue;
70
            }
71
72
            // support for converting associative array to DOMStringMap
73
            if (is_array($val)) {
74
                foreach ($val as $set => $setval) {
75
                    static::parseAttributesReal(["{$attr}-{$set}" => $setval], $attrStr);
76
                }
77
                continue;
78
            }
79
        }
80
81
        return $attrStr;
82
    }
83
84
    /**
85
     *
86
     */
87
    private static function parseAttributesRealSwitch(array $attrArr, string &$attrStr = ''): string
88
    {
89
        foreach ($attrArr as $attr => $val) {
90
91
            switch (true) {
92
                    // don't add empty strings or null values
93
                case ('' === $val && 'value' !== $attr || null === $val):
94
                    break;
95
96
                    // simple attribute
97
                case (is_string($val) || is_numeric($val)):
98
                    $attrStr .= static::renderAttribute($attr, $val);
99
                    break;
100
101
                    // support interface for defining custom parsing schemes
102
                case ($val instanceof HtmlAttributeInterface):
103
                    $attrStr .= static::renderAttribute($attr, $val->parse());
104
                    break;
105
106
                    // boolean attribute
107
                case ($val === true):
108
                    $attrStr .= static::renderAttribute($attr, $attr);
109
                    break;
110
111
                    // treat numerical keys as boolean values
112
                case (is_int($attr)):
113
                    $attrStr .= static::renderAttribute($val, $val);
114
                    break;
115
116
                    // support for passing an array of boolean values
117
                case ('@bool' === $attr):
118
                    foreach ((array) $val as $bool) {
119
                        $attrStr .= static::renderAttribute($bool, $bool);
120
                    }
121
                    break;
122
123
                    // support for converting indexed array to DOMTokenList
124
                case (is_array($val) && isset($val[0])):
125
                    $val = implode(' ', array_filter($val));
126
                    $attrStr .= static::renderAttribute($attr, $val);
127
                    break;
128
129
                    // support for converting associative array to DOMStringMap
130
                case (is_array($val)):
131
                    foreach ($val as $set => $setval) {
132
                        static::parseAttributesRealSwitch(["{$attr}-{$set}" => $setval], $attrStr);
133
                    }
134
                    break;
135
            }
136
        }
137
138
        return $attrStr;
139
    }
140
141
    /**
142
     *
143
     */
144
    protected static function renderAttribute($attribute, $value): string
145
    {
146
        $value = static::escapeAttribute($value);
147
148
        return " {$attribute}=\"{$value}\"";
149
    }
150
151
    /**
152
     *
153
     */
154
    protected static function escapeAttribute($attribute): string
155
    {
156
        return htmlspecialchars($attribute);
157
    }
158
159
    /**
160
     *
161
     */
162
    protected static function tag(string $tag, array $attributes = [], string $inner = ''): string
163
    {
164
        return static::open($tag, $attributes) . static::maybeClose($tag, $inner);
165
    }
166
167
    /**
168
     *
169
     */
170
    protected static function open(string $tag, array $attributes = []): string
171
    {
172
        $attributes = static::maybeParseAttributes($attributes);
173
        $slash = static::maybeAddSlash($tag);
174
175
        return "<{$tag}{$attributes}{$slash}>";
176
    }
177
178
    /**
179
     *
180
     */
181
    protected static function close(string $tag): string
182
    {
183
        return "</{$tag}>";
184
    }
185
186
    /**
187
     *
188
     */
189
    protected static function maybeClose(string $tag, string $inner = ''): string
190
    {
191
        return static::tagIsVoid($tag) ? '' : $inner . static::close($tag);
192
    }
193
194
    /**
195
     *
196
     */
197
    protected static function maybeParseAttributes(array $attributes): string
198
    {
199
        return empty($attributes) ? '' : static::parseAttributes($attributes);
200
    }
201
202
    /**
203
     *
204
     */
205
    protected static function maybeAddSlash(string $tag): string
206
    {
207
        return static::tagIsVoid($tag) ? ' /' : '';
208
    }
209
210
    /**
211
     *
212
     */
213
    protected static function tagIsVoid(string $tag): bool
214
    {
215
        return TagSage::isIt('self_closing', $tag);
216
    }
217
218
    /**
219
     *
220
     */
221
    protected static function indent(int $levels = 0): string
222
    {
223
        return str_repeat('&nbsp;', $levels);
224
    }
225
226
    /**
227
     *
228
     */
229
    protected static function newLine(bool $newLine = false): string
230
    {
231
        return $newLine ? "\n" : '';
232
    }
233
}
234