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