Total Complexity | 51 |
Total Lines | 526 |
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() |
||
209 | { |
||
210 | if (method_exists($this->calendar, 'isLast')) { |
||
211 | return $this->calendar->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() |
||
234 | { |
||
235 | if (method_exists($this->calendar, 'isEmpty')) { |
||
236 | return $this->calendar->isEmpty(); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Build the children. |
||
242 | * |
||
243 | * @param array $sDates array containing Calendar objects to select (optional) |
||
244 | * |
||
245 | * @return bool |
||
246 | * @abstract |
||
247 | */ |
||
248 | public function build($sDates = []) |
||
249 | { |
||
250 | $this->calendar->build($sDates); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Iterator method for fetching child Calendar subclass objects |
||
255 | * (e.g. a minute from an hour object). On reaching the end of |
||
256 | * the collection, returns false and resets the collection for |
||
257 | * further iteratations. |
||
258 | * |
||
259 | * @return mixed either an object subclass of Calendar or false |
||
260 | */ |
||
261 | public function fetch() |
||
262 | { |
||
263 | return $this->calendar->fetch(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Fetches all child from the current collection of children. |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public function fetchAll() |
||
272 | { |
||
273 | return $this->calendar->fetchAll(); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Get the number Calendar subclass objects stored in the internal collection. |
||
278 | * |
||
279 | * @return int |
||
280 | */ |
||
281 | public function size() |
||
282 | { |
||
283 | return $this->calendar->size(); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Determine whether this date is valid, with the bounds determined by |
||
288 | * the Calendar_Engine. The call is passed on to Calendar_Validator::isValid. |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | public function isValid() |
||
293 | { |
||
294 | return $this->calendar->isValid(); |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Returns an instance of Calendar_Validator. |
||
299 | * |
||
300 | * @return Calendar_Validator |
||
301 | */ |
||
302 | public function &getValidator() |
||
303 | { |
||
304 | $validator = $this->calendar->getValidator(); |
||
305 | |||
306 | return $validator; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Returns a reference to the current Calendar_Engine being used. Useful |
||
311 | * for Calendar_Table_Helper and Calendar_Validator. |
||
312 | * |
||
313 | * @return object implementing Calendar_Engine_Inteface |
||
314 | */ |
||
315 | public function &getEngine() |
||
316 | { |
||
317 | $engine = $this->calendar->getEngine(); |
||
318 | |||
319 | return $engine; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Returns the value for the previous year. |
||
324 | * |
||
325 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
326 | * |
||
327 | * @return int e.g. 2002 or timestamp |
||
328 | */ |
||
329 | public function prevYear($format = 'int') |
||
330 | { |
||
331 | return $this->calendar->prevYear($format); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Returns the value for this year. |
||
336 | * |
||
337 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
338 | * |
||
339 | * @return int e.g. 2003 or timestamp |
||
340 | */ |
||
341 | public function thisYear($format = 'int') |
||
342 | { |
||
343 | return $this->calendar->thisYear($format); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Returns the value for next year. |
||
348 | * |
||
349 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
350 | * |
||
351 | * @return int e.g. 2004 or timestamp |
||
352 | */ |
||
353 | public function nextYear($format = 'int') |
||
354 | { |
||
355 | return $this->calendar->nextYear($format); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Returns the value for the previous month. |
||
360 | * |
||
361 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
362 | * |
||
363 | * @return int e.g. 4 or Unix timestamp |
||
364 | */ |
||
365 | public function prevMonth($format = 'int') |
||
366 | { |
||
367 | return $this->calendar->prevMonth($format); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Returns the value for this month. |
||
372 | * |
||
373 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
374 | * |
||
375 | * @return int e.g. 5 or timestamp |
||
376 | */ |
||
377 | public function thisMonth($format = 'int') |
||
378 | { |
||
379 | return $this->calendar->thisMonth($format); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Returns the value for next month. |
||
384 | * |
||
385 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
386 | * |
||
387 | * @return int e.g. 6 or timestamp |
||
388 | */ |
||
389 | public function nextMonth($format = 'int') |
||
390 | { |
||
391 | return $this->calendar->nextMonth($format); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Returns the value for the previous week. |
||
396 | * |
||
397 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
398 | * |
||
399 | * @return int e.g. 4 or Unix timestamp |
||
400 | */ |
||
401 | public function prevWeek($format = 'n_in_month') |
||
402 | { |
||
403 | if (method_exists($this->calendar, 'prevWeek')) { |
||
404 | return $this->calendar->prevWeek($format); |
||
405 | } else { |
||
406 | require_once __DIR__ . '/PEAR.php'; |
||
407 | PEAR::raiseError('Cannot call prevWeek on Calendar object of type: ' . get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Decorator::prevWeek()'); |
||
408 | |||
409 | return false; |
||
410 | } |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Returns the value for this week. |
||
415 | * |
||
416 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
417 | * |
||
418 | * @return int e.g. 5 or timestamp |
||
419 | */ |
||
420 | public function thisWeek($format = 'n_in_month') |
||
421 | { |
||
422 | if (method_exists($this->calendar, 'thisWeek')) { |
||
423 | return $this->calendar->thisWeek($format); |
||
424 | } else { |
||
425 | require_once __DIR__ . '/PEAR.php'; |
||
426 | PEAR::raiseError('Cannot call thisWeek on Calendar object of type: ' . get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Decorator::thisWeek()'); |
||
427 | |||
428 | return false; |
||
429 | } |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Returns the value for next week. |
||
434 | * |
||
435 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
436 | * |
||
437 | * @return int e.g. 6 or timestamp |
||
438 | */ |
||
439 | public function nextWeek($format = 'n_in_month') |
||
440 | { |
||
441 | if (method_exists($this->calendar, 'nextWeek')) { |
||
442 | return $this->calendar->nextWeek($format); |
||
443 | } else { |
||
444 | require_once __DIR__ . '/PEAR.php'; |
||
445 | PEAR::raiseError('Cannot call thisWeek on Calendar object of type: ' . get_class($this->calendar), 133, PEAR_ERROR_TRIGGER, E_USER_NOTICE, 'Calendar_Decorator::nextWeek()'); |
||
446 | |||
447 | return false; |
||
448 | } |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Returns the value for the previous day. |
||
453 | * |
||
454 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
455 | * |
||
456 | * @return int e.g. 10 or timestamp |
||
457 | */ |
||
458 | public function prevDay($format = 'int') |
||
459 | { |
||
460 | return $this->calendar->prevDay($format); |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Returns the value for this day. |
||
465 | * |
||
466 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
467 | * |
||
468 | * @return int e.g. 11 or timestamp |
||
469 | */ |
||
470 | public function thisDay($format = 'int') |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Returns the value for the next day. |
||
477 | * |
||
478 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
479 | * |
||
480 | * @return int e.g. 12 or timestamp |
||
481 | */ |
||
482 | public function nextDay($format = 'int') |
||
483 | { |
||
484 | return $this->calendar->nextDay($format); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Returns the value for the previous hour. |
||
489 | * |
||
490 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
491 | * |
||
492 | * @return int e.g. 13 or timestamp |
||
493 | */ |
||
494 | public function prevHour($format = 'int') |
||
495 | { |
||
496 | return $this->calendar->prevHour($format); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Returns the value for this hour. |
||
501 | * |
||
502 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
503 | * |
||
504 | * @return int e.g. 14 or timestamp |
||
505 | */ |
||
506 | public function thisHour($format = 'int') |
||
507 | { |
||
508 | return $this->calendar->thisHour($format); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * Returns the value for the next hour. |
||
513 | * |
||
514 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
515 | * |
||
516 | * @return int e.g. 14 or timestamp |
||
517 | */ |
||
518 | public function nextHour($format = 'int') |
||
519 | { |
||
520 | return $this->calendar->nextHour($format); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * Returns the value for the previous minute. |
||
525 | * |
||
526 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
527 | * |
||
528 | * @return int e.g. 23 or timestamp |
||
529 | */ |
||
530 | public function prevMinute($format = 'int') |
||
531 | { |
||
532 | return $this->calendar->prevMinute($format); |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * Returns the value for this minute. |
||
537 | * |
||
538 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
539 | * |
||
540 | * @return int e.g. 24 or timestamp |
||
541 | */ |
||
542 | public function thisMinute($format = 'int') |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Returns the value for the next minute. |
||
549 | * |
||
550 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
551 | * |
||
552 | * @return int e.g. 25 or timestamp |
||
553 | */ |
||
554 | public function nextMinute($format = 'int') |
||
557 | } |
||
558 | |||
559 | /** |
||
560 | * Returns the value for the previous second. |
||
561 | * |
||
562 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
563 | * |
||
564 | * @return int e.g. 43 or timestamp |
||
565 | */ |
||
566 | public function prevSecond($format = 'int') |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Returns the value for this second. |
||
573 | * |
||
574 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
575 | * |
||
576 | * @return int e.g. 44 or timestamp |
||
577 | */ |
||
578 | public function thisSecond($format = 'int') |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Returns the value for the next second. |
||
585 | * |
||
586 | * @param string $format return value format ['int'|'timestamp'|'object'|'array'] |
||
587 | * |
||
588 | * @return int e.g. 45 or timestamp |
||
589 | */ |
||
590 | public function nextSecond($format = 'int') |
||
593 | } |
||
594 | } |
||
595 |
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.