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 getEvent($eventId) |
|
| 42 | { |
||
| 43 | $eventHandler = Extcal\Helper::getInstance()->getHandler(_EXTCAL_CLN_EVENT); |
||
| 44 | $event = $eventHandler->getEvent($eventId); |
||
| 45 | $t = $event->getVars(); |
||
| 46 | $data = []; |
||
| 47 | // while (list($key, $val) = each($t)) { |
||
| 48 | foreach ($t as $key => $val) { |
||
| 49 | $data[$key] = $val['value']; |
||
| 50 | } |
||
| 51 | |||
| 52 | return $data; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $REQUEST |
||
| 57 | * @param $event_picture1 |
||
| 58 | * @param $event_picture2 |
||
| 59 | */ |
||
| 60 | View Code Duplication | public static function loadImg(&$REQUEST, &$event_picture1, &$event_picture2) |
|
| 61 | { |
||
| 62 | /////////////////////////////////////////////////////////////////////////////// |
||
| 63 | $uploaddir_event = XOOPS_ROOT_PATH . '/uploads/extcal/'; |
||
| 64 | $uploadurl_event = XOOPS_URL . '/uploads/extcal/'; |
||
| 65 | //$picture = ''; |
||
| 66 | for ($j = 1; $j < 3; ++$j) { |
||
| 67 | $delimg = @$REQUEST['delimg_' . $j . '']; |
||
| 68 | $delimg = isset($delimg) ? (int)$delimg : 0; |
||
| 69 | if (0 == $delimg && !empty($REQUEST['xoops_upload_file'][$j])) { |
||
| 70 | $upload = new \XoopsMediaUploader($uploaddir_event, [ |
||
| 71 | 'image/gif', |
||
| 72 | 'image/jpeg', |
||
| 73 | 'image/pjpeg', |
||
| 74 | 'image/x-png', |
||
| 75 | 'image/png', |
||
| 76 | 'image/jpg', |
||
| 77 | ], 3145728, null, null); |
||
| 78 | if ($upload->fetchMedia($REQUEST['xoops_upload_file'][$j])) { |
||
| 79 | $upload->setPrefix('event_'); |
||
| 80 | $upload->fetchMedia($REQUEST['xoops_upload_file'][$j]); |
||
| 81 | if (!$upload->upload()) { |
||
| 82 | $errors = $upload->getErrors(); |
||
| 83 | redirect_header('javascript:history.go(-1)', 3, $errors); |
||
| 84 | } else { |
||
| 85 | if (1 == $j) { |
||
| 86 | $event_picture1 = $upload->getSavedFileName(); |
||
| 87 | } elseif (2 == $j) { |
||
| 88 | $event_picture2 = $upload->getSavedFileName(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } elseif (!empty($REQUEST['file' . $j])) { |
||
| 92 | if (1 == $j) { |
||
| 93 | $event_picture1 = $REQUEST['file' . $j]; |
||
| 94 | } elseif (2 == $j) { |
||
| 95 | $event_picture2 = $REQUEST['file' . $j]; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | $url_event = XOOPS_ROOT_PATH . '/uploads/extcal/' . $REQUEST['file' . $j]; |
||
| 100 | if (1 == $j) { |
||
| 101 | $event_picture1 = ''; |
||
| 102 | } elseif (2 == $j) { |
||
| 103 | $event_picture2 = ''; |
||
| 104 | } |
||
| 105 | if (is_file($url_event)) { |
||
| 106 | chmod($url_event, 0777); |
||
| 107 | unlink($url_event); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | //exit; |
||
| 112 | /////////////////////////////////////////////////////////////////////////////// |
||
| 113 | } |
||
| 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 | public static function getList($name, $caption, $defaut, $options, $sep = ';') |
||
| 241 | { |
||
| 242 | global $xoopsUser; |
||
| 243 | |||
| 244 | $select = new \XoopsFormSelect($caption, $name, $defaut); |
||
| 245 | if (!is_array($options)) { |
||
| 246 | $options = explode($sep, $options); |
||
| 247 | } |
||
| 248 | |||
| 249 | foreach ($options as $h => $hValue) { |
||
| 250 | $select->addOption($h, $options[$h]); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $select; |
||
| 254 | } |
||
| 255 | |||
| 256 | /******************************************************************* |
||
| 257 | * |
||
| 258 | ****************************************************************** |
||
| 259 | * @param $ts |
||
| 260 | * @param $startMonth |
||
| 261 | * @param $endMonth |
||
| 262 | * @param string $mode |
||
| 263 | * @return \DateTime |
||
| 264 | */ |
||
| 265 | View Code Duplication | public static function getDateBetweenDates($ts, $startMonth, $endMonth, $mode = 'w') |
|
| 266 | { |
||
| 267 | $d = new \DateTime($periodStart); |
||
| 268 | $d->setTimestamp($ts); |
||
| 269 | |||
| 270 | //echo "<br>affichage des periodes : <br>"; |
||
| 271 | $begin = new \DateTime(); |
||
| 272 | $begin->setTimestamp($startMonth); |
||
| 273 | //echo $begin->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53 |
||
| 274 | |||
| 275 | $end = new \DateTime(); |
||
| 276 | $end->setTimestamp($endMonth); |
||
| 277 | //echo $end->format("d/m/Y à H\hi:s").'<br>'; // 03/10/2007 à 19h39:53 |
||
| 278 | //echo "<hr>"; |
||
| 279 | $interval = \DateInterval::createFromDateString('next sunday'); |
||
| 280 | $period = new \DatePeriod($begin, $interval, $end); |
||
| 281 | //echoDateArray($period); |
||
| 282 | |||
| 283 | //echo "<hr>{$interval}"; |
||
| 284 | return $d; |
||
| 285 | |||
| 286 | //echo mktime($heure, $minute, $seconde, $mois, $jour, $an); |
||
| 287 | |||
| 288 | // |
||
| 289 | // $jour = date('d', $ts); |
||
| 290 | // $mois = date('m', $ts); |
||
| 291 | // $an = date('Y', $ts); |
||
| 292 | // $heure = date('H', $ts); |
||
| 293 | // $minute = date('i', $ts); |
||
| 294 | // $seconde = date('s', $ts); |
||
| 295 | // $d->setDate($heure,$minute,$seconde,$mois,$jour,$an); |
||
| 296 | |||
| 297 | // <?php |
||
| 298 | // $interval = DateInterval::createFromDateString('next sunday'); |
||
| 299 | // $period = new \DatePeriod($begin, $interval, $end); |
||
| 300 | // foreach ($period as $dt) { |
||
| 301 | // echo $dt->format( "l Y-m-d H:i:s\n" ); |
||
| 302 | } |
||
| 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 echoArray($t, $msg = '') |
|
| 329 | { |
||
| 330 | if ('' != $msg) { |
||
| 331 | echo "<hr>{$msg}<hr>"; |
||
| 332 | } |
||
| 333 | |||
| 334 | $txt = print_r($t, true); |
||
| 335 | echo '<pre>Number of items: ' . count($t) . "<br>{$txt}</pre>"; |
||
| 336 | } |
||
| 337 | |||
| 338 | /*****************************************************************/ |
||
| 339 | /** |
||
| 340 | * @param $line |
||
| 341 | * @param string $msg |
||
| 342 | */ |
||
| 343 | public static function extEcho($line, $msg = '') |
||
| 344 | { |
||
| 345 | if ('' != $msg) { |
||
| 346 | echo "<hr>{$msg}<hr>"; |
||
| 347 | } |
||
| 348 | echo $line . '<br>'; |
||
| 349 | } |
||
| 350 | |||
| 351 | /*****************************************************************/ |
||
| 352 | /** |
||
| 353 | * @param $tsName |
||
| 354 | * @param string $msg |
||
| 355 | */ |
||
| 356 | public static function echoTsn($tsName, $msg = '') |
||
| 357 | { |
||
| 358 | global $$tsName; |
||
| 359 | $ts = $$tsName; |
||
| 360 | static::echoTsu($ts, $tsName, $msg = ''); |
||
| 361 | } |
||
| 362 | |||
| 363 | /*****************************************************************/ |
||
| 364 | /** |
||
| 365 | * @param $ts |
||
| 366 | * @param $tsName |
||
| 367 | * @param string $msg |
||
| 368 | */ |
||
| 369 | View Code Duplication | public static function echoTsu($ts, $tsName, $msg = '') |
|
| 370 | { |
||
| 371 | if ('' != $msg) { |
||
| 372 | echo "<hr>{$msg}<hr>"; |
||
| 373 | } |
||
| 374 | |||
| 375 | echo 'date --->' . $tsName . ' = ' . $ts . ' - ' . date('d-m-Y H:m:s', $ts) . '<br>'; |
||
| 376 | } |
||
| 377 | |||
| 378 | /*****************************************************************/ |
||
| 379 | /*****************************************************************/ |
||
| 380 | /** |
||
| 381 | * @param $date |
||
| 382 | * @param string $sep |
||
| 383 | * |
||
| 384 | * @return int |
||
| 385 | */ |
||
| 386 | View Code Duplication | public static function convertDate($date, $sep = '-') |
|
| 387 | { |
||
| 388 | $lstSep = '/ .'; |
||
| 389 | |||
| 390 | for ($h = 0, $count = strlen($lstSep); $h < $count; ++$h) { |
||
| 391 | $sep2replace = substr($lstSep, $h, 1); |
||
| 392 | if (strpos($date, $sep2replace)) { |
||
| 393 | $date = str_replace($sep2replace, $sep, $date); |
||
| 394 | } |
||
| 395 | |||
| 396 | return strtotime($date); |
||
| 397 | } |
||
| 398 | } |
||
| 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 addDate($givendate, $day = 0, $mth = 0, $yr = 0) |
|
| 409 | { |
||
| 410 | //$cd = strtotime($givendate); |
||
| 411 | $cd = $givendate; |
||
| 412 | $newdate = date('Y-m-d h:i:s', mktime(date('h', $cd), date('i', $cd), date('s', $cd), date('m', $cd) + $mth, date('d', $cd) + $day, date('Y', $cd) + $yr)); |
||
| 413 | |||
| 414 | return strtotime($newdate); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param $date |
||
| 419 | * @param $number |
||
| 420 | * @param $interval |
||
| 421 | * |
||
| 422 | * @return int |
||
| 423 | */ |
||
| 424 | View Code Duplication | public static function addDate2($date, $number, $interval = 'd') |
|
| 425 | { |
||
| 426 | $date_time_array = getdate($date); |
||
| 427 | $hours = $date_time_array['hours']; |
||
| 428 | $minutes = $date_time_array['minutes']; |
||
| 429 | $seconds = $date_time_array['seconds']; |
||
| 430 | $month = $date_time_array['mon']; |
||
| 431 | $day = $date_time_array['mday']; |
||
| 432 | $year = $date_time_array['year']; |
||
| 433 | |||
| 434 | switch ($interval) { |
||
| 435 | |||
| 436 | case 'yyyy': |
||
| 437 | $year += $number; |
||
| 438 | break; |
||
| 439 | case 'q': |
||
| 440 | $year += ($number * 3); |
||
| 441 | break; |
||
| 442 | case 'm': |
||
| 443 | $month += $number; |
||
| 444 | break; |
||
| 445 | case 'y': |
||
| 446 | case 'd': |
||
| 447 | case 'w': |
||
| 448 | $day += $number; |
||
| 449 | break; |
||
| 450 | case 'ww': |
||
| 451 | $day += ($number * 7); |
||
| 452 | break; |
||
| 453 | case 'h': |
||
| 454 | $hours += $number; |
||
| 455 | break; |
||
| 456 | case 'n': |
||
| 457 | $minutes += $number; |
||
| 458 | break; |
||
| 459 | case 's': |
||
| 460 | $seconds += $number; |
||
| 461 | break; |
||
| 462 | } |
||
| 463 | $timestamp = mktime($hours, $minutes, $seconds, $month, $day, $year); |
||
| 464 | |||
| 465 | return $timestamp; |
||
| 466 | } |
||
| 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.