| Total Complexity | 46 |
| Total Lines | 450 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PearDate 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.
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 PearDate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class PearDate /* implements Calendar_Engine_Interface */ |
||
| 53 | { |
||
| 54 | /** |
||
| 55 | * Makes sure a given timestamp is only ever parsed once |
||
| 56 | * Uses a static variable to prevent date() being used twice |
||
| 57 | * for a date which is already known |
||
| 58 | * |
||
| 59 | * @param mixed $stamp Any timestamp format recognized by Pear::Date |
||
| 60 | * |
||
| 61 | * @return object Pear::Date object |
||
| 62 | * @access protected |
||
| 63 | */ |
||
| 64 | function stampCollection($stamp) |
||
|
|
|||
| 65 | { |
||
| 66 | static $stamps = array(); |
||
| 67 | if (!isset($stamps[$stamp])) { |
||
| 68 | $stamps[$stamp] = new Date($stamp); |
||
| 69 | } |
||
| 70 | return $stamps[$stamp]; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns a numeric year given a iso-8601 datetime |
||
| 75 | * |
||
| 76 | * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
||
| 77 | * |
||
| 78 | * @return int year (e.g. 2003) |
||
| 79 | * @access protected |
||
| 80 | */ |
||
| 81 | function stampToYear($stamp) |
||
| 82 | { |
||
| 83 | $date = PearDate::stampCollection($stamp); |
||
| 84 | return (int)$date->year; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a numeric month given a iso-8601 datetime |
||
| 89 | * |
||
| 90 | * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
||
| 91 | * |
||
| 92 | * @return int month (e.g. 9) |
||
| 93 | * @access protected |
||
| 94 | */ |
||
| 95 | function stampToMonth($stamp) |
||
| 96 | { |
||
| 97 | $date = PearDate::stampCollection($stamp); |
||
| 98 | return (int)$date->month; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns a numeric day given a iso-8601 datetime |
||
| 103 | * |
||
| 104 | * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
||
| 105 | * |
||
| 106 | * @return int day (e.g. 15) |
||
| 107 | * @access protected |
||
| 108 | */ |
||
| 109 | function stampToDay($stamp) |
||
| 110 | { |
||
| 111 | $date = PearDate::stampCollection($stamp); |
||
| 112 | return (int)$date->day; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns a numeric hour given a iso-8601 datetime |
||
| 117 | * |
||
| 118 | * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
||
| 119 | * |
||
| 120 | * @return int hour (e.g. 13) |
||
| 121 | * @access protected |
||
| 122 | */ |
||
| 123 | function stampToHour($stamp) |
||
| 124 | { |
||
| 125 | $date = PearDate::stampCollection($stamp); |
||
| 126 | return (int)$date->hour; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns a numeric minute given a iso-8601 datetime |
||
| 131 | * |
||
| 132 | * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
||
| 133 | * |
||
| 134 | * @return int minute (e.g. 34) |
||
| 135 | * @access protected |
||
| 136 | */ |
||
| 137 | function stampToMinute($stamp) |
||
| 138 | { |
||
| 139 | $date = PearDate::stampCollection($stamp); |
||
| 140 | return (int)$date->minute; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Returns a numeric second given a iso-8601 datetime |
||
| 145 | * |
||
| 146 | * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS) |
||
| 147 | * |
||
| 148 | * @return int second (e.g. 51) |
||
| 149 | * @access protected |
||
| 150 | */ |
||
| 151 | function stampToSecond($stamp) |
||
| 152 | { |
||
| 153 | $date = PearDate::stampCollection($stamp); |
||
| 154 | return (int)$date->second; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Returns a iso-8601 datetime |
||
| 159 | * |
||
| 160 | * @param int $y year (2003) |
||
| 161 | * @param int $m month (9) |
||
| 162 | * @param int $d day (13) |
||
| 163 | * @param int $h hour (13) |
||
| 164 | * @param int $i minute (34) |
||
| 165 | * @param int $s second (53) |
||
| 166 | * |
||
| 167 | * @return string iso-8601 datetime |
||
| 168 | * @access protected |
||
| 169 | */ |
||
| 170 | function dateToStamp($y, $m, $d, $h=0, $i=0, $s=0) |
||
| 171 | { |
||
| 172 | $r = array(); |
||
| 173 | PearDate::adjustDate($y, $m, $d, $h, $i, $s); |
||
| 174 | $key = $y.$m.$d.$h.$i.$s; |
||
| 175 | if (!isset($r[$key])) { |
||
| 176 | $r[$key] = sprintf("%04d-%02d-%02d %02d:%02d:%02d", |
||
| 177 | $y, $m, $d, $h, $i, $s); |
||
| 178 | } |
||
| 179 | return $r[$key]; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set the correct date values (useful for math operations on dates) |
||
| 184 | * |
||
| 185 | * @param int &$y year (2003) |
||
| 186 | * @param int &$m month (9) |
||
| 187 | * @param int &$d day (13) |
||
| 188 | * @param int &$h hour (13) |
||
| 189 | * @param int &$i minute (34) |
||
| 190 | * @param int &$s second (53) |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | * @access protected |
||
| 194 | */ |
||
| 195 | function adjustDate(&$y, &$m, &$d, &$h, &$i, &$s) |
||
| 196 | { |
||
| 197 | if ($s < 0) { |
||
| 198 | $m -= floor($s / 60); |
||
| 199 | $s = -$s % 60; |
||
| 200 | } |
||
| 201 | if ($s > 60) { |
||
| 202 | $m += floor($s / 60); |
||
| 203 | $s %= 60; |
||
| 204 | } |
||
| 205 | if ($i < 0) { |
||
| 206 | $h -= floor($i / 60); |
||
| 207 | $i = -$i % 60; |
||
| 208 | } |
||
| 209 | if ($i > 60) { |
||
| 210 | $h += floor($i / 60); |
||
| 211 | $i %= 60; |
||
| 212 | } |
||
| 213 | if ($h < 0) { |
||
| 214 | $d -= floor($h / 24); |
||
| 215 | $h = -$h % 24; |
||
| 216 | } |
||
| 217 | if ($h > 24) { |
||
| 218 | $d += floor($h / 24); |
||
| 219 | $h %= 24; |
||
| 220 | } |
||
| 221 | for(; $m < 1; $y--, $m+=12); |
||
| 222 | for(; $m > 12; $y++, $m-=12); |
||
| 223 | |||
| 224 | while ($d < 1) { |
||
| 225 | if ($m > 1) { |
||
| 226 | $m--; |
||
| 227 | } else { |
||
| 228 | $m = 12; |
||
| 229 | $y--; |
||
| 230 | } |
||
| 231 | $d += Date_Calc::daysInMonth($m, $y); |
||
| 232 | } |
||
| 233 | for ($max_days = Date_Calc::daysInMonth($m, $y); $d > $max_days; ) { |
||
| 234 | $d -= $max_days; |
||
| 235 | if ($m < 12) { |
||
| 236 | $m++; |
||
| 237 | } else { |
||
| 238 | $m = 1; |
||
| 239 | $y++; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * The upper limit on years that the Calendar Engine can work with |
||
| 246 | * |
||
| 247 | * @return int 9999 |
||
| 248 | * @access protected |
||
| 249 | */ |
||
| 250 | function getMaxYears() |
||
| 251 | { |
||
| 252 | return 9999; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * The lower limit on years that the Calendar Engine can work with |
||
| 257 | * |
||
| 258 | * @return int 0 |
||
| 259 | * @access protected |
||
| 260 | */ |
||
| 261 | function getMinYears() |
||
| 262 | { |
||
| 263 | return 0; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the number of months in a year |
||
| 268 | * |
||
| 269 | * @param int $y year |
||
| 270 | * |
||
| 271 | * @return int (12) |
||
| 272 | * @access protected |
||
| 273 | */ |
||
| 274 | function getMonthsInYear($y=null) |
||
| 275 | { |
||
| 276 | return 12; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns the number of days in a month, given year and month |
||
| 281 | * |
||
| 282 | * @param int $y year (2003) |
||
| 283 | * @param int $m month (9) |
||
| 284 | * |
||
| 285 | * @return int days in month |
||
| 286 | * @access protected |
||
| 287 | */ |
||
| 288 | function getDaysInMonth($y, $m) |
||
| 289 | { |
||
| 290 | return (int)Date_Calc::daysInMonth($m, $y); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Returns numeric representation of the day of the week in a month, |
||
| 295 | * given year and month |
||
| 296 | * |
||
| 297 | * @param int $y year (2003) |
||
| 298 | * @param int $m month (9) |
||
| 299 | * |
||
| 300 | * @return int from 0 to 7 |
||
| 301 | * @access protected |
||
| 302 | */ |
||
| 303 | function getFirstDayInMonth($y, $m) |
||
| 304 | { |
||
| 305 | return (int)Date_Calc::dayOfWeek(1, $m, $y); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the number of days in a week |
||
| 310 | * |
||
| 311 | * @param int $y year (2003) |
||
| 312 | * @param int $m month (9) |
||
| 313 | * @param int $d day (4) |
||
| 314 | * |
||
| 315 | * @return int (7) |
||
| 316 | * @access protected |
||
| 317 | */ |
||
| 318 | function getDaysInWeek($y=null, $m=null, $d=null) |
||
| 319 | { |
||
| 320 | return 7; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the number of the week in the year (ISO-8601), given a date |
||
| 325 | * |
||
| 326 | * @param int $y year (2003) |
||
| 327 | * @param int $m month (9) |
||
| 328 | * @param int $d day (4) |
||
| 329 | * |
||
| 330 | * @return int week number |
||
| 331 | * @access protected |
||
| 332 | */ |
||
| 333 | function getWeekNInYear($y, $m, $d) |
||
| 334 | { |
||
| 335 | //return Date_Calc::weekOfYear($d, $m, $y); //beware, Date_Calc doesn't follow ISO-8601 standard! |
||
| 336 | list($nYear, $nWeek) = Date_Calc::weekOfYear4th($d, $m, $y); |
||
| 337 | return $nWeek; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the number of the week in the month, given a date |
||
| 342 | * |
||
| 343 | * @param int $y year (2003) |
||
| 344 | * @param int $m month (9) |
||
| 345 | * @param int $d day (4) |
||
| 346 | * @param int $firstDay first day of the week (default: monday) |
||
| 347 | * |
||
| 348 | * @return int week number |
||
| 349 | * @access protected |
||
| 350 | */ |
||
| 351 | function getWeekNInMonth($y, $m, $d, $firstDay=1) |
||
| 352 | { |
||
| 353 | $weekEnd = ($firstDay == 0) ? $this->getDaysInWeek()-1 : $firstDay-1; |
||
| 354 | $end_of_week = (int)Date_Calc::nextDayOfWeek($weekEnd, 1, $m, $y, '%e', true); |
||
| 355 | $w = 1; |
||
| 356 | while ($d > $end_of_week) { |
||
| 357 | ++$w; |
||
| 358 | $end_of_week += $this->getDaysInWeek(); |
||
| 359 | } |
||
| 360 | return $w; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the number of weeks in the month |
||
| 365 | * |
||
| 366 | * @param int $y year (2003) |
||
| 367 | * @param int $m month (9) |
||
| 368 | * @param int $firstDay first day of the week (default: monday) |
||
| 369 | * |
||
| 370 | * @return int weeks number |
||
| 371 | * @access protected |
||
| 372 | */ |
||
| 373 | function getWeeksInMonth($y, $m, $firstDay=1) |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns the number of the day of the week (0=sunday, 1=monday...) |
||
| 393 | * |
||
| 394 | * @param int $y year (2003) |
||
| 395 | * @param int $m month (9) |
||
| 396 | * @param int $d day (4) |
||
| 397 | * |
||
| 398 | * @return int weekday number |
||
| 399 | * @access protected |
||
| 400 | */ |
||
| 401 | function getDayOfWeek($y, $m, $d) |
||
| 402 | { |
||
| 403 | return Date_Calc::dayOfWeek($d, $m, $y); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Returns a list of integer days of the week beginning 0 |
||
| 408 | * |
||
| 409 | * @param int $y year (2003) |
||
| 410 | * @param int $m month (9) |
||
| 411 | * @param int $d day (4) |
||
| 412 | * |
||
| 413 | * @return array (0, 1, 2, 3, 4, 5, 6) 1 = Monday |
||
| 414 | * @access protected |
||
| 415 | */ |
||
| 416 | function getWeekDays($y=null, $m=null, $d=null) |
||
| 417 | { |
||
| 418 | return array(0, 1, 2, 3, 4, 5, 6); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the default first day of the week |
||
| 423 | * |
||
| 424 | * @param int $y year (2003) |
||
| 425 | * @param int $m month (9) |
||
| 426 | * @param int $d day (4) |
||
| 427 | * |
||
| 428 | * @return int (default 1 = Monday) |
||
| 429 | * @access protected |
||
| 430 | */ |
||
| 431 | function getFirstDayOfWeek($y=null, $m=null, $d=null) |
||
| 432 | { |
||
| 433 | return 1; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Returns the number of hours in a day |
||
| 438 | * |
||
| 439 | * @param int $y year (2003) |
||
| 440 | * @param int $m month (9) |
||
| 441 | * @param int $d day (4) |
||
| 442 | * |
||
| 443 | * @return int (24) |
||
| 444 | * @access protected |
||
| 445 | */ |
||
| 446 | function getHoursInDay($y=null,$m=null,$d=null) |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns the number of minutes in an hour |
||
| 453 | * |
||
| 454 | * @param int $y year (2003) |
||
| 455 | * @param int $m month (9) |
||
| 456 | * @param int $d day (4) |
||
| 457 | * @param int $h hour |
||
| 458 | * |
||
| 459 | * @return int (60) |
||
| 460 | * @access protected |
||
| 461 | */ |
||
| 462 | function getMinutesInHour($y=null,$m=null,$d=null,$h=null) |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Returns the number of seconds in a minutes |
||
| 469 | * |
||
| 470 | * @param int $y year (2003) |
||
| 471 | * @param int $m month (9) |
||
| 472 | * @param int $d day (4) |
||
| 473 | * @param int $h hour |
||
| 474 | * @param int $i minute |
||
| 475 | * |
||
| 476 | * @return int (60) |
||
| 477 | * @access protected |
||
| 478 | */ |
||
| 479 | function getSecondsInMinute($y=null,$m=null,$d=null,$h=null,$i=null) |
||
| 480 | { |
||
| 481 | return 60; |
||
| 482 | } |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Checks if the given day is the current day |
||
| 486 | * |
||
| 487 | * @param mixed $stamp Any timestamp format recognized by Pear::Date |
||
| 488 | * |
||
| 489 | * @return boolean |
||
| 490 | * @access protected |
||
| 491 | */ |
||
| 492 | function isToday($stamp) |
||
| 502 | ); |
||
| 503 | } |
||
| 504 | } |
||
| 505 |
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.