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