Passed
Push — develop ( 1db626...f4c4ee )
by Mathieu
01:41
created

FormItem::select()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.0056

Importance

Changes 0
Metric Value
cc 10
eloc 28
nc 10
nop 5
dl 0
loc 39
ccs 25
cts 26
cp 0.9615
crap 10.0056
rs 7.6666
c 0
b 0
f 0

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 declare(strict_types=1);
2
namespace Suricate;
3
4
/**
5
 * @property string $id Formitem id attribute
6
 * @SuppressWarnings("StaticAccess")
7
 */
8
class FormItem
9
{
10
    public $objectHtmlProperties = [
11
        'type',
12
        'name',
13
        'id',
14
        'class',
15
        'value',
16
        'checked',
17
        'rows',
18
        'cols',
19
        'placeholder',
20
        'tabindex',
21
        'accesskey',
22
        'disabled',
23
        'spellcheck',
24
        'events',
25
        'multiple',
26
        'autocomplete',
27
        'autofocus',
28
        'required',
29
        'pattern',
30
        'min',
31
        'step',
32
        'max',
33
        'src',
34
    ];
35
    public $label;
36
    public $objectHtmlValues            = [];
37
    public static $encoding             = 'UTF-8';
38
39 22
    public function __construct($itemData = [])
40
    {
41 22
        foreach ($itemData as $itemProperty => $itemValue) {
42 22
            $this->$itemProperty = $itemValue;
43
        }
44 22
    }
45
46 21
    public function __get($name)
47
    {
48 21
        if (isset($this->objectHtmlValues[$name])) {
49 21
            return $this->objectHtmlValues[$name];
50
        }
51
52 21
        return null;
53
    }
54
55 22
    public function __set($name, $value)
56
    {
57 22
        if (in_array($name, $this->objectHtmlProperties)) {
58 22
            $this->objectHtmlValues[$name] = $value;
59
        }
60 22
    }
61
62 1
    public function __isset($name)
63
    {
64 1
        return array_key_exists($name, $this->objectHtmlValues);
65
    }
66
67 19
    public static function input($type, $name, $value = null, $label = null, $htmlAttributes = [])
68
    {
69 19
        $itemData           = [];
70 19
        $itemData['type']   = $type;
71 19
        $itemData['name']   = $name;
72 19
        $itemData['value']  = $value;
73 19
        $itemData['label']  = $label;
74 19
        if ($label !== null && !isset($htmlAttributes['id'])) {
75 15
            $itemData['id']     = $name;
76
        }
77 19
        $itemData = array_merge($itemData, $htmlAttributes);
78
79 19
        $item = new FormItem($itemData);
80
81 19
        $output  = $item->renderLabel();
82 19
        $output .= '<input';
83 19
        $output .= $item->renderAttributes();
84 19
        $output .= '/>';
85
86 19
        return $output;
87
    }
88
89 1
    public static function text($name, $value = null, $label = null, $htmlAttributes = [])
90
    {
91 1
        return static::input('text', $name, $value, $label, $htmlAttributes);
92
    }
93
94 1
    public static function password($name, $value, $label = null, $htmlAttributes = [])
95
    {
96 1
        return static::input('password', $name, $value, $label, $htmlAttributes);
97
    }
98
99 1
    public static function number($name, $value, $label = null, $htmlAttributes = [])
100
    {
101 1
        return static::input('number', $name, $value, $label, $htmlAttributes);
102
    }
103
104 1
    public static function button($name, $value, $label = null, $htmlAttributes = [])
105
    {
106 1
        return static::input('button', $name, $value, $label, $htmlAttributes);
107
    }
108
109 1
    public static function checkbox($name, $value = 1, $checked = false, $label = null, $htmlAttributes = [])
110
    {
111 1
        if (!isset($htmlAttributes['checked']) && $checked) {
112 1
            $htmlAttributes['checked'] = 'checked';
113
        }
114
        
115 1
        return static::input('checkbox', $name, $value, $label, $htmlAttributes);
116
    }
117
118 1
    public static function file($name, $label = null, $htmlAttributes = [])
119
    {
120 1
        return static::input('file', $name, null, $label, $htmlAttributes);
121
    }
122
123 1
    public static function hidden($name, $value, $htmlAttributes = [])
124
    {
125 1
        return static::input('hidden', $name, $value, null, $htmlAttributes);
126
    }
127
128 1
    public static function image($name, $url, $htmlAttributes = [])
129
    {
130 1
        $htmlAttributes['src'] = $url;
131
132 1
        return static::input('image', $name, null, null, $htmlAttributes);
133
    }
134
135 1
    public static function radio($name, $availableValues = [], $value = null, $label = null, $htmlAttributes = [])
136
    {
137 1
        $itemData           = [];
138 1
        $itemData['name']   = $name;
139 1
        $itemData['value']  = $value;
140 1
        $itemData['label']  = $label;
141 1
        $itemData = array_merge($itemData, $htmlAttributes);
142
143 1
        $item = new FormItem($itemData);
144
145 1
        $output  = $item->renderLabel();
146 1
        $output .= '<div class="radio-list">';
147 1
        foreach ($availableValues as $currentValue => $currentLabel) {
148 1
            $htmlAttributes = ['id' => $name . '-' . $currentValue];
149 1
            if ($currentValue == $value) {
150 1
                $htmlAttributes['checked'] = 'checked';
151
            }
152
153 1
            $output .= '<div class="radio-item">' . FormItem::input('radio', $name, $currentValue, $currentLabel, $htmlAttributes) . '</div>';
154
        }
155 1
        $output .= '</div>';
156
157 1
        return $output;
158
    }
159
160 1
    public static function reset($value = null, $htmlAttributes = [])
161
    {
162 1
        return static::input('reset', null, $value, null, $htmlAttributes);
163
    }
164
165 1
    public static function select($name, $availableValues = [], $value = null, $label = null, $htmlAttributes = [])
166
    {
167 1
        $itemData           = [];
168 1
        $itemData['name']   = $name;
169 1
        $itemData['value']  = $value;
170 1
        $itemData['label']  = $label;
171 1
        $itemData = array_merge($itemData, $htmlAttributes);
172
173 1
        $item = new FormItem($itemData);
174
175 1
        $output  = $item->renderLabel();
176 1
        $output .= '<select';
177 1
        $output .= $item->renderAttributes(true);
178 1
        $output .= '>';
179 1
        foreach ($availableValues as $currentKey => $currentOption) {
180 1
            if (is_array($currentOption)) {
181 1
                $output .= '<optgroup label="' . htmlentities((string) $currentKey, ENT_COMPAT, static::$encoding) . '">';
182 1
                foreach ($currentOption as $subKey => $subOption) {
183 1
                    if (is_array($value)) {
184
                        $selected = in_array($subKey, $value) ? ' selected' : '';
185
                    } else {
186 1
                        $selected = ($subKey == $value) ? ' selected' : '';
187
                    }
188 1
                    $output .= '<option value="' . htmlentities((string) $subKey, ENT_COMPAT, static::$encoding) . '"' . $selected . '>' . htmlentities((string) $subOption, ENT_COMPAT, static::$encoding) . '</option>';
189
                }
190 1
                $output .= '</optgroup>';
191
            } else {
192 1
                if (is_array($value)) {
193 1
                    $selected = in_array($currentKey, $value) ? ' selected' : '';
194
                } else {
195 1
                    $selected = ($currentKey == $value) ? ' selected' : '';
196
                }
197 1
                $output .= '<option value="' . htmlentities((string) $currentKey, ENT_COMPAT, static::$encoding) . '"' . $selected . '>' . htmlentities((string) $currentOption, ENT_COMPAT, static::$encoding) . '</option>';
198
            }
199
        }
200
201 1
        $output .= '</select>';
202
203 1
        return $output;
204
    }
205
206 1
    public static function submit($name, $value, $label = '', $htmlAttributes = [])
207
    {
208 1
        return static::input('submit', $name, $value, $label, $htmlAttributes);
209
    }
210
211 1
    public static function textarea($name, $value, $label = '', $htmlAttributes = [])
212
    {
213 1
        $itemData           = [];
214 1
        $itemData['name']   = $name;
215
216 1
        $itemData['label']  = $label;
217 1
        if ($label !== null && !isset($htmlAttributes['id'])) {
218 1
            $itemData['id']     = $name;
219
        }
220 1
        $itemData = array_merge($itemData, $htmlAttributes);
221
222 1
        $item = new FormItem($itemData);
223
224 1
        $output  = $item->renderLabel();
225 1
        $output .= '<textarea';
226 1
        $output .= $item->renderAttributes(true);
227 1
        $output .= '>';
228 1
        $output .= $value;
229 1
        $output .= '</textarea>';
230
231 1
        return $output;
232
    }
233
234 1
    public static function tel($name, $value, $label = '', $htmlAttributes = [])
235
    {
236 1
        return static::input('tel', $name, $value, $label, $htmlAttributes);
237
    }
238
239 1
    public static function url($name, $value, $label = '', $htmlAttributes = [])
240
    {
241 1
        return static::input('url', $name, $value, $label, $htmlAttributes);
242
    }
243
244 1
    public static function email($name, $value, $label = '', $htmlAttributes = [])
245
    {
246 1
        return static::input('email', $name, $value, $label, $htmlAttributes);
247
    }
248
249 1
    public static function search($name, $value, $label = '', $htmlAttributes = [])
250
    {
251 1
        return static::input('search', $name, $value, $label, $htmlAttributes);
252
    }
253
254 1
    public static function date($name, $value, $label = '', $htmlAttributes = [])
255
    {
256 1
        return static::input('date', $name, $value, $label, $htmlAttributes);
257
    }
258
259 1
    public static function dateTime($name, $value, $label = '', $htmlAttributes = [])
260
    {
261 1
        return static::input('datetime', $name, $value, $label, $htmlAttributes);
262
    }
263
264 1
    public static function time($name, $value, $label = '', $htmlAttributes = [])
265
    {
266 1
        return static::input('time', $name, $value, $label, $htmlAttributes);
267
    }
268
269 21
    protected function renderLabel()
270
    {
271 21
        $output = '';
272 21
        if ($this->label != '') {
273 18
            $output .= '<label';
274 18
            if ($this->id != '') {
275 17
                $output .= ' for="' . htmlentities($this->id, ENT_COMPAT, static::$encoding) . '"';
276
            }
277 18
            $output .= '>';
278 18
            $output .= $this->label;
279 18
            $output .= '</label>';
280
        }
281
282 21
        return $output;
283
    }
284
285 21
    protected function renderAttributes($skipValue = false)
286
    {
287 21
        $output = '';
288 21
        foreach ($this->objectHtmlProperties as $currentAttribute) {
289 21
            if (!($currentAttribute == 'value' && $skipValue)) {
290 21
                if ($this->$currentAttribute !== null) {
291 21
                    $output .= ' ' . $currentAttribute . '="' . htmlentities((string) $this->$currentAttribute, ENT_COMPAT, static::$encoding) . '"';
292
                }
293
            }
294
        }
295
296 21
        return $output;
297
    }
298
}
299