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:
| 1 | <?php |
||
| 33 | class Datium |
||
| 34 | { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Store DateTime object |
||
| 38 | */ |
||
| 39 | protected $date_time; |
||
| 40 | |||
| 41 | protected static $static_date_time; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Store config file statements |
||
| 45 | * |
||
| 46 | * @param array |
||
| 47 | */ |
||
| 48 | protected $config; |
||
| 49 | |||
| 50 | protected $date_interval_expression; |
||
| 51 | |||
| 52 | protected static $date_start; |
||
| 53 | |||
| 54 | protected static $date_end; |
||
| 55 | |||
| 56 | protected $translate_from; |
||
| 57 | |||
| 58 | protected $translate_to; |
||
| 59 | |||
| 60 | protected $toConfig; |
||
| 61 | |||
| 62 | protected $fromConfig; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Return store day number |
||
| 66 | * |
||
| 67 | * @param integer |
||
| 68 | */ |
||
| 69 | protected $day_of; |
||
| 70 | |||
| 71 | protected $leap; |
||
| 72 | |||
| 73 | protected $events; |
||
| 74 | |||
| 75 | protected $translate; |
||
| 76 | |||
| 77 | protected $gregorian_DayofWeek; |
||
| 78 | |||
| 79 | protected $convert_calendar; |
||
| 80 | |||
| 81 | protected $calendar_type; |
||
| 82 | |||
| 83 | protected static $array_date; |
||
| 84 | |||
| 85 | protected static $call_type; |
||
| 86 | |||
| 87 | protected $translate_from_file; |
||
| 88 | |||
| 89 | protected $translate_to_file; |
||
| 90 | |||
| 91 | protected $language; |
||
| 92 | |||
| 93 | protected static $timestamp; |
||
| 94 | |||
| 95 | protected $day_of_week; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * SimpleDiff |
||
| 99 | * |
||
| 100 | * @param integer |
||
| 101 | */ |
||
| 102 | protected $simpleDiff; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Datium class constructure |
||
| 106 | */ |
||
| 107 | public function __construct() |
||
| 108 | { |
||
| 109 | |||
| 110 | $this->language = 'en'; |
||
| 111 | |||
| 112 | $this->translate_from = 'gregorian'; |
||
| 113 | |||
| 114 | $this->translate_to = 'gregorian'; |
||
| 115 | $this->setConfig(); |
||
| 116 | |||
| 117 | $this->calendar_type = $this->config['default_calendar']; |
||
| 118 | |||
| 119 | $this->calendar_type = 'gregorian'; |
||
| 120 | |||
| 121 | switch (Datium::$call_type) { |
||
| 122 | |||
| 123 | |||
| 124 | case 'create-timestamp': |
||
| 125 | |||
| 126 | $this->date_time = new DateTime(); |
||
| 127 | |||
| 128 | $this->date_time->setTimestamp(self::$timestamp); |
||
| 129 | |||
| 130 | break; |
||
| 131 | |||
| 132 | case 'now': |
||
| 133 | |||
| 134 | $this->date_time = new DateTime('now'); |
||
| 135 | |||
| 136 | break; |
||
| 137 | |||
| 138 | case 'make': |
||
| 139 | |||
| 140 | $this->date_time = new DateTime('now'); |
||
| 141 | |||
| 142 | $this->date_time->setDate( |
||
| 143 | self::$array_date['year'], |
||
| 144 | self::$array_date['month'], |
||
| 145 | self::$array_date['day'] |
||
| 146 | ); |
||
| 147 | |||
| 148 | $this->date_time->setTime( |
||
| 149 | self::$array_date['hour'], |
||
| 150 | self::$array_date['minute'], |
||
| 151 | self::$array_date['second'] |
||
| 152 | ); |
||
| 153 | |||
| 154 | break; |
||
| 155 | |||
| 156 | case 'set': |
||
| 157 | |||
| 158 | $this->date_time = Datium::$static_date_time; |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | $this->gregorian_DayofWeek = $this->date_time->format('w'); |
||
| 163 | |||
| 164 | $this->day_of_week = $this->date_time->format('l'); |
||
| 165 | |||
| 166 | $this->convert_calendar = new Convert(); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Return all datetime parts as an object |
||
| 171 | * |
||
| 172 | * @return object |
||
| 173 | */ |
||
| 174 | public function all() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get current datetime |
||
| 197 | * |
||
| 198 | * @since Aug 17 2015 |
||
| 199 | * @return object |
||
| 200 | */ |
||
| 201 | public static function now() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set config |
||
| 212 | * |
||
| 213 | * @since June 28 2016 |
||
| 214 | * |
||
| 215 | * @param array $config |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function setConfig($config = []) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Create new date time |
||
| 230 | * |
||
| 231 | * @param integer $year Year number |
||
| 232 | * @param integer $month month number |
||
| 233 | * @param integer $day day number |
||
| 234 | * @param integer $hour hour number |
||
| 235 | * @param integer $minute minute number |
||
| 236 | * @param integer $second second number |
||
| 237 | * |
||
| 238 | * @return object |
||
| 239 | */ |
||
| 240 | public static function create( |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Accept Timestamp as Datium initializion |
||
| 275 | * |
||
| 276 | * @param timestamp $timestamp Input timestamp value |
||
| 277 | * |
||
| 278 | * @return object |
||
| 279 | */ |
||
| 280 | public static function createTimestamp($timestamp) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Select The range between two date object |
||
| 302 | * |
||
| 303 | * @param object $date_start Start of the DateTime |
||
| 304 | * @param object $date_end End of the DateTime |
||
| 305 | * |
||
| 306 | * @return object |
||
| 307 | */ |
||
| 308 | public static function between($date_start, $date_end) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Convert from current calendar, what type is current calendar? |
||
| 323 | * |
||
| 324 | * @param object $calendar Assigned calendar to start from |
||
| 325 | * |
||
| 326 | * @return $object |
||
|
|
|||
| 327 | */ |
||
| 328 | View Code Duplication | public function from($calendar) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Convert date to current Date |
||
| 349 | * |
||
| 350 | * @param object $calendar Assigned calendar to when calendar should start. |
||
| 351 | * |
||
| 352 | * @return object |
||
| 353 | */ |
||
| 354 | View Code Duplication | public function to($calendar) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Difference between two time |
||
| 374 | * |
||
| 375 | * @param DateTime $start Start of the date |
||
| 376 | * @param DateTime $end End of the date |
||
| 377 | * |
||
| 378 | * @return object |
||
| 379 | */ |
||
| 380 | public static function diff($start, $end) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Add new date value to current date |
||
| 405 | * |
||
| 406 | * @param string $value How much date should be added to current date |
||
| 407 | * |
||
| 408 | * @return object |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function add($value) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Sub date from current date |
||
| 449 | * |
||
| 450 | * @param string $value How much date should increase from current date |
||
| 451 | * |
||
| 452 | * @return obejct |
||
| 453 | */ |
||
| 454 | View Code Duplication | public function sub($value) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Check if current year is leap or not |
||
| 493 | * |
||
| 494 | * @param string $type Name of the calendar to caculate leap year |
||
| 495 | * |
||
| 496 | * @return boolean |
||
| 497 | */ |
||
| 498 | public function leap() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Caculate day of year or week |
||
| 509 | * |
||
| 510 | * @since Aug, 22 2015 |
||
| 511 | * |
||
| 512 | * @return integer |
||
| 513 | */ |
||
| 514 | public function dayOf() |
||
| 521 | |||
| 522 | // public function events() |
||
| 523 | // { |
||
| 524 | // |
||
| 525 | // if (Datium::$call_type == 'between' ) { |
||
| 526 | // |
||
| 527 | // $this->events = new Events(Datium::$date_start, Datium::$date_end); |
||
| 528 | // |
||
| 529 | // } else { |
||
| 530 | // |
||
| 531 | // $this->events = new Events($this->date_time); |
||
| 532 | // |
||
| 533 | // } |
||
| 534 | // |
||
| 535 | // return $this->events; |
||
| 536 | // |
||
| 537 | // } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Return Datetime as a original object |
||
| 541 | * |
||
| 542 | * @since Oct 22, 2015 |
||
| 543 | * |
||
| 544 | * @return object |
||
| 545 | */ |
||
| 546 | public function object() |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Translate current date string to selected language |
||
| 555 | * |
||
| 556 | * @param string $language language short name fa, en, ar ... |
||
| 557 | * |
||
| 558 | * @return object |
||
| 559 | */ |
||
| 560 | public function lang($language = 'fa') |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Return object as timestamp |
||
| 577 | * |
||
| 578 | * @return timestamp |
||
| 579 | */ |
||
| 580 | public function timestamp() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Return fainal result |
||
| 589 | * |
||
| 590 | * @param string $format Date format |
||
| 591 | * |
||
| 592 | * @since Aug 17 2015 |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function get($format = 'Y-m-d H:i:s') |
||
| 659 | } |
||
| 660 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.