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