Total Complexity | 49 |
Total Lines | 617 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Calendar 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
122 | class Calendar |
||
123 | { |
||
124 | /** |
||
125 | * Instance of class implementing calendar engine interface. |
||
126 | * |
||
127 | * @var object |
||
128 | */ |
||
129 | public $cE; |
||
130 | |||
131 | /** |
||
132 | * Instance of Calendar_Validator (lazy initialized when isValid() or |
||
133 | * getValidor() is called. |
||
134 | * |
||
135 | * @var Calendar_Validator |
||
136 | */ |
||
137 | public $validator; |
||
138 | |||
139 | /** |
||
140 | * Year for this calendar object e.g. 2003. |
||
141 | * |
||
142 | * @var int |
||
143 | */ |
||
144 | public $year; |
||
145 | |||
146 | /** |
||
147 | * Month for this calendar object e.g. 9. |
||
148 | * |
||
149 | * @var int |
||
150 | */ |
||
151 | public $month; |
||
152 | |||
153 | /** |
||
154 | * Day of month for this calendar object e.g. 23. |
||
155 | * |
||
156 | * @var int |
||
157 | */ |
||
158 | public $day; |
||
159 | |||
160 | /** |
||
161 | * Hour of day for this calendar object e.g. 13. |
||
162 | * |
||
163 | * @var int |
||
164 | */ |
||
165 | public $hour; |
||
166 | |||
167 | /** |
||
168 | * Minute of hour this calendar object e.g. 46. |
||
169 | * |
||
170 | * @var int |
||
171 | */ |
||
172 | public $minute; |
||
173 | |||
174 | /** |
||
175 | * Second of minute this calendar object e.g. 34. |
||
176 | * |
||
177 | * @var int |
||
178 | */ |
||
179 | public $second; |
||
180 | |||
181 | /** |
||
182 | * Marks this calendar object as selected (e.g. 'today'). |
||
183 | * |
||
184 | * @var bool |
||
185 | */ |
||
186 | public $selected = false; |
||
187 | |||
188 | /** |
||
189 | * Collection of child calendar objects created from subclasses |
||
190 | * of Calendar. Type depends on the object which created them. |
||
191 | * |
||
192 | * @var array |
||
193 | */ |
||
194 | public $children = []; |
||
195 | |||
196 | /** |
||
197 | * Constructs the Calendar. |
||
198 | * |
||
199 | * @param int $y year |
||
200 | * @param int $m month |
||
201 | * @param int $d day |
||
202 | * @param int $h hour |
||
203 | * @param int $i minute |
||
204 | * @param int $s second |
||
205 | */ |
||
206 | public function __construct($y = 2000, $m = 1, $d = 1, $h = 0, $i = 0, $s = 0) |
||
207 | { |
||
208 | static $cE = null; |
||
209 | if (!isset($cE)) { |
||
210 | $cE = Calendar_Engine_Factory::getEngine(); |
||
211 | } |
||
212 | $this->cE = &$cE; |
||
213 | $this->year = (int)$y; |
||
214 | $this->month = (int)$m; |
||
215 | $this->day = (int)$d; |
||
216 | $this->hour = (int)$h; |
||
217 | $this->minute = (int)$i; |
||
218 | $this->second = (int)$s; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Defines the calendar by a timestamp (Unix or ISO-8601), replacing values |
||
223 | * passed to the constructor. |
||
224 | * |
||
225 | * @param int|string $ts Unix or ISO-8601 timestamp |
||
226 | */ |
||
227 | public function setTimestamp($ts) |
||
228 | { |
||
229 | $this->year = $this->cE->stampToYear($ts); |
||
230 | $this->month = $this->cE->stampToMonth($ts); |
||
231 | $this->day = $this->cE->stampToDay($ts); |
||
232 | $this->hour = $this->cE->stampToHour($ts); |
||
233 | $this->minute = $this->cE->stampToMinute($ts); |
||
234 | $this->second = $this->cE->stampToSecond($ts); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Returns a timestamp from the current date / time values. Format of |
||
239 | * timestamp depends on Calendar_Engine implementation being used. |
||
240 | * |
||
241 | * @return int|string timestamp |
||
242 | */ |
||
243 | public function getTimestamp() |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Defines calendar object as selected (e.g. for today). |
||
250 | * |
||
251 | * @param bool $state whether Calendar subclass |
||
252 | */ |
||
253 | public function setSelected($state = true) |
||
254 | { |
||
255 | $this->selected = $state; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * True if the calendar subclass object is selected (e.g. today). |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isSelected() |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Checks if the current Calendar object is today's date. |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function isToday() |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Adjusts the date (helper method). |
||
280 | */ |
||
281 | public function adjust() |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Returns the date as an associative array (helper method). |
||
294 | * |
||
295 | * @param mixed $stamp timestamp (leave empty for current timestamp) |
||
296 | * |
||
297 | * @return array |
||
298 | */ |
||
299 | public function toArray($stamp = null) |
||
300 | { |
||
301 | if (null === $stamp) { |
||
302 | $stamp = $this->getTimestamp(); |
||
303 | } |
||
304 | |||
305 | return [ |
||
306 | 'year' => $this->cE->stampToYear($stamp), |
||
307 | 'month' => $this->cE->stampToMonth($stamp), |
||
308 | 'day' => $this->cE->stampToDay($stamp), |
||
309 | 'hour' => $this->cE->stampToHour($stamp), |
||
310 | 'minute' => $this->cE->stampToMinute($stamp), |
||
311 | 'second' => $this->cE->stampToSecond($stamp), |
||
312 | ]; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Returns the value as an associative array (helper method). |
||
317 | * |
||
318 | * @param string $returnType type of date object that return value represents |
||
319 | * @param string $format ['int' | 'array' | 'timestamp' | 'object'] |
||
320 | * @param mixed $stamp timestamp (depending on Calendar engine being used) |
||
321 | * @param int $default default value (i.e. give me the answer quick) |
||
322 | * |
||
323 | * @return mixed |
||
324 | */ |
||
325 | public function returnValue($returnType, $format, $stamp, $default) |
||
342 | } |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Abstract method for building the children of a calendar object. |
||
347 | * Implemented by Calendar subclasses. |
||
348 | * |
||
349 | * @param array $sDates array containing Calendar objects to select (optional) |
||
350 | * |
||
351 | * @return bool |
||
352 | * @abstract |
||
353 | */ |
||
354 | public function build($sDates = []) |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Abstract method for selected data objects called from build. |
||
364 | * |
||
365 | * @param array $sDates array of Calendar objects to select |
||
366 | * |
||
367 | * @return bool |
||
368 | * @abstract |
||
369 | */ |
||
370 | public function setSelection($sDates) |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Iterator method for fetching child Calendar subclass objects |
||
380 | * (e.g. a minute from an hour object). On reaching the end of |
||
381 | * the collection, returns false and resets the collection for |
||
382 | * further iteratations. |
||
383 | * |
||
384 | * @return mixed either an object subclass of Calendar or false |
||
385 | */ |
||
386 | public function fetch() |
||
387 | { |
||
388 | // $child = each($this->children); |
||
389 | $key = key($this->children); |
||
390 | $child = (null === $key) ? false : [$key, current($this->children), 'key' => $key, 'value' => current($this->children)]; |
||
391 | next($this->children); |
||
392 | |||
393 | if ($child) { |
||
394 | return $child['value']; |
||
395 | } |
||
396 | reset($this->children); |
||
397 | |||
398 | return false; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Fetches all child from the current collection of children. |
||
403 | * |
||
404 | * @return array |
||
405 | */ |
||
406 | public function fetchAll() |
||
407 | { |
||
408 | return $this->children; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Get the number Calendar subclass objects stored in the internal collection. |
||
413 | * |
||
414 | * @return int |
||
415 | */ |
||
416 | public function size() |
||
417 | { |
||
418 | return count($this->children); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Determine whether this date is valid, with the bounds determined by |
||
423 | * the Calendar_Engine. The call is passed on to Calendar_Validator::isValid. |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | public function isValid() |
||
428 | { |
||
429 | $validator = $this->getValidator(); |
||
430 | |||
431 | return $validator->isValid(); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Returns an instance of Calendar_Validator. |
||
436 | * |
||
437 | * @return Calendar_Validator |
||
438 | */ |
||
439 | public function &getValidator() |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Returns a reference to the current Calendar_Engine being used. Useful |
||
451 | * for Calendar_Table_Helper and Calendar_Validator. |
||
452 | * |
||
453 | * @return object implementing Calendar_Engine_Inteface |
||
454 | */ |
||
455 | public function &getEngine() |
||
456 | { |
||
457 | return $this->cE; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * Set the CALENDAR_FIRST_DAY_OF_WEEK constant to the $firstDay value |
||
462 | * if the constant is not set yet. |
||
463 | * |
||
464 | * @param int $firstDay first day of the week (0=sunday, 1=monday, ...) |
||
465 | * |
||
466 | * @return int |
||
467 | * @throws E_USER_WARNING this method throws a WARNING if the |
||
468 | * CALENDAR_FIRST_DAY_OF_WEEK constant is already defined and |
||
469 | * the $firstDay parameter is set to a different value |
||
470 | */ |
||
471 | public function defineFirstDayOfWeek($firstDay = null) |
||
472 | { |
||
473 | if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) { |
||
474 | if ((CALENDAR_FIRST_DAY_OF_WEEK != $firstDay) && null !== $firstDay) { |
||
475 | $msg = 'CALENDAR_FIRST_DAY_OF_WEEK constant already defined.' . ' The $firstDay parameter will be ignored.'; |
||
476 | trigger_error($msg, E_USER_WARNING); |
||
477 | } |
||
478 | |||
479 | return CALENDAR_FIRST_DAY_OF_WEEK; |
||
480 | } |
||
481 | if (null === $firstDay) { |
||
482 | $firstDay = $this->cE->getFirstDayOfWeek($this->thisYear(), $this->thisMonth(), $this->thisDay()); |
||
483 | } |
||
484 | define('CALENDAR_FIRST_DAY_OF_WEEK', $firstDay); |
||
485 | |||
486 | return CALENDAR_FIRST_DAY_OF_WEEK; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Returns the value for the previous year. |
||
491 | * |
||
492 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
493 | * |
||
494 | * @return int e.g. 2002 or timestamp |
||
495 | */ |
||
496 | public function prevYear($format = 'int') |
||
497 | { |
||
498 | $ts = $this->cE->dateToStamp($this->year - 1, 1, 1, 0, 0, 0); |
||
499 | |||
500 | return $this->returnValue('Year', $format, $ts, $this->year - 1); |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Returns the value for this year. |
||
505 | * |
||
506 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
507 | * |
||
508 | * @return int e.g. 2003 or timestamp |
||
509 | */ |
||
510 | public function thisYear($format = 'int') |
||
511 | { |
||
512 | $ts = $this->cE->dateToStamp($this->year, 1, 1, 0, 0, 0); |
||
513 | |||
514 | return $this->returnValue('Year', $format, $ts, $this->year); |
||
515 | } |
||
516 | |||
517 | /** |
||
518 | * Returns the value for next year. |
||
519 | * |
||
520 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
521 | * |
||
522 | * @return int e.g. 2004 or timestamp |
||
523 | */ |
||
524 | public function nextYear($format = 'int') |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Returns the value for the previous month. |
||
533 | * |
||
534 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
535 | * |
||
536 | * @return int e.g. 4 or Unix timestamp |
||
537 | */ |
||
538 | public function prevMonth($format = 'int') |
||
539 | { |
||
540 | $ts = $this->cE->dateToStamp($this->year, $this->month - 1, 1, 0, 0, 0); |
||
541 | |||
542 | return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts)); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Returns the value for this month. |
||
547 | * |
||
548 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
549 | * |
||
550 | * @return int e.g. 5 or timestamp |
||
551 | */ |
||
552 | public function thisMonth($format = 'int') |
||
553 | { |
||
554 | $ts = $this->cE->dateToStamp($this->year, $this->month, 1, 0, 0, 0); |
||
555 | |||
556 | return $this->returnValue('Month', $format, $ts, $this->month); |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Returns the value for next month. |
||
561 | * |
||
562 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
563 | * |
||
564 | * @return int e.g. 6 or timestamp |
||
565 | */ |
||
566 | public function nextMonth($format = 'int') |
||
567 | { |
||
568 | $ts = $this->cE->dateToStamp($this->year, $this->month + 1, 1, 0, 0, 0); |
||
569 | |||
570 | return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts)); |
||
571 | } |
||
572 | |||
573 | /** |
||
574 | * Returns the value for the previous day. |
||
575 | * |
||
576 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
577 | * |
||
578 | * @return int e.g. 10 or timestamp |
||
579 | */ |
||
580 | public function prevDay($format = 'int') |
||
581 | { |
||
582 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day - 1, 0, 0, 0); |
||
583 | |||
584 | return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts)); |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Returns the value for this day. |
||
589 | * |
||
590 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
591 | * |
||
592 | * @return int e.g. 11 or timestamp |
||
593 | */ |
||
594 | public function thisDay($format = 'int') |
||
595 | { |
||
596 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, 0, 0, 0); |
||
597 | |||
598 | return $this->returnValue('Day', $format, $ts, $this->day); |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * Returns the value for the next day. |
||
603 | * |
||
604 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
605 | * |
||
606 | * @return int e.g. 12 or timestamp |
||
607 | */ |
||
608 | public function nextDay($format = 'int') |
||
609 | { |
||
610 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day + 1, 0, 0, 0); |
||
611 | |||
612 | return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts)); |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Returns the value for the previous hour. |
||
617 | * |
||
618 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
619 | * |
||
620 | * @return int e.g. 13 or timestamp |
||
621 | */ |
||
622 | public function prevHour($format = 'int') |
||
623 | { |
||
624 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour - 1, 0, 0); |
||
625 | |||
626 | return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts)); |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * Returns the value for this hour. |
||
631 | * |
||
632 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
633 | * |
||
634 | * @return int e.g. 14 or timestamp |
||
635 | */ |
||
636 | public function thisHour($format = 'int') |
||
637 | { |
||
638 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, 0, 0); |
||
639 | |||
640 | return $this->returnValue('Hour', $format, $ts, $this->hour); |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * Returns the value for the next hour. |
||
645 | * |
||
646 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
647 | * |
||
648 | * @return int e.g. 14 or timestamp |
||
649 | */ |
||
650 | public function nextHour($format = 'int') |
||
651 | { |
||
652 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour + 1, 0, 0); |
||
653 | |||
654 | return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts)); |
||
655 | } |
||
656 | |||
657 | /** |
||
658 | * Returns the value for the previous minute. |
||
659 | * |
||
660 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
661 | * |
||
662 | * @return int e.g. 23 or timestamp |
||
663 | */ |
||
664 | public function prevMinute($format = 'int') |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * Returns the value for this minute. |
||
673 | * |
||
674 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
675 | * |
||
676 | * @return int e.g. 24 or timestamp |
||
677 | */ |
||
678 | public function thisMinute($format = 'int') |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * Returns the value for the next minute. |
||
687 | * |
||
688 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
689 | * |
||
690 | * @return int e.g. 25 or timestamp |
||
691 | */ |
||
692 | public function nextMinute($format = 'int') |
||
693 | { |
||
694 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, $this->minute + 1, 0); |
||
695 | |||
696 | return $this->returnValue('Minute', $format, $ts, $this->cE->stampToMinute($ts)); |
||
697 | } |
||
698 | |||
699 | /** |
||
700 | * Returns the value for the previous second. |
||
701 | * |
||
702 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
703 | * |
||
704 | * @return int e.g. 43 or timestamp |
||
705 | */ |
||
706 | public function prevSecond($format = 'int') |
||
707 | { |
||
708 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second - 1); |
||
709 | |||
710 | return $this->returnValue('Second', $format, $ts, $this->cE->stampToSecond($ts)); |
||
711 | } |
||
712 | |||
713 | /** |
||
714 | * Returns the value for this second. |
||
715 | * |
||
716 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
717 | * |
||
718 | * @return int e.g. 44 or timestamp |
||
719 | */ |
||
720 | public function thisSecond($format = 'int') |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * Returns the value for the next second. |
||
729 | * |
||
730 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
731 | * |
||
732 | * @return int e.g. 45 or timestamp |
||
733 | */ |
||
734 | public function nextSecond($format = 'int') |
||
739 | } |
||
740 | } |
||
741 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.