Conditions | 32 |
Paths | 374 |
Total Lines | 133 |
Code Lines | 108 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
267 |