Conditions | 28 |
Paths | 3042 |
Total Lines | 234 |
Lines | 44 |
Ratio | 18.8 % |
Changes | 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 |
||
88 | function list_blockinstances() |
||
89 | { |
||
90 | global $query4redirect, $block_arr, $xoopsGTicket; |
||
91 | |||
92 | $myts = MyTextSanitizer::getInstance(); |
||
93 | |||
94 | // cachetime options |
||
95 | $cachetimes = array( |
||
96 | '0' => _NOCACHE, |
||
97 | '30' => sprintf(_SECONDS, 30), |
||
98 | '60' => _MINUTE, |
||
99 | '300' => sprintf(_MINUTES, 5), |
||
100 | '1800' => sprintf(_MINUTES, 30), |
||
101 | '3600' => _HOUR, |
||
102 | '18000' => sprintf(_HOURS, 5), |
||
103 | '86400' => _DAY, |
||
104 | '259200' => sprintf(_DAYS, 3), |
||
105 | '604800' => _WEEK, |
||
106 | '2592000' => _MONTH |
||
107 | ); |
||
108 | |||
109 | // displaying TH |
||
110 | echo " |
||
111 | <form class='apcalForm' action='admin.php' name='blockadmin' method='post'> |
||
112 | <table width='95%' class='outer' cellpadding='4' cellspacing='1'> |
||
113 | <tr valign='middle'> |
||
114 | <th>" . _AM_APCAL_TITLE . "</th> |
||
115 | <th align='center' nowrap='nowrap'>" . _AM_APCAL_SIDE . "</th> |
||
116 | <th align='center'>" . _AM_APCAL_WEIGHT . "</th> |
||
117 | <th align='center'>" . _AM_APCAL_VISIBLEIN . "</th> |
||
118 | <th align='center'>" . _AM_APCAL_BCACHETIME . "</th> |
||
119 | <th align='right'>" . _AM_APCAL_ACTION . "</th> |
||
120 | </tr>\n"; |
||
121 | |||
122 | // get block instances |
||
123 | $crit = new Criteria('bid', '(' . implode(',', array_keys($block_arr)) . ')', 'IN'); |
||
124 | $criteria = new CriteriaCompo($crit); |
||
125 | $criteria->setSort('visible DESC, side ASC, weight'); |
||
126 | $instanceHandler = xoops_getHandler('blockinstance'); |
||
127 | $instances = $instanceHandler->getObjects($criteria, true, true); |
||
128 | |||
129 | //Get modules and pages for visible in |
||
130 | $module_list[_AM_APCAL_SYSTEMLEVEL]['0-2'] = _AM_APCAL_ADMINBLOCK; |
||
131 | $module_list[_AM_APCAL_SYSTEMLEVEL]['0-1'] = _AM_APCAL_TOPPAGE; |
||
132 | $module_list[_AM_APCAL_SYSTEMLEVEL]['0-0'] = _AM_APCAL_ALLPAGES; |
||
133 | $criteria = new CriteriaCompo(new Criteria('hasmain', 1)); |
||
134 | $criteria->add(new Criteria('isactive', 1)); |
||
135 | /** @var XoopsModuleHandler $moduleHandler */ |
||
136 | $moduleHandler = xoops_getHandler('module'); |
||
137 | $module_main = $moduleHandler->getObjects($criteria, true); |
||
138 | if (count($module_main) > 0) { |
||
139 | foreach (array_keys($module_main) as $mid) { |
||
140 | $module_list[$module_main[$mid]->getVar('name')][$mid . '-0'] = _AM_APCAL_ALLMODULEPAGES; |
||
141 | $pages = $module_main[$mid]->getInfo('pages'); |
||
142 | if ($pages === false) { |
||
143 | $pages = $module_main[$mid]->getInfo('sub'); |
||
144 | } |
||
145 | if (is_array($pages) && $pages != array()) { |
||
146 | foreach ($pages as $id => $pageinfo) { |
||
147 | $module_list[$module_main[$mid]->getVar('name')][$mid . '-' . $id] = $pageinfo['name']; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | // blocks displaying loop |
||
154 | $class = 'even'; |
||
155 | $block_configs = get_block_configs(); |
||
156 | foreach (array_keys($instances) as $i) { |
||
157 | $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = ''; |
||
158 | $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = '#FFFFFF'; |
||
159 | |||
160 | $weight = $instances[$i]->getVar('weight'); |
||
161 | $title = $instances[$i]->getVar('title'); |
||
162 | $bcachetime = $instances[$i]->getVar('bcachetime'); |
||
163 | $bid = $instances[$i]->getVar('bid'); |
||
164 | $name = $myts->htmlSpecialChars($block_arr[$bid]['name']); |
||
165 | |||
166 | $visiblein = $instances[$i]->getVisibleIn(); |
||
167 | |||
168 | // visible and side |
||
169 | View Code Duplication | if ($instances[$i]->getVar('visible') != 1) { |
|
170 | $sseln = ' checked'; |
||
171 | $scoln = '#FF0000'; |
||
172 | } else { |
||
173 | switch ($instances[$i]->getVar('side')) { |
||
174 | default: |
||
175 | case XOOPS_SIDEBLOCK_LEFT: |
||
176 | $ssel0 = ' checked'; |
||
177 | $scol0 = '#00FF00'; |
||
178 | break; |
||
179 | case XOOPS_SIDEBLOCK_RIGHT: |
||
180 | $ssel1 = ' checked'; |
||
181 | $scol1 = '#00FF00'; |
||
182 | break; |
||
183 | case XOOPS_CENTERBLOCK_LEFT: |
||
184 | $ssel2 = ' checked'; |
||
185 | $scol2 = '#00FF00'; |
||
186 | break; |
||
187 | case XOOPS_CENTERBLOCK_RIGHT: |
||
188 | $ssel4 = ' checked'; |
||
189 | $scol4 = '#00FF00'; |
||
190 | break; |
||
191 | case XOOPS_CENTERBLOCK_CENTER: |
||
192 | $ssel3 = ' checked'; |
||
193 | $scol3 = '#00FF00'; |
||
194 | break; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | // bcachetime |
||
199 | $cachetime_options = ''; |
||
200 | View Code Duplication | foreach ($cachetimes as $cachetime => $cachetime_name) { |
|
201 | if ($bcachetime == $cachetime) { |
||
202 | $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n"; |
||
203 | } else { |
||
204 | $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n"; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | $module_options = ''; |
||
209 | foreach ($module_list as $mname => $module) { |
||
210 | $module_options .= "<optgroup label='$mname'>\n"; |
||
211 | foreach ($module as $mkey => $mval) { |
||
212 | if (in_array($mkey, $visiblein)) { |
||
213 | $module_options .= "<option value='$mkey' selected>$mval</option>\n"; |
||
214 | } else { |
||
215 | $module_options .= "<option label='$mval' value='$mkey'>$mval</option>\n"; |
||
216 | } |
||
217 | } |
||
218 | $module_options .= "</optgroup>\n"; |
||
219 | } |
||
220 | |||
221 | // delete link if it is cloned block |
||
222 | $delete_link = "<br><a href='" . XOOPS_URL . "/modules/system/admin.php?fct=blocksadmin&op=delete&id=$i&selmod=$mid'>" . _DELETE . '</a>'; |
||
223 | |||
224 | // displaying part |
||
225 | echo " |
||
226 | <tr valign='middle'> |
||
227 | <td class='$class'> |
||
228 | $name |
||
229 | <br> |
||
230 | <input type='text' name='title[$i]' value='$title' size='20' /> |
||
231 | </td> |
||
232 | <td class='$class' align='center' nowrap='nowrap' width='125px'> |
||
233 | <div style='float:left;background-color:$scol0;'> |
||
234 | <input type='radio' name='side[$i]' value='" . XOOPS_SIDEBLOCK_LEFT . "' style='background-color:$scol0;' $ssel0 /> |
||
235 | </div> |
||
236 | <div style='float:left;'>-</div> |
||
237 | <div style='float:left;background-color:$scol2;'> |
||
238 | <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_LEFT . "' style='background-color:$scol2;' $ssel2 /> |
||
239 | </div> |
||
240 | <div style='float:left;background-color:$scol3;'> |
||
241 | <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_CENTER . "' style='background-color:$scol3;' $ssel3 /> |
||
242 | </div> |
||
243 | <div style='float:left;background-color:$scol4;'> |
||
244 | <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_RIGHT . "' style='background-color:$scol4;' $ssel4 /> |
||
245 | </div> |
||
246 | <div style='float:left;'>-</div> |
||
247 | <div style='float:left;background-color:$scol1;'> |
||
248 | <input type='radio' name='side[$i]' value='" . XOOPS_SIDEBLOCK_RIGHT . "' style='background-color:$scol1;' $ssel1 /> |
||
249 | </div> |
||
250 | <br> |
||
251 | <br> |
||
252 | <div style='float:left;width:40px;'> </div> |
||
253 | <div style='float:left;background-color:$scoln;'> |
||
254 | <input type='radio' name='side[$i]' value='-1' style='background-color:$scoln;' $sseln /> |
||
255 | </div> |
||
256 | <div style='float:left;'>" . _NONE . "</div> |
||
257 | </td> |
||
258 | <td class='$class' align='center'> |
||
259 | <input type='text' name=weight[$i] value='$weight' size='3' maxlength='5' style='text-align:right;' /> |
||
260 | </td> |
||
261 | <td class='$class' align='center'> |
||
262 | <select name='bmodule[$i][]' size='5' multiple='multiple'> |
||
263 | $module_options |
||
264 | </select> |
||
265 | </td> |
||
266 | <td class='$class' align='center'> |
||
267 | <select name='bcachetime[$i]' size='1'> |
||
268 | $cachetime_options |
||
269 | </select> |
||
270 | </td> |
||
271 | <td class='$class' align='right'> |
||
272 | <a href='" . XOOPS_URL . "/modules/system/admin.php?fct=blocksadmin&op=edit&id=$i'>" . _EDIT . "</a>{$delete_link} |
||
273 | <input type='hidden' name='id[$i]' value='$i' /> |
||
274 | </td> |
||
275 | </tr>\n"; |
||
276 | |||
277 | $class = ($class === 'even') ? 'odd' : 'even'; |
||
278 | } |
||
279 | |||
280 | // list block classes for add (not instances) |
||
281 | foreach ($block_arr as $bid => $block) { |
||
282 | $description4show = ''; |
||
283 | View Code Duplication | foreach ($block_configs as $bconf) { |
|
284 | if ($block['show_func'] == $bconf['show_func'] && $block['func_file'] == $bconf['file'] |
||
285 | && (empty($bconf['template']) || $block['template'] == $bconf['template']) |
||
286 | ) { |
||
287 | if (!empty($bconf['description'])) { |
||
288 | $description4show = $myts->htmlSpecialChars($bconf['description']); |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | |||
293 | echo " |
||
294 | <tr> |
||
295 | <td class='$class' align='left'> |
||
296 | " . $myts->htmlSpecialChars($block['name']) . " |
||
297 | </td> |
||
298 | <td class='$class' align='left' colspan='4'> |
||
299 | $description4show |
||
300 | </td> |
||
301 | <td class='$class' align='center'> |
||
302 | <input type='submit' name='addblock[$bid]' value='" . _ADD . "' /> |
||
303 | </td> |
||
304 | </tr> |
||
305 | \n"; |
||
306 | $class = ($class === 'even') ? 'odd' : 'even'; |
||
307 | } |
||
308 | |||
309 | echo " |
||
310 | <tr> |
||
311 | <td class='foot' align='center' colspan='6'> |
||
312 | <input type='hidden' name='query4redirect' value='$query4redirect' /> |
||
313 | <input type='hidden' name='fct' value='blocksadmin' /> |
||
314 | <input type='hidden' name='op' value='order2' /> |
||
315 | " . $xoopsGTicket->getTicketHtml(__LINE__, 1800, 'myblocksadmin') . " |
||
316 | <input type='submit' name='submit' value='" . _SUBMIT . "' /> |
||
317 | </td> |
||
318 | </tr> |
||
319 | </table> |
||
320 | </form>\n"; |
||
321 | } |
||
322 | |||
373 |
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.