Conditions | 35 |
Paths | 374 |
Total Lines | 149 |
Code Lines | 121 |
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_checkboxes($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 = 'checkbox'; |
||
56 | $values = null; |
||
57 | $options = null; |
||
58 | $selected = array(); |
||
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 'escape': |
||
72 | case 'labels': |
||
73 | case 'label_ids': |
||
74 | $$_key = (bool)$_val; |
||
75 | break; |
||
76 | case 'options': |
||
77 | $$_key = (array)$_val; |
||
78 | break; |
||
79 | case 'values': |
||
80 | case 'output': |
||
81 | $$_key = array_values((array)$_val); |
||
82 | break; |
||
83 | case 'checked': |
||
84 | case 'selected': |
||
85 | if (is_array($_val)) { |
||
86 | $selected = array(); |
||
87 | foreach ($_val as $_sel) { |
||
88 | if (is_object($_sel)) { |
||
89 | if (method_exists($_sel, '__toString')) { |
||
90 | $_sel = smarty_function_escape_special_chars((string)$_sel->__toString()); |
||
91 | } else { |
||
92 | trigger_error( |
||
93 | 'html_checkboxes: selected attribute contains an object of class \'' . |
||
94 | get_class($_sel) . '\' without __toString() method', |
||
95 | E_USER_NOTICE |
||
96 | ); |
||
97 | continue; |
||
98 | } |
||
99 | } else { |
||
100 | $_sel = smarty_function_escape_special_chars((string)$_sel); |
||
101 | } |
||
102 | $selected[ $_sel ] = true; |
||
103 | } |
||
104 | } elseif (is_object($_val)) { |
||
105 | if (method_exists($_val, '__toString')) { |
||
106 | $selected = smarty_function_escape_special_chars((string)$_val->__toString()); |
||
107 | } else { |
||
108 | trigger_error( |
||
109 | 'html_checkboxes: selected attribute is an object of class \'' . get_class($_val) . |
||
110 | '\' without __toString() method', |
||
111 | E_USER_NOTICE |
||
112 | ); |
||
113 | } |
||
114 | } else { |
||
115 | $selected = smarty_function_escape_special_chars((string)$_val); |
||
116 | } |
||
117 | break; |
||
118 | case 'checkboxes': |
||
119 | trigger_error( |
||
120 | 'html_checkboxes: the use of the "checkboxes" attribute is deprecated, use "options" instead', |
||
121 | E_USER_WARNING |
||
122 | ); |
||
123 | $options = (array)$_val; |
||
124 | break; |
||
125 | case 'assign': |
||
126 | break; |
||
127 | case 'strict': |
||
128 | break; |
||
129 | case 'disabled': |
||
130 | case 'readonly': |
||
131 | if (!empty($params[ 'strict' ])) { |
||
132 | if (!is_scalar($_val)) { |
||
133 | trigger_error( |
||
134 | "html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute", |
||
135 | E_USER_NOTICE |
||
136 | ); |
||
137 | } |
||
138 | if ($_val === true || $_val === $_key) { |
||
139 | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"'; |
||
140 | } |
||
141 | break; |
||
142 | } |
||
143 | // omit break; to fall through! |
||
144 | // no break |
||
145 | default: |
||
146 | if (!is_array($_val)) { |
||
147 | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; |
||
148 | } else { |
||
149 | trigger_error("html_checkboxes: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE); |
||
150 | } |
||
151 | break; |
||
152 | } |
||
153 | } |
||
154 | if (!isset($options) && !isset($values)) { |
||
155 | return ''; |
||
156 | } /* raise error here? */ |
||
157 | $_html_result = array(); |
||
158 | if (isset($options)) { |
||
159 | foreach ($options as $_key => $_val) { |
||
160 | $_html_result[] = |
||
161 | smarty_function_html_checkboxes_output( |
||
162 | $name, |
||
163 | $_key, |
||
164 | $_val, |
||
165 | $selected, |
||
166 | $extra, |
||
167 | $separator, |
||
168 | $labels, |
||
169 | $label_ids, |
||
170 | $escape |
||
171 | ); |
||
172 | } |
||
173 | } else { |
||
174 | foreach ($values as $_i => $_key) { |
||
175 | $_val = isset($output[ $_i ]) ? $output[ $_i ] : ''; |
||
176 | $_html_result[] = |
||
177 | smarty_function_html_checkboxes_output( |
||
178 | $name, |
||
179 | $_key, |
||
180 | $_val, |
||
181 | $selected, |
||
182 | $extra, |
||
183 | $separator, |
||
184 | $labels, |
||
185 | $label_ids, |
||
186 | $escape |
||
187 | ); |
||
188 | } |
||
189 | } |
||
190 | if (!empty($params[ 'assign' ])) { |
||
191 | $template->assign($params[ 'assign' ], $_html_result); |
||
192 | } else { |
||
193 | return implode("\n", $_html_result); |
||
194 | } |
||
287 |