Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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() |
||
244 | { |
||
245 | return $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second); |
||
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() |
||
264 | { |
||
265 | return $this->selected; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Checks if the current Calendar object is today's date. |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function isToday() |
||
274 | { |
||
275 | return $this->cE->isToday($this->getTimestamp()); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Adjusts the date (helper method). |
||
280 | */ |
||
281 | public function adjust() |
||
282 | { |
||
283 | $stamp = $this->getTimestamp(); |
||
284 | $this->year = $this->cE->stampToYear($stamp); |
||
285 | $this->month = $this->cE->stampToMonth($stamp); |
||
286 | $this->day = $this->cE->stampToDay($stamp); |
||
287 | $this->hour = $this->cE->stampToHour($stamp); |
||
288 | $this->minute = $this->cE->stampToMinute($stamp); |
||
289 | $this->second = $this->cE->stampToSecond($stamp); |
||
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 (is_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) |
||
326 | { |
||
327 | switch (strtolower($format)) { |
||
328 | case 'int': |
||
329 | return $default; |
||
330 | case 'array': |
||
331 | return $this->toArray($stamp); |
||
332 | break; |
||
333 | case 'object': |
||
334 | require_once CALENDAR_ROOT . 'Factory.php'; |
||
335 | |||
336 | return Calendar_Factory::createByTimestamp($returnType, $stamp); |
||
337 | break; |
||
338 | case 'timestamp': |
||
339 | default: |
||
340 | return $stamp; |
||
341 | break; |
||
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 = []) |
||
355 | { |
||
356 | require_once __DIR__ . '/PEAR.php'; |
||
357 | PEAR::raiseError('Calendar::build is abstract', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar::build()'); |
||
358 | |||
359 | return false; |
||
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) |
||
371 | { |
||
372 | require_once __DIR__ . '/PEAR.php'; |
||
373 | PEAR::raiseError('Calendar::setSelection is abstract', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar::setSelection()'); |
||
374 | |||
375 | return false; |
||
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 | if ($child) { |
||
390 | return $child['value']; |
||
391 | } else { |
||
392 | reset($this->children); |
||
393 | |||
394 | return false; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Fetches all child from the current collection of children. |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public function fetchAll() |
||
404 | { |
||
405 | return $this->children; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Get the number Calendar subclass objects stored in the internal collection. |
||
410 | * |
||
411 | * @return int |
||
412 | */ |
||
413 | public function size() |
||
414 | { |
||
415 | return count($this->children); |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Determine whether this date is valid, with the bounds determined by |
||
420 | * the Calendar_Engine. The call is passed on to Calendar_Validator::isValid. |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | public function isValid() |
||
425 | { |
||
426 | $validator = $this->getValidator(); |
||
427 | |||
428 | return $validator->isValid(); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Returns an instance of Calendar_Validator. |
||
433 | * |
||
434 | * @return Calendar_Validator |
||
435 | */ |
||
436 | public function &getValidator() |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * Returns a reference to the current Calendar_Engine being used. Useful |
||
448 | * for Calendar_Table_Helper and Calendar_Validator. |
||
449 | * |
||
450 | * @return object implementing Calendar_Engine_Inteface |
||
451 | */ |
||
452 | public function &getEngine() |
||
453 | { |
||
454 | return $this->cE; |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * Set the CALENDAR_FIRST_DAY_OF_WEEK constant to the $firstDay value |
||
459 | * if the constant is not set yet. |
||
460 | * |
||
461 | * @param int $firstDay first day of the week (0=sunday, 1=monday, ...) |
||
462 | * |
||
463 | * @return int |
||
464 | * |
||
465 | * @throws E_USER_WARNING this method throws a WARNING if the |
||
466 | * CALENDAR_FIRST_DAY_OF_WEEK constant is already defined and |
||
467 | * the $firstDay parameter is set to a different value |
||
468 | */ |
||
469 | public function defineFirstDayOfWeek($firstDay = null) |
||
470 | { |
||
471 | if (defined('CALENDAR_FIRST_DAY_OF_WEEK')) { |
||
472 | if ((CALENDAR_FIRST_DAY_OF_WEEK != $firstDay) && !is_null($firstDay)) { |
||
473 | $msg = 'CALENDAR_FIRST_DAY_OF_WEEK constant already defined.' . ' The $firstDay parameter will be ignored.'; |
||
474 | trigger_error($msg, E_USER_WARNING); |
||
475 | } |
||
476 | |||
477 | return CALENDAR_FIRST_DAY_OF_WEEK; |
||
478 | } |
||
479 | if (is_null($firstDay)) { |
||
480 | $firstDay = $this->cE->getFirstDayOfWeek($this->thisYear(), $this->thisMonth(), $this->thisDay()); |
||
481 | } |
||
482 | define('CALENDAR_FIRST_DAY_OF_WEEK', $firstDay); |
||
483 | |||
484 | return CALENDAR_FIRST_DAY_OF_WEEK; |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Returns the value for the previous year. |
||
489 | * |
||
490 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
491 | * |
||
492 | * @return int e.g. 2002 or timestamp |
||
493 | */ |
||
494 | public function prevYear($format = 'int') |
||
495 | { |
||
496 | $ts = $this->cE->dateToStamp($this->year - 1, 1, 1, 0, 0, 0); |
||
497 | |||
498 | return $this->returnValue('Year', $format, $ts, $this->year - 1); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * Returns the value for this year. |
||
503 | * |
||
504 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
505 | * |
||
506 | * @return int e.g. 2003 or timestamp |
||
507 | */ |
||
508 | public function thisYear($format = 'int') |
||
509 | { |
||
510 | $ts = $this->cE->dateToStamp($this->year, 1, 1, 0, 0, 0); |
||
511 | |||
512 | return $this->returnValue('Year', $format, $ts, $this->year); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Returns the value for next year. |
||
517 | * |
||
518 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
519 | * |
||
520 | * @return int e.g. 2004 or timestamp |
||
521 | */ |
||
522 | public function nextYear($format = 'int') |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Returns the value for the previous month. |
||
531 | * |
||
532 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
533 | * |
||
534 | * @return int e.g. 4 or Unix timestamp |
||
535 | */ |
||
536 | public function prevMonth($format = 'int') |
||
537 | { |
||
538 | $ts = $this->cE->dateToStamp($this->year, $this->month - 1, 1, 0, 0, 0); |
||
539 | |||
540 | return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts)); |
||
541 | } |
||
542 | |||
543 | /** |
||
544 | * Returns the value for this month. |
||
545 | * |
||
546 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
547 | * |
||
548 | * @return int e.g. 5 or timestamp |
||
549 | */ |
||
550 | public function thisMonth($format = 'int') |
||
551 | { |
||
552 | $ts = $this->cE->dateToStamp($this->year, $this->month, 1, 0, 0, 0); |
||
553 | |||
554 | return $this->returnValue('Month', $format, $ts, $this->month); |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Returns the value for next month. |
||
559 | * |
||
560 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
561 | * |
||
562 | * @return int e.g. 6 or timestamp |
||
563 | */ |
||
564 | public function nextMonth($format = 'int') |
||
565 | { |
||
566 | $ts = $this->cE->dateToStamp($this->year, $this->month + 1, 1, 0, 0, 0); |
||
567 | |||
568 | return $this->returnValue('Month', $format, $ts, $this->cE->stampToMonth($ts)); |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Returns the value for the previous day. |
||
573 | * |
||
574 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
575 | * |
||
576 | * @return int e.g. 10 or timestamp |
||
577 | */ |
||
578 | public function prevDay($format = 'int') |
||
579 | { |
||
580 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day - 1, 0, 0, 0); |
||
581 | |||
582 | return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts)); |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Returns the value for this day. |
||
587 | * |
||
588 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
589 | * |
||
590 | * @return int e.g. 11 or timestamp |
||
591 | */ |
||
592 | public function thisDay($format = 'int') |
||
593 | { |
||
594 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, 0, 0, 0); |
||
595 | |||
596 | return $this->returnValue('Day', $format, $ts, $this->day); |
||
597 | } |
||
598 | |||
599 | /** |
||
600 | * Returns the value for the next day. |
||
601 | * |
||
602 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
603 | * |
||
604 | * @return int e.g. 12 or timestamp |
||
605 | */ |
||
606 | public function nextDay($format = 'int') |
||
607 | { |
||
608 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day + 1, 0, 0, 0); |
||
609 | |||
610 | return $this->returnValue('Day', $format, $ts, $this->cE->stampToDay($ts)); |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Returns the value for the previous hour. |
||
615 | * |
||
616 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
617 | * |
||
618 | * @return int e.g. 13 or timestamp |
||
619 | */ |
||
620 | public function prevHour($format = 'int') |
||
621 | { |
||
622 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour - 1, 0, 0); |
||
623 | |||
624 | return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts)); |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Returns the value for this hour. |
||
629 | * |
||
630 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
631 | * |
||
632 | * @return int e.g. 14 or timestamp |
||
633 | */ |
||
634 | public function thisHour($format = 'int') |
||
635 | { |
||
636 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, 0, 0); |
||
637 | |||
638 | return $this->returnValue('Hour', $format, $ts, $this->hour); |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Returns the value for the next hour. |
||
643 | * |
||
644 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
645 | * |
||
646 | * @return int e.g. 14 or timestamp |
||
647 | */ |
||
648 | public function nextHour($format = 'int') |
||
649 | { |
||
650 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour + 1, 0, 0); |
||
651 | |||
652 | return $this->returnValue('Hour', $format, $ts, $this->cE->stampToHour($ts)); |
||
653 | } |
||
654 | |||
655 | /** |
||
656 | * Returns the value for the previous minute. |
||
657 | * |
||
658 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
659 | * |
||
660 | * @return int e.g. 23 or timestamp |
||
661 | */ |
||
662 | public function prevMinute($format = 'int') |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Returns the value for this minute. |
||
671 | * |
||
672 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
673 | * |
||
674 | * @return int e.g. 24 or timestamp |
||
675 | */ |
||
676 | public function thisMinute($format = 'int') |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * Returns the value for the next minute. |
||
685 | * |
||
686 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
687 | * |
||
688 | * @return int e.g. 25 or timestamp |
||
689 | */ |
||
690 | public function nextMinute($format = 'int') |
||
691 | { |
||
692 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, $this->minute + 1, 0); |
||
693 | |||
694 | return $this->returnValue('Minute', $format, $ts, $this->cE->stampToMinute($ts)); |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * Returns the value for the previous second. |
||
699 | * |
||
700 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
701 | * |
||
702 | * @return int e.g. 43 or timestamp |
||
703 | */ |
||
704 | public function prevSecond($format = 'int') |
||
705 | { |
||
706 | $ts = $this->cE->dateToStamp($this->year, $this->month, $this->day, $this->hour, $this->minute, $this->second - 1); |
||
707 | |||
708 | return $this->returnValue('Second', $format, $ts, $this->cE->stampToSecond($ts)); |
||
709 | } |
||
710 | |||
711 | /** |
||
712 | * Returns the value for this second. |
||
713 | * |
||
714 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
715 | * |
||
716 | * @return int e.g. 44 or timestamp |
||
717 | */ |
||
718 | public function thisSecond($format = 'int') |
||
723 | } |
||
724 | |||
725 | /** |
||
726 | * Returns the value for the next second. |
||
727 | * |
||
728 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
729 | * |
||
730 | * @return int e.g. 45 or timestamp |
||
731 | */ |
||
732 | public function nextSecond($format = 'int') |
||
737 | } |
||
738 | } |
||
739 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.