smarty_function_html_radios()   F
last analyzed

Complexity

Conditions 32
Paths 374

Size

Total Lines 133
Code Lines 108

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 32
eloc 108
c 1
b 0
f 0
nc 374
nop 2
dl 0
loc 133
rs 0.9266

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
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {html_radios} function plugin
10
 * File:       function.html_radios.php
11
 * Type:       function
12
 * Name:       html_radios
13
 * Date:       24.Feb.2003
14
 * Purpose:    Prints out a list of radio input types
15
 * Params:
16
 *
17
 * - name       (optional) - string default "radio"
18
 * - values     (required) - array
19
 * - options    (required) - associative array
20
 * - checked    (optional) - array default not set
21
 * - separator  (optional) - ie <br> or &nbsp;
22
 * - output     (optional) - the output next to each radio button
23
 * - assign     (optional) - assign the output as an array to this variable
24
 * - escape     (optional) - escape the content (not value), defaults to true
25
 *
26
 * Examples:
27
 *
28
 * {html_radios values=$ids output=$names}
29
 * {html_radios values=$ids name='box' separator='<br>' output=$names}
30
 * {html_radios values=$ids checked=$checked separator='<br>' output=$names}
31
 *
32
 * @link    http://smarty.php.net/manual/en/language.function.html.radios.php {html_radios}
33
 *          (Smarty online manual)
34
 * @author  Christopher Kvarme <[email protected]>
35
 * @author  credits to Monte Ohrt <monte at ohrt dot com>
36
 * @version 1.0
37
 *
38
 * @param array                    $params   parameters
39
 * @param Smarty_Internal_Template $template template object
40
 *
41
 * @return string
42
 * @uses    smarty_function_escape_special_chars()
43
 * @throws \SmartyException
44
 */
