Passed
Push — master ( 654a40...7259f7 )
by Mathieu
23:53
created

FormItem::select()   C

Complexity

Conditions 12
Paths 20

Size

Total Lines 88
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 12.0208

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 12
eloc 66
c 2
b 0
f 1
nc 20
nop 5
dl 0
loc 88
ccs 54
cts 57
cp 0.9474
crap 12.0208
rs 6.3151

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
declare(strict_types=1);
4
5
namespace Suricate;
6
7
/**
8
 * @property string $id Formitem id attribute
9
 * @SuppressWarnings("StaticAccess")
10
 */
11
class FormItem
12
{
13
    public $objectHtmlProperties = [
14
        'type',
15
        'name',
16
        'id',
17
        'class',
18
        'accept',
19
        'capture',
20
        'value',
21
        'checked',
22
        'rows',
23
        'cols',
24
        'placeholder',
25
        'tabindex',
26
        'accesskey',
27
        'disabled',
28
        'spellcheck',
29
        'events',
30
        'multiple',
31
        'autocomplete',
32
        'autofocus',
33
        'required',
34
        'pattern',
35
        'min',
36
        'minlength',
37
        'step',
38
        'max',
39
        'maxlength',
40
        'src',
41
        'readonly',
42 22
        'data', // Array for data-attributes
43
    ];
44 22
    public $label;
45 22
    public $objectHtmlValues = [];
46
    public static $encoding = 'UTF-8';
47 22
48
    public function __construct($itemData = [])
49 21
    {
50
        foreach ($itemData as $itemProperty => $itemValue) {
51 21
            $this->$itemProperty = $itemValue;
52 21
        }
53
    }
54
55 21
    public function __get($name)
56
    {
57
        if (isset($this->objectHtmlValues[$name])) {
58 22
            return $this->objectHtmlValues[$name];
59
        }
60 22
61 22
        return null;
62
    }
63 22
64
    public function __set($name, $value)
65 1
    {
66
        if (in_array($name, $this->objectHtmlProperties)) {
67 1
            $this->objectHtmlValues[$name] = $value;
68
        }
69
    }
70 19
71
    public function __isset($name)
72
    {
73
        return array_key_exists($name, $this->objectHtmlValues);
74
    }
75
76
    public static function input(
77 19
        $type,
78 19
        $name,
79 19
        $value = null,
80 19
        $label = null,
81 19
        $htmlAttributes = []
82 19
    ) {
83 15
        $itemData = [];
84
        $itemData['type'] = $type;
85 19
        $itemData['name'] = $name;
86
        $itemData['value'] = $value;
87 19
        $itemData['label'] = $label;
88
        if ($label !== null && !isset($htmlAttributes['id'])) {
89 19
            $itemData['id'] = $name;
90 19
        }
91 19
        $itemData = array_merge($itemData, $htmlAttributes);
92 19
93
        $item = new FormItem($itemData);
94 19
95
        $output = $item->renderLabel();
96
        $output .= '<input';
97 1
        $output .= $item->renderAttributes();
98
        $output .= '/>';
99
100
        return $output;
101
    }
102
103 1
    public static function text(
104
        $name,
105
        $value = null,
106 1
        $label = null,
107
        $htmlAttributes = []
108
    ) {
109
        return static::input('text', $name, $value, $label, $htmlAttributes);
110
    }
111
112 1
    public static function password(
113 1
        $name,
114
        $value,
115
        $label = null,
116
        $htmlAttributes = []
117
    ) {
118
        return static::input(
119
            'password',
120
            $name,
121 1
            $value,
122
            $label,
123
            $htmlAttributes
124
        );
125
    }
126
127 1
    public static function number(
128
        $name,
129
        $value,
130 1
        $label = null,
131
        $htmlAttributes = []
132
    ) {
133
        return static::input('number', $name, $value, $label, $htmlAttributes);
134
    }
135
136 1
    public static function button(
137
        $name,
138
        $value,
139 1
        $label = null,
140
        $htmlAttributes = []
141
    ) {
142
        return static::input('button', $name, $value, $label, $htmlAttributes);
143
    }
144
145
    public static function checkbox(
146 1
        $name,
147 1
        $value = 1,
148
        $checked = false,
149
        $label = null,
150 1
        $htmlAttributes = []
151 1
    ) {
152
        if (!isset($htmlAttributes['checked']) && $checked) {
153
            $htmlAttributes['checked'] = 'checked';
154
        }
155
156
        return static::input(
157
            'checkbox',
158
            $name,
159 1
            $value,
160
            $label,
161 1
            $htmlAttributes
162
        );
163
    }
164 1
165
    public static function file($name, $label = null, $htmlAttributes = [])
166 1
    {
167
        return static::input('file', $name, null, $label, $htmlAttributes);
168
    }
169 1
170
    public static function hidden($name, $value, $htmlAttributes = [])
171 1
    {
172
        return static::input('hidden', $name, $value, null, $htmlAttributes);
173 1
    }
174
175
    public static function image($name, $url, $htmlAttributes = [])
176 1
    {
177
        $htmlAttributes['src'] = $url;
178
179
        return static::input('image', $name, null, null, $htmlAttributes);
180
    }
181
182
    public static function radio(
183 1
        $name,
184 1
        $availableValues = [],
185 1
        $value = null,
186 1
        $label = null,
187 1
        $htmlAttributes = []
188
    ) {
189 1
        $itemData = [];
190
        $itemData['name'] = $name;
191 1
        $itemData['value'] = $value;
192 1
        $itemData['label'] = $label;
193 1
        $itemData = array_merge($itemData, $htmlAttributes);
194 1
195 1
        $item = new FormItem($itemData);
196 1
197
        $output = $item->renderLabel();
198
        $output .= '<div class="radio-list">';
199
        foreach ($availableValues as $currentValue => $currentLabel) {
200
            $htmlAttributes = ['id' => $name . '-' . $currentValue];
201 1
            if ($currentValue == $value) {
202 1
                $htmlAttributes['checked'] = 'checked';
203 1
            }
204 1
205 1
            $output .=
206 1
                '<div class="radio-item">' .
207
                FormItem::input(
208 1
                    'radio',
209
                    $name,
210 1
                    $currentValue,
211
                    $currentLabel,
212 1
                    $htmlAttributes
213
                ) .
214
                '</div>';
215 1
        }
216
        $output .= '</div>';
217 1
218
        return $output;
219
    }
220 1
221
    public static function reset($value = null, $htmlAttributes = [])
222
    {
223
        return static::input('reset', null, $value, null, $htmlAttributes);
224
    }
225
226
    public static function select(
227 1
        $name,
228 1
        $availableValues = [],
229 1
        $value = null,
230 1
        $label = null,
231 1
        $htmlAttributes = []
232
    ) {
233 1
        $itemData = [];
234
        $itemData['name'] = $name;
235 1
        $itemData['value'] = $value;
236 1
        $itemData['label'] = $label;
237 1
        if ($label !== null && !isset($htmlAttributes['id'])) {
238 1
            $itemData['id'] = $name;
239 1
        }
240 1
        $itemData = array_merge($itemData, $htmlAttributes);
241
242
        $item = new FormItem($itemData);
243 1
244 1
        $output = $item->renderLabel();
245 1
        $output .= '<select';
246 1
        $output .= $item->renderAttributes(true);
247
        $output .= '>';
248 1
        foreach ($availableValues as $currentKey => $currentOption) {
249 1
            if (is_array($currentOption)) {
250 1
                $output .=
251
                    '<optgroup label="' .
252
                    htmlentities(
253
                        (string) $currentKey,
254
                        ENT_COMPAT,
255 1
                        static::$encoding
256
                    ) .
257
                    '">';
258
                foreach ($currentOption as $subKey => $subOption) {
259 1
                    if (is_array($value)) {
260 1
                        $selected = in_array($subKey, $value)
261 1
                            ? ' selected'
262 1
                            : '';
263
                    } else {
264 1
                        $selected = $subKey == $value ? ' selected' : '';
265 1
                    }
266 1
                    $output .=
267 1
                        '<option value="' .
268 1
                        htmlentities(
269 1
                            (string) $subKey,
270 1
                            ENT_COMPAT,
271
                            static::$encoding
272 1
                        ) .
273
                        '"' .
274 1
                        $selected .
275
                        '>' .
276 1
                        htmlentities(
277 1
                            (string) $subOption,
278 1
                            ENT_COMPAT,
279 1
                            static::$encoding
280
                        ) .
281 1
                        '</option>';
282
                }
283
                $output .= '</optgroup>';
284
            } else {
285 1
                if (is_array($value)) {
286 1
                    $selected = in_array($currentKey, $value)
287 1
                        ? ' selected'
288 1
                        : '';
289
                } else {
290 1
                    $selected = $currentKey == $value ? ' selected' : '';
291 1
                }
292 1
                $output .=
293 1
                    '<option value="' .
294 1
                    htmlentities(
295 1
                        (string) $currentKey,
296 1
                        ENT_COMPAT,
297
                        static::$encoding
298 1
                    ) .
299
                    '"' .
300
                    $selected .
301
                    '>' .
302 1
                    htmlentities(
303
                        (string) $currentOption,
304 1
                        ENT_COMPAT,
305
                        static::$encoding
306
                    ) .
307 1
                    '</option>';
308
            }
309
        }
310
311
        $output .= '</select>';
312
313 1
        return $output;
314
    }
315
316 1
    public static function submit(
317
        $name,
318
        $value,
319
        $label = '',
320
        $htmlAttributes = []
321
    ) {
322 1
        return static::input('submit', $name, $value, $label, $htmlAttributes);
323 1
    }
324
325 1
    public static function textarea(
326 1
        $name,
327 1
        $value,
328
        $label = '',
329 1
        $htmlAttributes = []
330
    ) {
331 1
        $itemData = [];
332
        $itemData['name'] = $name;
333 1
334 1
        $itemData['label'] = $label;
335 1
        if ($label !== null && !isset($htmlAttributes['id'])) {
336 1
            $itemData['id'] = $name;
337 1
        }
338 1
        $itemData = array_merge($itemData, $htmlAttributes);
339
340 1
        $item = new FormItem($itemData);
341
342
        $output = $item->renderLabel();
343 1
        $output .= '<textarea';
344
        $output .= $item->renderAttributes(true);
345 1
        $output .= '>';
346
        $output .= $value;
347
        $output .= '</textarea>';
348 1
349
        return $output;
350 1
    }
351
352
    public static function tel($name, $value, $label = '', $htmlAttributes = [])
353 1
    {
354
        return static::input('tel', $name, $value, $label, $htmlAttributes);
355
    }
356
357
    public static function url($name, $value, $label = '', $htmlAttributes = [])
358
    {
359 1
        return static::input('url', $name, $value, $label, $htmlAttributes);
360
    }
361
362 1
    public static function email(
363
        $name,
364
        $value,
365
        $label = '',
366
        $htmlAttributes = []
367
    ) {
368 1
        return static::input('email', $name, $value, $label, $htmlAttributes);
369
    }
370
371 1
    public static function search(
372
        $name,
373
        $value,
374
        $label = '',
375
        $htmlAttributes = []
376
    ) {
377 1
        return static::input('search', $name, $value, $label, $htmlAttributes);
378
    }
379
380 1
    public static function date(
381
        $name,
382
        $value,
383
        $label = '',
384
        $htmlAttributes = []
385
    ) {
386 1
        return static::input('date', $name, $value, $label, $htmlAttributes);
387 1
    }
388
389
    public static function dateTime(
390
        $name,
391
        $value,
392
        $label = '',
393
        $htmlAttributes = []
394
    ) {
395 1
        return static::input(
396
            'datetime-local',
397
            $name,
398
            $value,
399
            $label,
400
            $htmlAttributes
401 1
        );
402
    }
403
404 21
    public static function time(
405
        $name,
406 21
        $value,
407 21
        $label = '',
408 18
        $htmlAttributes = []
409 18
    ) {
410
        return static::input('time', $name, $value, $label, $htmlAttributes);
411
    }
412 17
413 17
    public static function color(
414
        $name,
415 18
        $value = null,
416 18
        $label = null,
417 18
        $htmlAttributes = []
418
    ) {
419
        return static::input('color', $name, $value, $label, $htmlAttributes);
420 21
    }
421
422
    protected function renderLabel()
423 21
    {
424
        $output = '';
425 21
        if ($this->label != '') {
426 21
            $output .= '<label';
427 21
            if ($this->id != '') {
428 21
                $output .=
429
                    ' for="' .
430
                    htmlentities($this->id, ENT_COMPAT, static::$encoding) .
431 21
                    '"';
432 21
            }
433 21
            $output .= '>';
434 21
            $output .= $this->label;
435 21
            $output .= '</label>';
436 21
        }
437
438 21
        return $output;
439
    }
440
441
    protected function renderAttributes($skipValue = false)
442
    {
443 21
        $output = '';
444
        foreach ($this->objectHtmlProperties as $currentAttribute) {
445
            if ($currentAttribute === 'data' && is_array($this->$currentAttribute)) {
446
                foreach ($this->$currentAttribute as $dataKey => $dataValue) {
447
                    $output .=
448
                    ' data-' .
449
                    $dataKey .
450
                    '="' .
451
                    htmlentities(
452
                        (string) $dataValue,
453
                        ENT_COMPAT,
454
                        static::$encoding
455
                    ) .
456
                    '"';
457
                }
458
                continue;
459
            }
460
            if (!($currentAttribute == 'value' && $skipValue)) {
461
                if ($this->$currentAttribute !== null) {
462
                    $output .=
463
                        ' ' .
464
                        $currentAttribute .
465
                        '="' .
466
                        htmlentities(
467
                            (string) $this->$currentAttribute,
468
                            ENT_COMPAT,
469
                            static::$encoding
470
                        ) .
471
                        '"';
472
                }
473
            }
474
        }
475
476
        return $output;
477
    }
478
}
479