Complex classes like ExtcalUtilities often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExtcalUtilities, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ExtcalUtilities |
||
24 | { |
||
25 | /** |
||
26 | * @param $eventId |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function extcal_getEvent($eventId) |
||
31 | { |
||
32 | $eventHandler = xoops_getModuleHandler(_EXTCAL_CLS_EVENT, _EXTCAL_MODULE); |
||
33 | $event = $eventHandler->getEvent($eventId); |
||
34 | $t = $event->getVars(); |
||
35 | $data = array(); |
||
36 | while (list($key, $val) = each($t)) { |
||
37 | $data[$key] = $val['value']; |
||
38 | } |
||
39 | |||
40 | return $data; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @param $REQUEST |
||
45 | * @param $event_picture1 |
||
46 | * @param $event_picture2 |
||
47 | */ |
||
48 | public static function extcal_loadImg(&$REQUEST, &$event_picture1, &$event_picture2) |
||
49 | { |
||
50 | /////////////////////////////////////////////////////////////////////////////// |
||
51 | $uploaddir_event = XOOPS_ROOT_PATH . '/uploads/extcal/'; |
||
52 | $uploadurl_event = XOOPS_URL . '/uploads/extcal/'; |
||
53 | //$picture = ''; |
||
54 | for ($j = 1; $j < 3; ++$j) { |
||
55 | $delimg = @$REQUEST['delimg_' . $j . '']; |
||
56 | $delimg = isset($delimg) ? (int)$delimg : 0; |
||
57 | if (0 == $delimg && !empty($REQUEST['xoops_upload_file'][$j])) { |
||
58 | $upload = new XoopsMediaUploader($uploaddir_event, array( |
||
59 | 'image/gif', |
||
60 | 'image/jpeg', |
||
61 | 'image/pjpeg', |
||
62 | 'image/x-png', |
||
63 | 'image/png', |
||
64 | 'image/jpg', |
||
65 | ), 3145728, null, null); |
||
66 | if ($upload->fetchMedia($REQUEST['xoops_upload_file'][$j])) { |
||
67 | $upload->setPrefix('event_'); |
||
68 | $upload->fetchMedia($REQUEST['xoops_upload_file'][$j]); |
||
69 | if (!$upload->upload()) { |
||
70 | $errors = $upload->getErrors(); |
||
71 | redirect_header('javascript:history.go(-1)', 3, $errors); |
||
72 | } else { |
||
73 | if ($j == 1) { |
||
74 | $event_picture1 = $upload->getSavedFileName(); |
||
75 | } elseif ($j == 2) { |
||
76 | $event_picture2 = $upload->getSavedFileName(); |
||
77 | } |
||
78 | } |
||
79 | } elseif (!empty($REQUEST['file' . $j])) { |
||
80 | if ($j == 1) { |
||
81 | $event_picture1 = $REQUEST['file' . $j]; |
||
82 | } elseif ($j == 2) { |
||
83 | $event_picture2 = $REQUEST['file' . $j]; |
||
84 | } |
||
85 | } |
||
86 | } else { |
||
87 | $url_event = XOOPS_ROOT_PATH . '/uploads/extcal/' . $REQUEST['file' . $j]; |
||
88 | if ($j == 1) { |
||
89 | $event_picture1 = ''; |
||
90 | } elseif ($j == 2) { |
||
91 | $event_picture2 = ''; |
||
92 | } |
||
93 | if (is_file($url_event)) { |
||
94 | chmod($url_event, 0777); |
||
95 | unlink($url_event); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | //exit; |
||
100 | /////////////////////////////////////////////////////////////////////////////// |
||
101 | } |
||
102 | |||
103 | /******************************************************************* |
||
104 | * |
||
105 | ****************************************************************** |
||
106 | * @param $cat |
||
107 | * @param bool $addNone |
||
108 | * @param string $name |
||
109 | * @return XoopsFormSelect |
||
110 | */ |
||
111 | public static function getListCategories($cat, $addNone = true, $name = 'cat') |
||
112 | { |
||
113 | global $xoopsUser; |
||
114 | // Category selectbox |
||
115 | $catHandler = xoops_getModuleHandler(_EXTCAL_CLS_CAT, _EXTCAL_MODULE); |
||
116 | |||
117 | $catsList = $catHandler->getAllCat($xoopsUser); |
||
118 | $catSelect = new XoopsFormSelect('', $name, $cat); |
||
119 | if ($addNone) { |
||
120 | $catSelect->addOption(0, ' '); |
||
121 | } |
||
122 | |||
123 | foreach ($catsList as $catList) { |
||
124 | $catSelect->addOption($catList->getVar('cat_id'), $catList->getVar('cat_name')); |
||
125 | } |
||
126 | |||
127 | return $catSelect; |
||
128 | } |
||
129 | |||
130 | /******************************************************************* |
||
131 | * |
||
132 | ****************************************************************** |
||
133 | * @param string $name |
||
134 | * @param $cat |
||
135 | * @return array |
||
136 | */ |
||
137 | public static function getCheckeCategories($name = 'cat', $cat) |
||
138 | { |
||
139 | global $xoopsUser; |
||
140 | // Category selectbox |
||
141 | //<option style="background-color:#00FFFF;">VARCHAR</option> |
||
142 | |||
143 | $catHandler = xoops_getModuleHandler(_EXTCAL_CLS_CAT, _EXTCAL_MODULE); |
||
144 | $catsList = $catHandler->getAllCat($xoopsUser); |
||
145 | |||
146 | $t = array(); |
||
147 | foreach ($catsList as $catList) { |
||
148 | $cat_id = $catList->getVar('cat_id'); |
||
149 | $name = $catList->getVar('cat_name'); |
||
150 | $cat_color = $catList->getVar('cat_color'); |
||
151 | $checked = in_array($cat_id, $cat) ? 'checked' : ''; |
||
152 | $cat = '' . "<div style='float:left; margin-left:5px;'>" . "<input type='checkbox' name='{$name}[{$cat_id}]' value='1' {$checked}>" |
||
153 | . "<div style='absolute:left;height:12px; width:6px; background-color:#{$cat_color}; border:1px solid black; float:left; margin-right:5px;' ></div>" . " {$name}" . '</div>'; |
||
154 | |||
155 | $t[] = $cat; |
||
156 | } |
||
157 | |||
158 | return $t; |
||
159 | } |
||
160 | |||
161 | /******************************************************************* |
||
162 | * |
||
163 | ****************************************************************** |
||
164 | * @param string $name |
||
165 | * @param string $caption |
||
166 | * @param $defaut |
||
167 | * @param bool $addNone |
||
168 | * @return XoopsFormSelect |
||
169 | */ |
||
170 | public static function getListOrderBy($name = 'orderby', $caption = '', $defaut, $addNone = false) |
||
171 | { |
||
172 | global $xoopsUser; |
||
173 | |||
174 | $select = new XoopsFormSelect($caption, $name, $defaut); |
||
175 | if ($addNone) { |
||
176 | $select->addOption('', ''); |
||
177 | } |
||
178 | |||
179 | $select->addOption('year ASC', _MD_EXTCAL_YEAR . ' ' . _MD_EXTCAL_ORDER_BY_ASC); |
||
180 | $select->addOption('year DESC', _MD_EXTCAL_YEAR . ' ' . _MD_EXTCAL_ORDER_BY_DESC); |
||
181 | |||
182 | $select->addOption('month ASC', _MD_EXTCAL_MONTH . ' ' . _MD_EXTCAL_ORDER_BY_ASC); |
||
183 | $select->addOption('month DESC', _MD_EXTCAL_MONTH . ' ' . _MD_EXTCAL_ORDER_BY_DESC); |
||
184 | |||
185 | $select->addOption('event_title ASC', _MD_EXTCAL_ALPHA . ' ' . _MD_EXTCAL_ORDER_BY_ASC); |
||
186 | $select->addOption('event_title DESC', _MD_EXTCAL_ALPHA . ' ' . _MD_EXTCAL_ORDER_BY_DESC); |
||
187 | |||
188 | $select->addOption('cat_name ASC', _MD_EXTCAL_CATEGORY . ' ' . _MD_EXTCAL_ORDER_BY_ASC); |
||
189 | $select->addOption('cat_name DESC', _MD_EXTCAL_CATEGORY . ' ' . _MD_EXTCAL_ORDER_BY_DESC); |
||
190 | |||
191 | return $select; |
||
192 | } |
||
193 | |||
194 | /******************************************************************* |
||
195 | * |
||
196 | ****************************************************************** |
||
197 | * @param string $name |
||
198 | * @param string $caption |
||
199 | * @param $defaut |
||
200 | * @return XoopsFormSelect |
||
201 | */ |
||
202 | public static function getListAndOr($name = 'andor', $caption = '', $defaut) |
||
203 | { |
||
204 | global $xoopsUser; |
||
205 | |||
206 | $select = new XoopsFormSelect($caption, $name, $defaut); |
||
207 | |||
208 | $select->addOption('AND', _MD_EXTCAL_AND); |
||
209 | $select->addOption('OR', _MD_EXTCAL_OR); |
||
210 | |||
211 | return $select; |
||
212 | } |
||
213 | |||
214 | /******************************************************************* |
||
215 | * |
||
216 | ****************************************************************** |
||
217 | * @param $name |
||
218 | * @param $caption |
||
219 | * @param $defaut |
||
220 | * @param $options |
||
221 | * @param string $sep |
||
222 | * @return XoopsFormSelect |
||
223 | */ |
||
224 | public static function getList($name, $caption, $defaut, $options, $sep = ';') |
||
225 | { |
||
226 | global $xoopsUser; |
||
227 | |||
228 | $select = new XoopsFormSelect($caption, $name, $defaut); |
||
229 | if (!is_array($options)) { |
||
230 | $options = explode($sep, $options); |
||
231 | } |
||
232 | |||
233 | for ($h = 0, $count = count($options); $h < $count; ++$h) { |
||
234 | $select->addOption($h, $options[$h]); |
||
235 | } |
||
236 | |||
237 | return $select; |
||
238 | } |
||
239 | |||
240 | /******************************************************************* |
||
241 | * |
||
242 | ****************************************************************** |
||
243 | * @param $ts |
||
244 | * @param $startMonth |
||
245 | * @param $endMonth |
||
246 | * @param string $mode |
||
247 | * @return DateTime |
||
248 | */ |
||
249 | public static function getDateBetweenDates($ts, $startMonth, $endMonth, $mode = 'w') |
||
250 | { |
||
251 | $d = new DateTime($periodStart); |
||
252 | $d->setTimestamp($ts); |
||
253 | |||
254 | //echo "<br>affichage des periodes : <br>"; |
||
255 | $begin = new DateTime(); |
||
256 | $begin->setTimestamp($startMonth); |
||
257 | //echo $begin->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53 |
||
258 | |||
259 | $end = new DateTime(); |
||
260 | $end->setTimestamp($endMonth); |
||
261 | //echo $end->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53 |
||
262 | //echo "<hr>"; |
||
263 | $interval = DateInterval::createFromDateString('next sunday'); |
||
264 | $period = new DatePeriod($begin, $interval, $end); |
||
265 | //echoDateArray($period); |
||
266 | |||
267 | //echo "<hr>{$interval}"; |
||
268 | return $d; |
||
269 | |||
270 | //echo mktime($heure, $minute, $seconde, $mois, $jour, $an); |
||
271 | |||
272 | // |
||
273 | // $jour = date('d', $ts); |
||
274 | // $mois = date('m', $ts); |
||
275 | // $an = date('Y', $ts); |
||
276 | // $heure = date('H', $ts); |
||
277 | // $minute = date('i', $ts); |
||
278 | // $seconde = date('s', $ts); |
||
279 | // $d->setDate($heure,$minute,$seconde,$mois,$jour,$an); |
||
280 | |||
281 | // <?php |
||
282 | // $interval = DateInterval::createFromDateString('next sunday'); |
||
283 | // $period = new DatePeriod($begin, $interval, $end); |
||
284 | // foreach ($period as $dt) { |
||
285 | // echo $dt->format( "l Y-m-d H:i:s\n" ); |
||
286 | } |
||
287 | |||
288 | /* |
||
289 | Sunday 2009-11-01 00:00:00 |
||
290 | Sunday 2009-11-08 00:00:00 |
||
291 | Sunday 2009-11-15 00:00:00 |
||
292 | Sunday 2009-11-22 00:00:00 |
||
293 | Sunday 2009-11-29 00:00:00 |
||
294 | Sunday 2009-12-06 00:00:00 |
||
295 | ... |
||
296 | */ |
||
297 | /** |
||
298 | * @param $period |
||
299 | */ |
||
300 | public static function echoDateArray($period) |
||
306 | |||
307 | /*****************************************************************/ |
||
308 | /** |
||
309 | * @param $t |
||
310 | * @param string $msg |
||
311 | */ |
||
312 | public static function ext_echoArray($t, $msg = '') |
||
313 | { |
||
314 | if ($msg != '') { |
||
315 | echo "<hr>{$msg}<hr>"; |
||
316 | } |
||
317 | |||
318 | $txt = print_r($t, true); |
||
319 | echo '<pre>Number of items: ' . count($t) . "<br>{$txt}</pre>"; |
||
320 | } |
||
321 | |||
322 | /*****************************************************************/ |
||
323 | /** |
||
324 | * @param $line |
||
325 | * @param string $msg |
||
326 | */ |
||
327 | public static function ext_echo($line, $msg = '') |
||
334 | |||
335 | /*****************************************************************/ |
||
336 | /** |
||
337 | * @param $tsName |
||
338 | * @param string $msg |
||
339 | */ |
||
340 | public static function ext_echoTSN($tsName, $msg = '') |
||
346 | |||
347 | /*****************************************************************/ |
||
348 | /** |
||
349 | * @param $ts |
||
350 | * @param $tsName |
||
351 | * @param string $msg |
||
352 | */ |
||
353 | public static function ext_echoTSU($ts, $tsName, $msg = '') |
||
361 | |||
362 | /*****************************************************************/ |
||
363 | /*****************************************************************/ |
||
364 | /** |
||
365 | * @param $date |
||
366 | * @param string $sep |
||
367 | * |
||
368 | * @return int |
||
369 | */ |
||
370 | public static function ext_convert_date($date, $sep = '-') |
||
383 | |||
384 | /** |
||
385 | * @param $givendate |
||
386 | * @param int $day |
||
387 | * @param int $mth |
||
388 | * @param int $yr |
||
389 | * |
||
390 | * @return int |
||
391 | */ |
||
392 | public static function ext_DateAdd($givendate, $day = 0, $mth = 0, $yr = 0) |
||
400 | |||
401 | /** |
||
402 | * @param $date |
||
403 | * @param $number |
||
404 | * @param $interval |
||
405 | * |
||
406 | * @return int |
||
407 | */ |
||
408 | public static function ext_DateAdd2($date, $number, $interval = 'd') |
||
409 | { |
||
410 | $date_time_array = getdate($date); |
||
411 | $hours = $date_time_array['hours']; |
||
412 | $minutes = $date_time_array['minutes']; |
||
413 | $seconds = $date_time_array['seconds']; |
||
414 | $month = $date_time_array['mon']; |
||
415 | $day = $date_time_array['mday']; |
||
416 | $year = $date_time_array['year']; |
||
417 | |||
418 | switch ($interval) { |
||
419 | |||
420 | case 'yyyy': |
||
421 | $year += $number; |
||
422 | break; |
||
423 | case 'q': |
||
424 | $year += ($number * 3); |
||
425 | break; |
||
426 | case 'm': |
||
427 | $month += $number; |
||
428 | break; |
||
429 | case 'y': |
||
430 | case 'd': |
||
431 | case 'w': |
||
432 | $day += $number; |
||
433 | break; |
||
434 | case 'ww': |
||
435 | $day += ($number * 7); |
||
436 | break; |
||
437 | case 'h': |
||
438 | $hours += $number; |
||
439 | break; |
||
440 | case 'n': |
||
441 | $minutes += $number; |
||
442 | break; |
||
443 | case 's': |
||
444 | $seconds += $number; |
||
445 | break; |
||
446 | } |
||
447 | $timestamp = mktime($hours, $minutes, $seconds, $month, $day, $year); |
||
448 | |||
449 | return $timestamp; |
||
450 | } |
||
451 | |||
452 | // function date_diff($date1, $date2) { |
||
453 | // $current = $date1; |
||
454 | // $datetime2 = date_create($date2); |
||
455 | // $count = 0; |
||
456 | // while (date_create($current) < $datetime2) { |
||
457 | // $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); |
||
458 | // ++$count; |
||
459 | // } |
||
460 | // return $count; |
||
461 | // } |
||
462 | |||
463 | /**************************************************************************/ |
||
464 | /** |
||
465 | * @param $color |
||
466 | * @param $plancher |
||
467 | * @param $plafond |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | public static function getLighterColor($color, $plancher, $plafond) |
||
479 | /**************************************************************************/ |
||
480 | |||
481 | /** |
||
482 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
483 | * |
||
484 | * @param string $folder The full path of the directory to check |
||
485 | * |
||
486 | * @return void |
||
487 | */ |
||
488 | public static function createFolder($folder) |
||
502 | |||
503 | /** |
||
504 | * @param $file |
||
505 | * @param $folder |
||
506 | * @return bool |
||
507 | */ |
||
508 | public static function copyFile($file, $folder) |
||
522 | |||
523 | /** |
||
524 | * @param $src |
||
525 | * @param $dst |
||
526 | */ |
||
527 | public static function recurseCopy($src, $dst) |
||
542 | |||
543 | /** |
||
544 | * |
||
545 | * Verifies XOOPS version meets minimum requirements for this module |
||
546 | * @static |
||
547 | * @param XoopsModule $module |
||
548 | * |
||
549 | * @return bool true if meets requirements, false if not |
||
550 | */ |
||
551 | public static function checkXoopsVer(XoopsModule $module) |
||
584 | |||
585 | /** |
||
586 | * |
||
587 | * Verifies PHP version meets minimum requirements for this module |
||
588 | * @static |
||
589 | * @param XoopsModule $module |
||
590 | * |
||
591 | * @return bool true if meets requirements, false if not |
||
592 | */ |
||
593 | public static function checkPhpVer(XoopsModule $module) |
||
609 | } |
||
610 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.