Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Utility 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 Utility, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Extcal; |
||
27 | class Utility |
||
28 | { |
||
29 | use common\VersionChecks; //checkVerXoops, checkVerPhp Traits |
||
30 | |||
31 | use common\ServerStats; // getServerStats Trait |
||
32 | |||
33 | use common\FilesManagement; // Files Management Trait |
||
34 | |||
35 | //--------------- Custom module methods ----------------------------- |
||
36 | /** |
||
37 | * @param $eventId |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | View Code Duplication | public static function extcal_getEvent($eventId) |
|
54 | |||
55 | /** |
||
56 | * @param $REQUEST |
||
57 | * @param $event_picture1 |
||
58 | * @param $event_picture2 |
||
59 | */ |
||
60 | View Code Duplication | public static function extcal_loadImg(&$REQUEST, &$event_picture1, &$event_picture2) |
|
114 | |||
115 | /******************************************************************* |
||
116 | * |
||
117 | ****************************************************************** |
||
118 | * @param $cat |
||
119 | * @param bool $addNone |
||
120 | * @param string $name |
||
121 | * @return XoopsFormSelect |
||
122 | */ |
||
123 | View Code Duplication | public static function getListCategories($cat, $addNone = true, $name = 'cat') |
|
141 | |||
142 | /******************************************************************* |
||
143 | * |
||
144 | ****************************************************************** |
||
145 | * @param string $name |
||
146 | * @param $cat |
||
147 | * @return array |
||
148 | */ |
||
149 | View Code Duplication | public static function getCheckeCategories($name = 'cat', $cat) |
|
176 | |||
177 | /******************************************************************* |
||
178 | * |
||
179 | ****************************************************************** |
||
180 | * @param string $name |
||
181 | * @param string $caption |
||
182 | * @param $defaut |
||
183 | * @param bool $addNone |
||
184 | * @return XoopsFormSelect |
||
185 | */ |
||
186 | View Code Duplication | public static function getListOrderBy($name = 'orderby', $caption = '', $defaut, $addNone = false) |
|
209 | |||
210 | /******************************************************************* |
||
211 | * |
||
212 | ****************************************************************** |
||
213 | * @param string $name |
||
214 | * @param string $caption |
||
215 | * @param $defaut |
||
216 | * @return XoopsFormSelect |
||
217 | */ |
||
218 | View Code Duplication | public static function getListAndOr($name = 'andor', $caption = '', $defaut) |
|
229 | |||
230 | /******************************************************************* |
||
231 | * |
||
232 | ****************************************************************** |
||
233 | * @param $name |
||
234 | * @param $caption |
||
235 | * @param $defaut |
||
236 | * @param $options |
||
237 | * @param string $sep |
||
238 | * @return XoopsFormSelect |
||
239 | */ |
||
240 | View Code Duplication | public static function getList($name, $caption, $defaut, $options, $sep = ';') |
|
255 | |||
256 | /******************************************************************* |
||
257 | * |
||
258 | ****************************************************************** |
||
259 | * @param $ts |
||
260 | * @param $startMonth |
||
261 | * @param $endMonth |
||
262 | * @param string $mode |
||
263 | * @return DateTime |
||
264 | */ |
||
265 | public static function getDateBetweenDates($ts, $startMonth, $endMonth, $mode = 'w') |
||
303 | |||
304 | /* |
||
305 | Sunday 2009-11-01 00:00:00 |
||
306 | Sunday 2009-11-08 00:00:00 |
||
307 | Sunday 2009-11-15 00:00:00 |
||
308 | Sunday 2009-11-22 00:00:00 |
||
309 | Sunday 2009-11-29 00:00:00 |
||
310 | Sunday 2009-12-06 00:00:00 |
||
311 | ... |
||
312 | */ |
||
313 | /** |
||
314 | * @param $period |
||
315 | */ |
||
316 | public static function echoDateArray($period) |
||
322 | |||
323 | /*****************************************************************/ |
||
324 | /** |
||
325 | * @param $t |
||
326 | * @param string $msg |
||
327 | */ |
||
328 | View Code Duplication | public static function ext_echoArray($t, $msg = '') |
|
337 | |||
338 | /*****************************************************************/ |
||
339 | /** |
||
340 | * @param $line |
||
341 | * @param string $msg |
||
342 | */ |
||
343 | public static function ext_echo($line, $msg = '') |
||
350 | |||
351 | /*****************************************************************/ |
||
352 | /** |
||
353 | * @param $tsName |
||
354 | * @param string $msg |
||
355 | */ |
||
356 | public static function ext_echoTSN($tsName, $msg = '') |
||
362 | |||
363 | /*****************************************************************/ |
||
364 | /** |
||
365 | * @param $ts |
||
366 | * @param $tsName |
||
367 | * @param string $msg |
||
368 | */ |
||
369 | View Code Duplication | public static function ext_echoTSU($ts, $tsName, $msg = '') |
|
377 | |||
378 | /*****************************************************************/ |
||
379 | /*****************************************************************/ |
||
380 | /** |
||
381 | * @param $date |
||
382 | * @param string $sep |
||
383 | * |
||
384 | * @return int |
||
385 | */ |
||
386 | View Code Duplication | public static function ext_convert_date($date, $sep = '-') |
|
399 | |||
400 | /** |
||
401 | * @param $givendate |
||
402 | * @param int $day |
||
403 | * @param int $mth |
||
404 | * @param int $yr |
||
405 | * |
||
406 | * @return int |
||
407 | */ |
||
408 | View Code Duplication | public static function ext_DateAdd($givendate, $day = 0, $mth = 0, $yr = 0) |
|
416 | |||
417 | /** |
||
418 | * @param $date |
||
419 | * @param $number |
||
420 | * @param $interval |
||
421 | * |
||
422 | * @return int |
||
423 | */ |
||
424 | View Code Duplication | public static function ext_DateAdd2($date, $number, $interval = 'd') |
|
467 | |||
468 | // function date_diff($date1, $date2) { |
||
469 | // $current = $date1; |
||
470 | // $datetime2 = date_create($date2); |
||
471 | // $count = 0; |
||
472 | // while (date_create($current) < $datetime2) { |
||
473 | // $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); |
||
474 | // ++$count; |
||
475 | // } |
||
476 | // return $count; |
||
477 | // } |
||
478 | |||
479 | /**************************************************************************/ |
||
480 | /** |
||
481 | * @param $color |
||
482 | * @param $plancher |
||
483 | * @param $plafond |
||
484 | * |
||
485 | * @return string |
||
486 | */ |
||
487 | public static function getLighterColor($color, $plancher, $plafond) |
||
495 | /**************************************************************************/ |
||
496 | |||
497 | } |
||
498 |
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.