Html   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 306
Duplicated Lines 9.15 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 28
loc 306
c 0
b 0
f 0
wmc 26
lcom 1
cbo 5
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A beginForm() 7 7 1
A endForm() 0 4 1
A imageButton() 0 4 1
A button() 0 4 1
A textArea() 7 7 1
A legend() 0 4 1
A label() 0 6 1
A dropDownList() 0 7 1
D listBox() 0 30 10
A optGroup() 0 15 3
A option() 0 6 1
A arrayToOptions() 0 10 2
A resetButton() 7 7 1
A submitButton() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php /** MicroHtml */
2
3
namespace Micro\Web\Html;
4
5
/**
6
 * Html class file.
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/linpax/microphp-framework
10
 * @copyright Copyright (c) 2013 Oleg Lunegov
11
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
12
 * @package Micro
13
 * @subpackage Web\Html
14
 * @version 1.0
15
 * @since 1.0
16
 */
17
class Html
18
{
19
    use TagTrait, HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait {
20
        TagTrait::openTag insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
21
        TagTrait::closeTag insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
22
        TagTrait::tag insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
23
        TagTrait::br insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
24
        TagTrait::embed insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
25
        TagTrait::heading insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
26
        TagTrait::href insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
27
        TagTrait::image insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
28
        TagTrait::imageMap insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
29
        TagTrait::lists insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
30
        TagTrait::mailto insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
31
        TagTrait::object insteadof HeadTagTrait, TableTagTrait, Html5TagTrait, FieldTagTrait;
32
    }
33
34
35
    /**
36
     * Render begin form tag
37
     *
38
     * @access public
39
     *
40
     * @param  string $action path to URL action
41
     * @param  string $method method of request
42
     * @param  array $attributes attributes tag
43
     *
44
     * @return string
45
     * @static
46
     */
47 View Code Duplication
    public static function beginForm($action, $method = 'POST', array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        return static::openTag('form', array_merge($attributes, [
50
            'action' => $action,
51
            'method' => $method
52
        ]));
53
    }
54
55
    /**
56
     * Render end form tag
57
     *
58
     * @access public
59
     * @return string
60
     * @static
61
     */
62
    public static function endForm()
63
    {
64
        return static::closeTag('form');
65
    }
66
67
68
    /**
69
     * Render image button tag
70
     *
71
     * @access public
72
     *
73
     * @param  string $name image name
74
     * @param  string $file image file path
75
     * @param  array $attributesButton attributes for button
76
     * @param  array $attributesImage attributes for image
77
     *
78
     * @return string
79
     * @static
80
     */
81
    public static function imageButton($name, $file, array $attributesButton = [], array $attributesImage = [])
82
    {
83
        return static::button(static::image($name, $file, $attributesImage), $attributesButton);
84
    }
85
86
    /**
87
     * Render button tag
88
     *
89
     * @access public
90
     *
91
     * @param  string $text text for button
92
     * @param  array $attributes attributes tag
93
     *
94
     * @return string
95
     * @static
96
     */
97
    public static function button($text, array $attributes = [])
98
    {
99
        return static::openTag('button', $attributes).$text.static::closeTag('button');
100
    }
101
102
    /**
103
     * Render textArea tag
104
     *
105
     * @access public
106
     *
107
     * @param  string $name textArea name
108
     * @param  string $text textArea text
109
     * @param  array $attributes attributes tag
110
     *
111
     * @return string
112
     * @static
113
     */
114 View Code Duplication
    public static function textArea($name, $text, array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        return static::openTag('textarea', array_merge($attributes, [
117
            'id' => $name,
118
            'name' => $name
119
        ])).$text.static::closeTag('textarea');
120
    }
121
122
    /**
123
     * Render legend tag
124
     *
125
     * @access public
126
     *
127
     * @param  string $text legend text
128
     * @param  array $attributes attributes tag
129
     *
130
     * @return string
131
     * @static
132
     */
133
    public static function legend($text, array $attributes = [])
134
    {
135
        return static::openTag('legend', $attributes).$text.static::closeTag('legend');
136
    }
137
138
    /**
139
     * Render label tag
140
     *
141
     * @access public
142
     *
143
     * @param string $name label name
144
     * @param string $elemId element ID
145
     * @param array $attributes attributes tag
146
     *
147
     * @return string
148
     * @static
149
     */
150
    public static function label($name, $elemId = '', array $attributes = [])
151
    {
152
        return static::openTag('label', array_merge($attributes, [
153
            'for' => $elemId
154
        ])).$name.static::closeTag('label');
155
    }
156
157
    /**
158
     * Render dropDownList (select tag)
159
     *
160
     * @access public
161
     *
162
     * @param string $name dropDown name
163
     * @param array $options format array(value, text, attributes) OR array(label, options, attributes)
164
     * @param array $attributes attributes tag
165
     *
166
     * @return string
167
     * @static
168
     */
169
    public static function dropDownList($name, array $options = [], array $attributes = [])
170
    {
171
        return static::listBox($name, $options, array_merge($attributes, [
172
            'id' => $name,
173
            'size' => 1
174
        ]));
175
    }
176
177
    /**
178
     * Render listBox (select tag)
179
     *
180
     * @access public
181
     *
182
     * @param string $name listBox name
183
     * @param array $options format array(value, text, attributes) OR array(label, options, attributes)
184
     * @param array $attributes attributes tag
185
     *
186
     * @return string
187
     * @static
188
     */
189
    public static function listBox($name, array $options = [], array $attributes = [])
190
    {
191
        $selected = empty($attributes['selected']) ? null : $attributes['selected'];
192
        unset($attributes['selected']);
193
194
        $attributes['name'] = $name;
195
        $opts = '';
196
197
        foreach ($options AS $option) {
198
            if (!empty($option['label'])) {
199
                $opts .= static::optGroup($option['label'], $option['options'], $option['attributes']);
200
            } else {
201
                $attr = empty($option['attributes']) ? [] : $option['attributes'];
202
                unset($option['attributes']);
203
204
                if (!empty($option['value']) && (string)$option['value'] === (string)$selected) {
205
                    $attr['selected'] = 'selected';
206
                }
207
208
                $text = empty($option['text']) ? '' : $option['text'];
209
                unset($option['text']);
210
211
                $opts .= static::option(!empty($option['value']) ? $option['value'] : '', $text, $attr);
212
            }
213
        }
214
215
        $attributes['name'] .= array_key_exists('multiple', $attributes) ? '[]' : '';
216
217
        return static::openTag('select', $attributes).$opts.static::closeTag('select');
218
    }
219
220
    /**
221
     * Render optGroup tag
222
     *
223
     * @access public
224
     *
225
     * @param string $label label for options group
226
     * @param array $options format array(value, text, attributes) OR array(label, options, attributes)
227
     * @param array $attributes attributes tag
228
     *
229
     * @return string
230
     * @static
231
     */
232
    public static function optGroup($label, array $options = [], array $attributes = [])
233
    {
234
        $opts = '';
235
        foreach ($options AS $option) {
236
            if (!empty($option['label'])) {
237
                $opts .= static::optGroup($option['label'], $option['options'], $option['attributes']);
238
            } else {
239
                $opts .= static::option($option['value'], $option['text'], $option['attributes']);
240
            }
241
        }
242
243
        return static::openTag('optgroup', array_merge($attributes, [
244
            'label' => $label
245
        ])).$opts.static::closeTag('optgroup');
246
    }
247
248
    /**
249
     * Render option tag
250
     *
251
     * @access public
252
     *
253
     * @param string $value option value
254
     * @param string $text label for option
255
     * @param array $attributes attributes tag
256
     *
257
     * @return string
258
     * @static
259
     */
260
    public static function option($value, $text, array $attributes = [])
261
    {
262
        return static::openTag('option', array_merge($attributes, [
263
            'value' =>$value
264
        ])).$text.static::closeTag('option');
265
    }
266
267
    /**
268
     * Converting array to options
269
     *
270
     * @param array $arr Input array
271
     *
272
     * @return array|null Output array
273
     */
274
    public static function arrayToOptions(array $arr = [])
275
    {
276
        $result = [];
277
278
        foreach ($arr AS $n => $m) {
279
            $result[] = ['value' => $n, 'text' => $m];
280
        }
281
282
        return $result;
283
    }
284
285
    /**
286
     * Render reset button tag
287
     *
288
     * @access public
289
     *
290
     * @param  string $label text for label on button
291
     * @param  array $attributes attributes tag
292
     *
293
     * @return string
294
     * @static
295
     */
296 View Code Duplication
    public static function resetButton($label = 'Reset', array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
    {
298
        return static::tag('input', array_merge($attributes, [
299
            'type' => 'reset',
300
            'value' => $label
301
        ]));
302
    }
303
304
    /**
305
     * Render submit button tag
306
     *
307
     * @access public
308
     *
309
     * @param  string $label text for label on button
310
     * @param  array $attributes attributes tag
311
     *
312
     * @return string
313
     * @static
314
     */
315 View Code Duplication
    public static function submitButton($label = 'Submit', array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
316
    {
317
        return static::tag('input', array_merge($attributes, [
318
            'type' => 'submit',
319
            'value' => $label
320
        ]));
321
    }
322
}
323