| Total Complexity | 51 |
| Total Lines | 583 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Calendar_Decorator 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_Decorator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | class Calendar_Decorator |
||
| 66 | { |
||
| 67 | /** |
||
| 68 | * Subclass of Calendar being decorated |
||
| 69 | * @var object |
||
| 70 | * @access private |
||
| 71 | */ |
||
| 72 | var $calendar; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Constructs the Calendar_Decorator |
||
| 76 | * |
||
| 77 | * @param object &$calendar subclass to Calendar to decorate |
||
| 78 | */ |
||
| 79 | function __construct(&$calendar) |
||
|
|
|||
| 80 | { |
||
| 81 | $this->calendar = & $calendar; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Defines the calendar by a Unix timestamp, replacing values |
||
| 86 | * passed to the constructor |
||
| 87 | * |
||
| 88 | * @param int $ts Unix timestamp |
||
| 89 | * |
||
| 90 | * @return void |
||
| 91 | * @access public |
||
| 92 | */ |
||
| 93 | function setTimestamp($ts) |
||
| 94 | { |
||
| 95 | $this->calendar->setTimestamp($ts); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns a timestamp from the current date / time values. Format of |
||
| 100 | * timestamp depends on Calendar_Engine implementation being used |
||
| 101 | * |
||
| 102 | * @return int $ts timestamp |
||
| 103 | * @access public |
||
| 104 | */ |
||
| 105 | function getTimestamp() |
||
| 106 | { |
||
| 107 | return $this->calendar->getTimeStamp(); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Defines calendar object as selected (e.g. for today) |
||
| 112 | * |
||
| 113 | * @param boolean $state whether Calendar subclass must be selected |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | * @access public |
||
| 117 | */ |
||
| 118 | function setSelected($state = true) |
||
| 119 | { |
||
| 120 | $this->calendar->setSelected($state = true); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * True if the calendar subclass object is selected (e.g. today) |
||
| 125 | * |
||
| 126 | * @return boolean |
||
| 127 | * @access public |
||
| 128 | */ |
||
| 129 | function isSelected() |
||
| 130 | { |
||
| 131 | return $this->calendar->isSelected(); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Adjusts the date (helper method) |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | * @access public |
||
| 139 | */ |
||
| 140 | function adjust() |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the date as an associative array (helper method) |
||
| 147 | * |
||
| 148 | * @param mixed $stamp timestamp (leave empty for current timestamp) |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | * @access public |
||
| 152 | */ |
||
| 153 | function toArray($stamp = null) |
||
| 154 | { |
||
| 155 | return $this->calendar->toArray($stamp); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns the value as an associative array (helper method) |
||
| 160 | * |
||
| 161 | * @param string $returnType type of date object that return value represents |
||
| 162 | * @param string $format ['int'|'timestamp'|'object'|'array'] |
||
| 163 | * @param mixed $stamp timestamp (depending on Calendar engine being used) |
||
| 164 | * @param integer $default default value (i.e. give me the answer quick) |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | * @access private |
||
| 168 | */ |
||
| 169 | function returnValue($returnType, $format, $stamp, $default) |
||
| 170 | { |
||
| 171 | return $this->calendar->returnValue($returnType, $format, $stamp, $default); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Defines Day object as first in a week |
||
| 176 | * Only used by Calendar_Month_Weekdays::build() |
||
| 177 | * |
||
| 178 | * @param boolean $state whether it's first or not |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | * @access private |
||
| 182 | */ |
||
| 183 | function setFirst($state = true) |
||
| 184 | { |
||
| 185 | if (method_exists($this->calendar, 'setFirst')) { |
||
| 186 | $this->calendar->setFirst($state); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Defines Day object as last in a week |
||
| 192 | * Used only following Calendar_Month_Weekdays::build() |
||
| 193 | * |
||
| 194 | * @param boolean $state whether it's last or not |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | * @access private |
||
| 198 | */ |
||
| 199 | function setLast($state = true) |
||
| 200 | { |
||
| 201 | if (method_exists($this->calendar, 'setLast')) { |
||
| 202 | $this->calendar->setLast($state); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns true if Day object is first in a Week |
||
| 208 | * Only relevant when Day is created by Calendar_Month_Weekdays::build() |
||
| 209 | * |
||
| 210 | * @return boolean |
||
| 211 | * @access public |
||
| 212 | */ |
||
| 213 | function isFirst() |
||
| 214 | { |
||
| 215 | if (method_exists($this->calendar, 'isFirst')) { |
||
| 216 | return $this->calendar->isFirst(); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns true if Day object is last in a Week |
||
| 222 | * Only relevant when Day is created by Calendar_Month_Weekdays::build() |
||
| 223 | * |
||
| 224 | * @return boolean |
||
| 225 | * @access public |
||
| 226 | */ |
||
| 227 | function isLast() |
||
| 228 | { |
||
| 229 | if (method_exists($this->calendar, 'isLast')) { |
||
| 230 | return $this->calendar->isLast(); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Defines Day object as empty |
||
| 236 | * Only used by Calendar_Month_Weekdays::build() |
||
| 237 | * |
||
| 238 | * @param boolean $state whether it's empty or not |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | * @access private |
||
| 242 | */ |
||
| 243 | function setEmpty ($state = true) |
||
| 244 | { |
||
| 245 | if (method_exists($this->calendar, 'setEmpty')) { |
||
| 246 | $this->calendar->setEmpty($state); |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Check if the current object is empty |
||
| 252 | * |
||
| 253 | * @return boolean |
||
| 254 | * @access public |
||
| 255 | */ |
||
| 256 | function isEmpty() |
||
| 257 | { |
||
| 258 | if (method_exists($this->calendar, 'isEmpty')) { |
||
| 259 | return $this->calendar->isEmpty(); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Build the children |
||
| 265 | * |
||
| 266 | * @param array $sDates array containing Calendar objects to select (optional) |
||
| 267 | * |
||
| 268 | * @return boolean |
||
| 269 | * @access public |
||
| 270 | * @abstract |
||
| 271 | */ |
||
| 272 | function build($sDates = array()) |
||
| 273 | { |
||
| 274 | $this->calendar->build($sDates); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Iterator method for fetching child Calendar subclass objects |
||
| 279 | * (e.g. a minute from an hour object). On reaching the end of |
||
| 280 | * the collection, returns false and resets the collection for |
||
| 281 | * further iteratations. |
||
| 282 | * |
||
| 283 | * @return mixed either an object subclass of Calendar or false |
||
| 284 | * @access public |
||
| 285 | */ |
||
| 286 | function fetch() |
||
| 287 | { |
||
| 288 | return $this->calendar->fetch(); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Fetches all child from the current collection of children |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | * @access public |
||
| 296 | */ |
||
| 297 | function fetchAll() |
||
| 298 | { |
||
| 299 | return $this->calendar->fetchAll(); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the number Calendar subclass objects stored in the internal collection |
||
| 304 | * |
||
| 305 | * @return int |
||
| 306 | * @access public |
||
| 307 | */ |
||
| 308 | function size() |
||
| 309 | { |
||
| 310 | return $this->calendar->size(); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Determine whether this date is valid, with the bounds determined by |
||
| 315 | * the Calendar_Engine. The call is passed on to Calendar_Validator::isValid |
||
| 316 | * |
||
| 317 | * @return boolean |
||
| 318 | * @access public |
||
| 319 | */ |
||
| 320 | function isValid() |
||
| 321 | { |
||
| 322 | return $this->calendar->isValid(); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Returns an instance of Calendar_Validator |
||
| 327 | * |
||
| 328 | * @return Calendar_Validator |
||
| 329 | * @access public |
||
| 330 | */ |
||
| 331 | function & getValidator() |
||
| 332 | { |
||
| 333 | $validator = $this->calendar->getValidator(); |
||
| 334 | return $validator; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns a reference to the current Calendar_Engine being used. Useful |
||
| 339 | * for Calendar_Table_Helper and Calendar_Validator |
||
| 340 | * |
||
| 341 | * @return object implementing Calendar_Engine_Inteface |
||
| 342 | * @access private |
||
| 343 | */ |
||
| 344 | function & getEngine() |
||
| 345 | { |
||
| 346 | $engine = $this->calendar->getEngine(); |
||
| 347 | return $engine; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Returns the value for the previous year |
||
| 352 | * |
||
| 353 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 354 | * |
||
| 355 | * @return int e.g. 2002 or timestamp |
||
| 356 | * @access public |
||
| 357 | */ |
||
| 358 | function prevYear($format = 'int') |
||
| 359 | { |
||
| 360 | return $this->calendar->prevYear($format); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns the value for this year |
||
| 365 | * |
||
| 366 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 367 | * |
||
| 368 | * @return int e.g. 2003 or timestamp |
||
| 369 | * @access public |
||
| 370 | */ |
||
| 371 | function thisYear($format = 'int') |
||
| 372 | { |
||
| 373 | return $this->calendar->thisYear($format); |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns the value for next year |
||
| 378 | * |
||
| 379 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 380 | * |
||
| 381 | * @return int e.g. 2004 or timestamp |
||
| 382 | * @access public |
||
| 383 | */ |
||
| 384 | function nextYear($format = 'int') |
||
| 385 | { |
||
| 386 | return $this->calendar->nextYear($format); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the value for the previous month |
||
| 391 | * |
||
| 392 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 393 | * |
||
| 394 | * @return int e.g. 4 or Unix timestamp |
||
| 395 | * @access public |
||
| 396 | */ |
||
| 397 | function prevMonth($format = 'int') |
||
| 398 | { |
||
| 399 | return $this->calendar->prevMonth($format); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns the value for this month |
||
| 404 | * |
||
| 405 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 406 | * |
||
| 407 | * @return int e.g. 5 or timestamp |
||
| 408 | * @access public |
||
| 409 | */ |
||
| 410 | function thisMonth($format = 'int') |
||
| 411 | { |
||
| 412 | return $this->calendar->thisMonth($format); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns the value for next month |
||
| 417 | * |
||
| 418 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 419 | * |
||
| 420 | * @return int e.g. 6 or timestamp |
||
| 421 | * @access public |
||
| 422 | */ |
||
| 423 | function nextMonth($format = 'int') |
||
| 424 | { |
||
| 425 | return $this->calendar->nextMonth($format); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Returns the value for the previous week |
||
| 430 | * |
||
| 431 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 432 | * |
||
| 433 | * @return int e.g. 4 or Unix timestamp |
||
| 434 | * @access public |
||
| 435 | */ |
||
| 436 | function prevWeek($format = 'n_in_month') |
||
| 437 | { |
||
| 438 | if ( method_exists($this->calendar, 'prevWeek')) { |
||
| 439 | return $this->calendar->prevWeek($format); |
||
| 440 | } else { |
||
| 441 | include_once 'PEAR.php'; |
||
| 442 | PEAR::raiseError( |
||
| 443 | 'Cannot call prevWeek on Calendar object of type: '. |
||
| 444 | get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, |
||
| 445 | E_USER_NOTICE, 'Calendar_Decorator::prevWeek()'); |
||
| 446 | return false; |
||
| 447 | } |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Returns the value for this week |
||
| 452 | * |
||
| 453 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 454 | * |
||
| 455 | * @return int e.g. 5 or timestamp |
||
| 456 | * @access public |
||
| 457 | */ |
||
| 458 | function thisWeek($format = 'n_in_month') |
||
| 459 | { |
||
| 460 | if ( method_exists($this->calendar, 'thisWeek')) { |
||
| 461 | return $this->calendar->thisWeek($format); |
||
| 462 | } else { |
||
| 463 | include_once 'PEAR.php'; |
||
| 464 | PEAR::raiseError( |
||
| 465 | 'Cannot call thisWeek on Calendar object of type: '. |
||
| 466 | get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, |
||
| 467 | E_USER_NOTICE, 'Calendar_Decorator::thisWeek()'); |
||
| 468 | return false; |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Returns the value for next week |
||
| 474 | * |
||
| 475 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 476 | * |
||
| 477 | * @return int e.g. 6 or timestamp |
||
| 478 | * @access public |
||
| 479 | */ |
||
| 480 | function nextWeek($format = 'n_in_month') |
||
| 481 | { |
||
| 482 | if ( method_exists($this->calendar, 'nextWeek')) { |
||
| 483 | return $this->calendar->nextWeek($format); |
||
| 484 | } else { |
||
| 485 | include_once 'PEAR.php'; |
||
| 486 | PEAR::raiseError( |
||
| 487 | 'Cannot call thisWeek on Calendar object of type: '. |
||
| 488 | get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, |
||
| 489 | E_USER_NOTICE, 'Calendar_Decorator::nextWeek()'); |
||
| 490 | return false; |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Returns the value for the previous day |
||
| 496 | * |
||
| 497 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 498 | * |
||
| 499 | * @return int e.g. 10 or timestamp |
||
| 500 | * @access public |
||
| 501 | */ |
||
| 502 | function prevDay($format = 'int') |
||
| 503 | { |
||
| 504 | return $this->calendar->prevDay($format); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns the value for this day |
||
| 509 | * |
||
| 510 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 511 | * |
||
| 512 | * @return int e.g. 11 or timestamp |
||
| 513 | * @access public |
||
| 514 | */ |
||
| 515 | function thisDay($format = 'int') |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns the value for the next day |
||
| 522 | * |
||
| 523 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 524 | * |
||
| 525 | * @return int e.g. 12 or timestamp |
||
| 526 | * @access public |
||
| 527 | */ |
||
| 528 | function nextDay($format = 'int') |
||
| 529 | { |
||
| 530 | return $this->calendar->nextDay($format); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Returns the value for the previous hour |
||
| 535 | * |
||
| 536 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 537 | * |
||
| 538 | * @return int e.g. 13 or timestamp |
||
| 539 | * @access public |
||
| 540 | */ |
||
| 541 | function prevHour($format = 'int') |
||
| 542 | { |
||
| 543 | return $this->calendar->prevHour($format); |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the value for this hour |
||
| 548 | * |
||
| 549 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 550 | * |
||
| 551 | * @return int e.g. 14 or timestamp |
||
| 552 | * @access public |
||
| 553 | */ |
||
| 554 | function thisHour($format = 'int') |
||
| 555 | { |
||
| 556 | return $this->calendar->thisHour($format); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Returns the value for the next hour |
||
| 561 | * |
||
| 562 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 563 | * |
||
| 564 | * @return int e.g. 14 or timestamp |
||
| 565 | * @access public |
||
| 566 | */ |
||
| 567 | function nextHour($format = 'int') |
||
| 568 | { |
||
| 569 | return $this->calendar->nextHour($format); |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Returns the value for the previous minute |
||
| 574 | * |
||
| 575 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 576 | * |
||
| 577 | * @return int e.g. 23 or timestamp |
||
| 578 | * @access public |
||
| 579 | */ |
||
| 580 | function prevMinute($format = 'int') |
||
| 581 | { |
||
| 582 | return $this->calendar->prevMinute($format); |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Returns the value for this minute |
||
| 587 | * |
||
| 588 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 589 | * |
||
| 590 | * @return int e.g. 24 or timestamp |
||
| 591 | * @access public |
||
| 592 | */ |
||
| 593 | function thisMinute($format = 'int') |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Returns the value for the next minute |
||
| 600 | * |
||
| 601 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 602 | * |
||
| 603 | * @return int e.g. 25 or timestamp |
||
| 604 | * @access public |
||
| 605 | */ |
||
| 606 | function nextMinute($format = 'int') |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Returns the value for the previous second |
||
| 613 | * |
||
| 614 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 615 | * |
||
| 616 | * @return int e.g. 43 or timestamp |
||
| 617 | * @access public |
||
| 618 | */ |
||
| 619 | function prevSecond($format = 'int') |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Returns the value for this second |
||
| 626 | * |
||
| 627 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 628 | * |
||
| 629 | * @return int e.g. 44 or timestamp |
||
| 630 | * @access public |
||
| 631 | */ |
||
| 632 | function thisSecond($format = 'int') |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Returns the value for the next second |
||
| 639 | * |
||
| 640 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
| 641 | * |
||
| 642 | * @return int e.g. 45 or timestamp |
||
| 643 | * @access public |
||
| 644 | */ |
||
| 645 | function nextSecond($format = 'int') |
||
| 648 | } |
||
| 649 | } |
||
| 650 |
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.