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