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 iCalDate 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 iCalDate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class iCalDate { |
||
| 34 | /**#@+ |
||
| 35 | * @access private |
||
| 36 | */ |
||
| 37 | |||
| 38 | /** Text version */ |
||
| 39 | var $_text; |
||
| 40 | |||
| 41 | /** Epoch version */ |
||
| 42 | var $_epoch; |
||
| 43 | |||
| 44 | /** Fragmented parts */ |
||
| 45 | var $_yy; |
||
| 46 | var $_mo; |
||
| 47 | var $_dd; |
||
| 48 | var $_hh; |
||
| 49 | var $_mi; |
||
| 50 | var $_ss; |
||
| 51 | var $_tz; |
||
| 52 | |||
| 53 | /** Which day of the week does the week start on */ |
||
| 54 | var $_wkst; |
||
| 55 | |||
| 56 | /**#@-*/ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The constructor takes either an iCalendar date, a text string formatted as |
||
| 60 | * an iCalendar date, or epoch seconds. |
||
| 61 | */ |
||
| 62 | function iCalDate( $input ) { |
||
|
|
|||
| 63 | if ( gettype($input) == 'object' ) { |
||
| 64 | $this->_text = $input->_text; |
||
| 65 | $this->_epoch = $input->_epoch; |
||
| 66 | $this->_yy = $input->_yy; |
||
| 67 | $this->_mo = $input->_mo; |
||
| 68 | $this->_dd = $input->_dd; |
||
| 69 | $this->_hh = $input->_hh; |
||
| 70 | $this->_mi = $input->_mi; |
||
| 71 | $this->_ss = $input->_ss; |
||
| 72 | $this->_tz = $input->_tz; |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->_wkst = 1; // Monday |
||
| 77 | if ( preg_match( '/^\d{8}[T ]\d{6}$/', $input ) ) { |
||
| 78 | $this->SetLocalDate($input); |
||
| 79 | } |
||
| 80 | else if ( preg_match( '/^\d{8}[T ]\d{6}Z$/', $input ) ) { |
||
| 81 | $this->SetGMTDate($input); |
||
| 82 | } |
||
| 83 | else if ( intval($input) == 0 ) { |
||
| 84 | $this->SetLocalDate(strtotime($input)); |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | else { |
||
| 88 | $this->SetEpochDate($input); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Set the date from a text string |
||
| 95 | */ |
||
| 96 | function SetGMTDate( $input ) { |
||
| 97 | $this->_text = $input; |
||
| 98 | $this->_PartsFromText(); |
||
| 99 | $this->_GMTEpochFromParts(); |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Set the date from a text string |
||
| 105 | */ |
||
| 106 | function SetLocalDate( $input ) { |
||
| 107 | $this->_text = $input; |
||
| 108 | $this->_PartsFromText(); |
||
| 109 | $this->_EpochFromParts(); |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Set the date from an epoch |
||
| 115 | */ |
||
| 116 | function SetEpochDate( $input ) { |
||
| 117 | $this->_epoch = intval($input); |
||
| 118 | $this->_TextFromEpoch(); |
||
| 119 | $this->_PartsFromText(); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Given an epoch date, convert it to text |
||
| 125 | */ |
||
| 126 | function _TextFromEpoch() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Given a GMT epoch date, convert it to text |
||
| 133 | */ |
||
| 134 | function _GMTTextFromEpoch() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Given a text date, convert it to parts |
||
| 141 | */ |
||
| 142 | function _PartsFromText() { |
||
| 143 | $this->_yy = intval(substr($this->_text,0,4)); |
||
| 144 | $this->_mo = intval(substr($this->_text,4,2)); |
||
| 145 | $this->_dd = intval(substr($this->_text,6,2)); |
||
| 146 | $this->_hh = intval(substr($this->_text,9,2)); |
||
| 147 | $this->_mi = intval(substr($this->_text,11,2)); |
||
| 148 | $this->_ss = intval(substr($this->_text,13,2)); |
||
| 149 | } |
||
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * Given a GMT text date, convert it to an epoch |
||
| 154 | */ |
||
| 155 | function _GMTEpochFromParts() { |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Given a local text date, convert it to an epoch |
||
| 163 | */ |
||
| 164 | function _EpochFromParts() { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the day of week used for calculation of week starts |
||
| 172 | * |
||
| 173 | * @param string $weekstart The day of the week which is the first business day. |
||
| 174 | */ |
||
| 175 | function SetWeekStart($weekstart) { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the day of week used for calculation of week starts |
||
| 183 | */ |
||
| 184 | function Render( $fmt = 'Y-m-d H:i:s' ) { |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Render the date as GMT |
||
| 191 | */ |
||
| 192 | function RenderGMT( $fmt = 'Ymd\THis\Z' ) { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * No of days in a month 1(Jan) - 12(Dec) |
||
| 199 | */ |
||
| 200 | function DaysInMonth( $mo=false, $yy=false ) { |
||
| 201 | if ( $mo === false ) $mo = $this->_mo; |
||
| 202 | switch( $mo ) { |
||
| 203 | case 1: // January |
||
| 204 | case 3: // March |
||
| 205 | case 5: // May |
||
| 206 | case 7: // July |
||
| 207 | case 8: // August |
||
| 208 | case 10: // October |
||
| 209 | case 12: // December |
||
| 210 | return 31; |
||
| 211 | break; |
||
| 212 | |||
| 213 | case 4: // April |
||
| 214 | case 6: // June |
||
| 215 | case 9: // September |
||
| 216 | case 11: // November |
||
| 217 | return 30; |
||
| 218 | break; |
||
| 219 | |||
| 220 | case 2: // February |
||
| 221 | if ( $yy === false ) $yy = $this->_yy; |
||
| 222 | if ( (($yy % 4) == 0) && ((($yy % 100) != 0) || (($yy % 400) == 0) ) ) return 29; |
||
| 223 | return 28; |
||
| 224 | break; |
||
| 225 | |||
| 226 | default: |
||
| 227 | dbg_error_log( "ERROR"," Invalid month of '%s' passed to DaysInMonth", $mo ); |
||
| 228 | break; |
||
| 229 | |||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the day in the month to what we have been given |
||
| 236 | */ |
||
| 237 | function SetMonthDay( $dd ) { |
||
| 238 | if ( $dd == $this->_dd ) return; // Shortcut |
||
| 239 | $dd = min($dd,$this->DaysInMonth()); |
||
| 240 | $this->_dd = $dd; |
||
| 241 | $this->_EpochFromParts(); |
||
| 242 | $this->_TextFromEpoch(); |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Add some number of months to a date |
||
| 248 | */ |
||
| 249 | function AddMonths( $mo ) { |
||
| 250 | dbg_error_log( "RRule", " Adding %d months to %s", $mo, $this->_text ); |
||
| 251 | $this->_mo += $mo; |
||
| 252 | while ( $this->_mo < 1 ) { |
||
| 253 | $this->_mo += 12; |
||
| 254 | $this->_yy--; |
||
| 255 | } |
||
| 256 | while ( $this->_mo > 12 ) { |
||
| 257 | $this->_mo -= 12; |
||
| 258 | $this->_yy++; |
||
| 259 | } |
||
| 260 | |||
| 261 | if ( ($this->_dd > 28 && $this->_mo == 2) || $this->_dd > 30 ) { |
||
| 262 | // Ensure the day of month is still reasonable and coerce to last day of month if needed |
||
| 263 | $dim = $this->DaysInMonth(); |
||
| 264 | if ( $this->_dd > $dim ) { |
||
| 265 | $this->_dd = $dim; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | $this->_EpochFromParts(); |
||
| 269 | $this->_TextFromEpoch(); |
||
| 270 | dbg_error_log( "RRule", " Added %d months and got %s", $mo, $this->_text ); |
||
| 271 | } |
||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * Add some integer number of days to a date |
||
| 276 | */ |
||
| 277 | function AddDays( $dd ) { |
||
| 278 | $at_start = $this->_text; |
||
| 279 | $this->_dd += $dd; |
||
| 280 | while ( 1 > $this->_dd ) { |
||
| 281 | $this->_mo--; |
||
| 282 | if ( $this->_mo < 1 ) { |
||
| 283 | $this->_mo += 12; |
||
| 284 | $this->_yy--; |
||
| 285 | } |
||
| 286 | $this->_dd += $this->DaysInMonth(); |
||
| 287 | } |
||
| 288 | while ( ($dim = $this->DaysInMonth($this->_mo)) < $this->_dd ) { |
||
| 289 | $this->_dd -= $dim; |
||
| 290 | $this->_mo++; |
||
| 291 | if ( $this->_mo > 12 ) { |
||
| 292 | $this->_mo -= 12; |
||
| 293 | $this->_yy++; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | $this->_EpochFromParts(); |
||
| 297 | $this->_TextFromEpoch(); |
||
| 298 | dbg_error_log( "RRule", " Added %d days to %s and got %s", $dd, $at_start, $this->_text ); |
||
| 299 | } |
||
| 300 | |||
| 301 | |||
| 302 | /** |
||
| 303 | * Add duration |
||
| 304 | */ |
||
| 305 | function AddDuration( $duration ) { |
||
| 306 | if ( strstr($duration,'T') === false ) $duration .= 'T'; |
||
| 307 | list( $sign, $days, $time ) = preg_split( '/[PT]/', $duration ); |
||
| 308 | $sign = ( $sign == "-" ? -1 : 1); |
||
| 309 | dbg_error_log( "RRule", " Adding duration to '%s' of sign: %d, days: %s, time: %s", $this->_text, $sign, $days, $time ); |
||
| 310 | if ( preg_match( '/(\d+)(D|W)/', $days, $matches ) ) { |
||
| 311 | $days = intval($matches[1]); |
||
| 312 | if ( $matches[2] == 'W' ) $days *= 7; |
||
| 313 | $this->AddDays( $days * $sign ); |
||
| 314 | } |
||
| 315 | $hh = 0; $mi = 0; $ss = 0; |
||
| 316 | if ( preg_match( '/(\d+)(H)/', $time, $matches ) ) $hh = $matches[1]; |
||
| 317 | if ( preg_match( '/(\d+)(M)/', $time, $matches ) ) $mi = $matches[1]; |
||
| 318 | if ( preg_match( '/(\d+)(S)/', $time, $matches ) ) $ss = $matches[1]; |
||
| 319 | |||
| 320 | dbg_error_log( "RRule", " Adding %02d:%02d:%02d * %d to %02d:%02d:%02d", $hh, $mi, $ss, $sign, $this->_hh, $this->_mi, $this->_ss ); |
||
| 321 | $this->_hh += ($hh * $sign); |
||
| 322 | $this->_mi += ($mi * $sign); |
||
| 323 | $this->_ss += ($ss * $sign); |
||
| 324 | |||
| 325 | View Code Duplication | if ( $this->_ss < 0 ) { $this->_mi -= (intval(abs($this->_ss/60))+1); $this->_ss += ((intval(abs($this->_mi/60))+1) * 60); } |
|
| 326 | View Code Duplication | if ( $this->_ss > 59) { $this->_mi += (intval(abs($this->_ss/60))+1); $this->_ss -= ((intval(abs($this->_mi/60))+1) * 60); } |
|
| 327 | View Code Duplication | if ( $this->_mi < 0 ) { $this->_hh -= (intval(abs($this->_mi/60))+1); $this->_mi += ((intval(abs($this->_mi/60))+1) * 60); } |
|
| 328 | View Code Duplication | if ( $this->_mi > 59) { $this->_hh += (intval(abs($this->_mi/60))+1); $this->_mi -= ((intval(abs($this->_mi/60))+1) * 60); } |
|
| 329 | View Code Duplication | if ( $this->_hh < 0 ) { $this->AddDays( -1 * (intval(abs($this->_hh/24))+1) ); $this->_hh += ((intval(abs($this->_hh/24))+1)*24); } |
|
| 330 | View Code Duplication | if ( $this->_hh > 23) { $this->AddDays( (intval(abs($this->_hh/24))+1) ); $this->_hh -= ((intval(abs($this->_hh/24))+1)*24); } |
|
| 331 | |||
| 332 | $this->_EpochFromParts(); |
||
| 333 | $this->_TextFromEpoch(); |
||
| 334 | } |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * Produce an iCalendar format DURATION for the difference between this an another iCalDate |
||
| 339 | * |
||
| 340 | * @param date $from The start of the period |
||
| 341 | * @return string The date difference, as an iCalendar duration format |
||
| 342 | */ |
||
| 343 | function DateDifference( $from ) { |
||
| 344 | if ( !is_object($from) ) { |
||
| 345 | $from = new iCalDate($from); |
||
| 346 | } |
||
| 347 | if ( $from->_epoch < $this->_epoch ) { |
||
| 348 | /** One way to simplify is to always go for positive differences */ |
||
| 349 | return( "-". $from->DateDifference( $this ) ); |
||
| 350 | } |
||
| 351 | // if ( $from->_yy == $this->_yy && $from->_mo == $this->_mo ) { |
||
| 352 | /** Also somewhat simpler if we can use seconds */ |
||
| 353 | $diff = $from->_epoch - $this->_epoch; |
||
| 354 | $result = ""; |
||
| 355 | if ( $diff >= 86400) { |
||
| 356 | $result = intval($diff / 86400); |
||
| 357 | $diff = $diff % 86400; |
||
| 358 | if ( $diff == 0 && (($result % 7) == 0) ) { |
||
| 359 | // Duration is an integer number of weeks. |
||
| 360 | $result .= intval($result / 7) . "W"; |
||
| 361 | return $result; |
||
| 362 | } |
||
| 363 | $result .= "D"; |
||
| 364 | } |
||
| 365 | $result = "P".$result."T"; |
||
| 366 | if ( $diff >= 3600) { |
||
| 367 | $result .= intval($diff / 3600) . "H"; |
||
| 368 | $diff = $diff % 3600; |
||
| 369 | } |
||
| 370 | if ( $diff >= 60) { |
||
| 371 | $result .= intval($diff / 60) . "M"; |
||
| 372 | $diff = $diff % 60; |
||
| 373 | } |
||
| 374 | if ( $diff > 0) { |
||
| 375 | $result .= intval($diff) . "S"; |
||
| 376 | } |
||
| 377 | return $result; |
||
| 378 | // } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * From an intense reading of RFC2445 it appears that durations which are not expressible |
||
| 382 | * in Weeks/Days/Hours/Minutes/Seconds are invalid. |
||
| 383 | * ==> This code is not needed then :-) |
||
| 384 | $yy = $from->_yy - $this->_yy; |
||
| 385 | $mo = $from->_mo - $this->_mo; |
||
| 386 | $dd = $from->_dd - $this->_dd; |
||
| 387 | $hh = $from->_hh - $this->_hh; |
||
| 388 | $mi = $from->_mi - $this->_mi; |
||
| 389 | $ss = $from->_ss - $this->_ss; |
||
| 390 | |||
| 391 | if ( $ss < 0 ) { $mi -= 1; $ss += 60; } |
||
| 392 | if ( $mi < 0 ) { $hh -= 1; $mi += 60; } |
||
| 393 | if ( $hh < 0 ) { $dd -= 1; $hh += 24; } |
||
| 394 | if ( $dd < 0 ) { $mo -= 1; $dd += $this->DaysInMonth(); } // Which will use $this->_(mo|yy) - seemingly sensible |
||
| 395 | if ( $mo < 0 ) { $yy -= 1; $mo += 12; } |
||
| 396 | |||
| 397 | $result = ""; |
||
| 398 | if ( $yy > 0) { $result .= $yy."Y"; } |
||
| 399 | if ( $mo > 0) { $result .= $mo."M"; } |
||
| 400 | if ( $dd > 0) { $result .= $dd."D"; } |
||
| 401 | $result .= "T"; |
||
| 402 | if ( $hh > 0) { $result .= $hh."H"; } |
||
| 403 | if ( $mi > 0) { $result .= $mi."M"; } |
||
| 404 | if ( $ss > 0) { $result .= $ss."S"; } |
||
| 405 | return $result; |
||
| 406 | */ |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Test to see if our _mo matches something in the list of months we have received. |
||
| 411 | * @param string $monthlist A comma-separated list of months. |
||
| 412 | * @return boolean Whether this date falls within one of those months. |
||
| 413 | */ |
||
| 414 | function TestByMonth( $monthlist ) { |
||
| 415 | dbg_error_log( "RRule", " Testing BYMONTH %s against month %d", (isset($monthlist) ? $monthlist : "no month list"), $this->_mo ); |
||
| 416 | if ( !isset($monthlist) ) return true; // If BYMONTH is not specified any month is OK |
||
| 417 | $months = array_flip(split( ',',$monthlist )); |
||
| 418 | return isset($months[$this->_mo]); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Applies any BYDAY to the month to return a set of days |
||
| 423 | * @param string $byday The BYDAY rule |
||
| 424 | * @return array An array of the day numbers for the month which meet the rule. |
||
| 425 | */ |
||
| 426 | function GetMonthByDay($byday) { |
||
| 427 | dbg_error_log( "RRule", " Applying BYDAY %s to month", $byday ); |
||
| 428 | $days_in_month = $this->DaysInMonth(); |
||
| 429 | $dayrules = split(',',$byday); |
||
| 430 | $set = array(); |
||
| 431 | $first_dow = (date('w',$this->_epoch) - $this->_dd + 36) % 7; |
||
| 432 | foreach( $dayrules AS $k => $v ) { |
||
| 433 | $days = $this->MonthDays($first_dow,$days_in_month,$v); |
||
| 434 | foreach( $days AS $k2 => $v2 ) { |
||
| 435 | $set[$v2] = $v2; |
||
| 436 | } |
||
| 437 | } |
||
| 438 | asort( $set, SORT_NUMERIC ); |
||
| 439 | return $set; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Applies any BYMONTHDAY to the month to return a set of days |
||
| 444 | * @param string $bymonthday The BYMONTHDAY rule |
||
| 445 | * @return array An array of the day numbers for the month which meet the rule. |
||
| 446 | */ |
||
| 447 | function GetMonthByMonthDay($bymonthday) { |
||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * Applies any BYDAY to the week to return a set of days |
||
| 463 | * @param string $byday The BYDAY rule |
||
| 464 | * @param string $increasing When we are moving by months, we want any day of the week, but when by day we only want to increase. Default false. |
||
| 465 | * @return array An array of the day numbers for the week which meet the rule. |
||
| 466 | */ |
||
| 467 | function GetWeekByDay($byday, $increasing = false) { |
||
| 468 | global $ical_weekdays; |
||
| 469 | dbg_error_log( "RRule", " Applying BYDAY %s to week", $byday ); |
||
| 470 | $days = explode(',',$byday); |
||
| 471 | $dow = date('w',$this->_epoch); |
||
| 472 | $set = array(); |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * Test if $this is greater than the date parameter |
||
| 493 | * @param string $lesser The other date, as a local time string |
||
| 494 | * @return boolean True if $this > $lesser |
||
| 495 | */ |
||
| 496 | function GreaterThan($lesser) { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Test if $this is less than the date parameter |
||
| 508 | * @param string $greater The other date, as a local time string |
||
| 509 | * @return boolean True if $this < $greater |
||
| 510 | */ |
||
| 511 | function LessThan($greater) { |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * Given a MonthDays string like "1MO", "-2WE" return an integer day of the month. |
||
| 523 | * |
||
| 524 | * @param string $dow_first The day of week of the first of the month. |
||
| 525 | * @param string $days_in_month The number of days in the month. |
||
| 526 | * @param string $dayspec The specification for a month day (or days) which we parse. |
||
| 527 | * |
||
| 528 | * @return array An array of the day numbers for the month which meet the rule. |
||
| 529 | */ |
||
| 530 | function &MonthDays($dow_first, $days_in_month, $dayspec) { |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * Given set position descriptions like '1', '3', '11', '-3' or '-1' and a set, |
||
| 574 | * return the subset matching the list of set positions. |
||
| 575 | * |
||
| 576 | * @param string $bysplist The list of set positions. |
||
| 577 | * @param string $set The set of days that we will apply the positions to. |
||
| 578 | * |
||
| 579 | * @return array The subset which matches. |
||
| 580 | */ |
||
| 581 | function &ApplyBySetPos($bysplist, $set) { |
||
| 598 | } |
||
| 599 | |||
| 995 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.