45
function smarty_function_html_radios($params, Smarty_Internal_Template $template)
46
{
47
    $template->_checkPlugins(
48
        array(
49
            array(
50
                'function' => 'smarty_function_escape_special_chars',
51
                'file'     => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'
52
            )
53
        )
54
    );
55
    $name = 'radio';
56
    $values = null;
57
    $options = null;
58
    $selected = null;
59
    $separator = '';
60
    $escape = true;
61
    $labels = true;
62
    $label_ids = false;
63
    $output = null;
64
    $extra = '';
65
    foreach ($params as $_key => $_val) {
66
        switch ($_key) {
67
            case 'name':
68
            case 'separator':
69
                $$_key = (string)$_val;
70
                break;
71
            case 'checked':
72
            case 'selected':
73
                if (is_array($_val)) {
74
                    trigger_error('html_radios: the "' . $_key . '" attribute cannot be an array', E_USER_WARNING);
75
                } elseif (is_object($_val)) {
76
                    if (method_exists($_val, '__toString')) {
77
                        $selected = smarty_function_escape_special_chars((string)$_val->__toString());
78
                    } else {
79
                        trigger_error(
80
                            'html_radios: selected attribute is an object of class \'' . get_class($_val) .
81
                            '\' without __toString() method',
82
                            E_USER_NOTICE
83
                        );
84
                    }
85
                } else {
86
                    $selected = (string)$_val;
87
                }
88
                break;
89
            case 'escape':
90
            case 'labels':
91
            case 'label_ids':
92
                $$_key = (bool)$_val;
93
                break;
94
            case 'options':
95
                $$_key = (array)$_val;
96
                break;
97
            case 'values':
98
            case 'output':
99
                $$_key = array_values((array)$_val);
100
                break;
101
            case 'radios':
102
                trigger_error(
103
                    'html_radios: the use of the "radios" attribute is deprecated, use "options" instead',
104
                    E_USER_WARNING
105
                );
106
                $options = (array)$_val;
107
                break;
108
            case 'assign':
109
                break;
110
            case 'strict':
111
                break;
112
            case 'disabled':
113
            case 'readonly':
114
                if (!empty($params[ 'strict' ])) {
115
                    if (!is_scalar($_val)) {
116
                        trigger_error(
117
                            "html_options: {$_key} attribute must be a scalar, only boolean true or string '$_key' will actually add the attribute",
118
                            E_USER_NOTICE
119
                        );
120
                    }
121
                    if ($_val === true || $_val === $_key) {
122
                        $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"';
123
                    }
124
                    break;
125
                }
126
            // omit break; to fall through!
127
            // no break
128
            default:
129
                if (!is_array($_val)) {
130
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
131
                } else {
132
                    trigger_error("html_radios: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
133
                }
134
                break;
135
        }
136
    }
137
    if (!isset($options) && !isset($values)) {
138
        /* raise error here? */
139
        return '';
140
    }
141
    $_html_result = array();
142
    if (isset($options)) {
143
        foreach ($options as $_key => $_val) {
144
            $_html_result[] =
145
                smarty_function_html_radios_output(
146
                    $name,
147
                    $_key,
148
                    $_val,
149
                    $selected,
150
                    $extra,
151
                    $separator,
152
                    $labels,
153
                    $label_ids,
154
                    $escape
155
                );
156
        }
157
    } else {
158
        foreach ($values as $_i => $_key) {
159
            $_val = isset($output[ $_i ]) ? $output[ $_i ] : '';
160
            $_html_result[] =
161
                smarty_function_html_radios_output(
162
                    $name,
163
                    $_key,
164
                    $_val,
165
                    $selected,
166
                    $extra,
167
                    $separator,
168
                    $labels,
169
                    $label_ids,
170
                    $escape
171
                );
172
        }
173
    }
174
    if (!empty($params[ 'assign' ])) {
175
        $template->assign($params[ 'assign' ], $_html_result);
176
    } else {
177
        return implode("\n", $_html_result);
178
    }
179
}
180
181
/**
182
 * @param $name
183
 * @param $value
184
 * @param $output
185
 * @param $selected
186
 * @param $extra
187
 * @param $separator
188
 * @param $labels
189
 * @param $label_ids
190
 * @param $escape
191
 *
192
 * @return string
193
 */
194
function smarty_function_html_radios_output(
195
    $name,
196
    $value,
197
    $output,
198
    $selected,
199
    $extra,
200
    $separator,
201
    $labels,
202
    $label_ids,
203
    $escape
204
) {
205
    $_output = '';
206
    if (is_object($value)) {
207
        if (method_exists($value, '__toString')) {
208
            $value = (string)$value->__toString();
209
        } else {
210
            trigger_error(
211
                'html_options: value is an object of class \'' . get_class($value) .
212
                '\' without __toString() method',
213
                E_USER_NOTICE
214
            );
215
            return '';
216
        }
217
    } else {
218
        $value = (string)$value;
219
    }
220
    if (is_object($output)) {
221
        if (method_exists($output, '__toString')) {
222
            $output = (string)$output->__toString();
223
        } else {
224
            trigger_error(
225
                'html_options: output is an object of class \'' . get_class($output) .
226
                '\' without __toString() method',
227
                E_USER_NOTICE
228
            );
229
            return '';
230
        }
231
    } else {
232
        $output = (string)$output;
233
    }
234
    if ($labels) {
235
        if ($label_ids) {
236
            $_id = smarty_function_escape_special_chars(
237
                preg_replace(
238
                    '![^\w\-\.]!' . Smarty::$_UTF8_MODIFIER,
239
                    '_',
240
                    $name . '_' . $value
241
                )
242
            );
243
            $_output .= '<label for="' . $_id . '">';
244
        } else {
245
            $_output .= '<label>';
246
        }
247
    }
248
    $name = smarty_function_escape_special_chars($name);
249
    $value = smarty_function_escape_special_chars($value);
250
    if ($escape) {
251
        $output = smarty_function_escape_special_chars($output);
252
    }
253
    $_output .= '<input type="radio" name="' . $name . '" value="' . $value . '"';
254
    if ($labels && $label_ids) {
255
        $_output .= ' id="' . $_id . '"';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_id does not seem to be defined for all execution paths leading up to this point.
Loading history...
256
    }
257
    if ($value === $selected) {
258
        $_output .= ' checked="checked"';
259
    }
260
    $_output .= $extra . ' />' . $output;
261
    if ($labels) {
262
        $_output .= '</label>';
263
    }
264
    $_output .= $separator;
265
    return $_output;
266
}
267