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 |
||
23 | abstract class Calendar { |
||
24 | |||
25 | /** |
||
26 | * Calendar Id. |
||
27 | * |
||
28 | * @access public |
||
29 | * @var int |
||
30 | */ |
||
31 | public $id = 0; |
||
32 | |||
33 | /** |
||
34 | * Calendar post object. |
||
35 | * |
||
36 | * @access public |
||
37 | * @var \WP_Post |
||
38 | */ |
||
39 | public $post = null; |
||
40 | |||
41 | /** |
||
42 | * Calendar Type. |
||
43 | * |
||
44 | * @access public |
||
45 | * @var string |
||
46 | */ |
||
47 | public $type = ''; |
||
48 | |||
49 | /** |
||
50 | * Calendar Name. |
||
51 | * |
||
52 | * @access public |
||
53 | * @var string |
||
54 | */ |
||
55 | public $name = ''; |
||
56 | |||
57 | /** |
||
58 | * Feed type. |
||
59 | * |
||
60 | * @access public |
||
61 | * @var string |
||
62 | */ |
||
63 | public $feed = ''; |
||
64 | |||
65 | /** |
||
66 | * Calendar start. |
||
67 | * |
||
68 | * @access public |
||
69 | * @var int |
||
70 | */ |
||
71 | public $start = 0; |
||
72 | |||
73 | /** |
||
74 | * Calendar end. |
||
75 | * |
||
76 | * @access public |
||
77 | * @var int |
||
78 | */ |
||
79 | public $end = 0; |
||
80 | |||
81 | /** |
||
82 | * Static calendar. |
||
83 | * |
||
84 | * @access public |
||
85 | * @var bool |
||
86 | */ |
||
87 | public $static = false; |
||
88 | |||
89 | /** |
||
90 | * Today. |
||
91 | * |
||
92 | * @access public |
||
93 | * @var int |
||
94 | */ |
||
95 | public $today = 0; |
||
96 | |||
97 | /** |
||
98 | * Time now. |
||
99 | * |
||
100 | * @access public |
||
101 | * @var int |
||
102 | */ |
||
103 | public $now = 0; |
||
104 | |||
105 | /** |
||
106 | * Timezone offset. |
||
107 | * |
||
108 | * @access public |
||
109 | * @var int |
||
110 | */ |
||
111 | public $offset = 0; |
||
112 | |||
113 | /** |
||
114 | * Timezone |
||
115 | * |
||
116 | * @access public |
||
117 | * @var string |
||
118 | */ |
||
119 | public $timezone = 'UTC'; |
||
120 | |||
121 | /** |
||
122 | * Site timezone. |
||
123 | * |
||
124 | * @access public |
||
125 | * @var string |
||
126 | */ |
||
127 | public $site_timezone = 'UTC'; |
||
128 | |||
129 | /** |
||
130 | * Date format. |
||
131 | * |
||
132 | * @access public |
||
133 | * @var string |
||
134 | */ |
||
135 | public $date_format = ''; |
||
136 | |||
137 | /** |
||
138 | * Time format. |
||
139 | * |
||
140 | * @access public |
||
141 | * @var string |
||
142 | */ |
||
143 | public $time_format = ''; |
||
144 | |||
145 | /** |
||
146 | * Date-time separator. |
||
147 | * |
||
148 | * @access public |
||
149 | * @var string |
||
150 | */ |
||
151 | public $datetime_separator = '@'; |
||
152 | |||
153 | /** |
||
154 | * First day of the week. |
||
155 | * |
||
156 | * @access public |
||
157 | * @var int |
||
158 | */ |
||
159 | public $week_starts = 0; |
||
160 | |||
161 | /** |
||
162 | * Events to display. |
||
163 | * |
||
164 | * @access public |
||
165 | * @var array |
||
166 | */ |
||
167 | public $events = array(); |
||
168 | |||
169 | /** |
||
170 | * Errors. |
||
171 | * |
||
172 | * @access public |
||
173 | * @var array |
||
174 | */ |
||
175 | protected $errors = array(); |
||
176 | |||
177 | /** |
||
178 | * Earliest event. |
||
179 | * |
||
180 | * @access public |
||
181 | * @var int |
||
182 | */ |
||
183 | public $earliest_event = 0; |
||
184 | |||
185 | /** |
||
186 | * Latest event. |
||
187 | * |
||
188 | * @access public |
||
189 | * @var int |
||
190 | */ |
||
191 | public $latest_event = 0; |
||
192 | |||
193 | /** |
||
194 | * Event builder content. |
||
195 | * |
||
196 | * @access public |
||
197 | * @var string |
||
198 | */ |
||
199 | public $events_template = ''; |
||
200 | |||
201 | /** |
||
202 | * Calendar Views. |
||
203 | * |
||
204 | * The calendar available views. |
||
205 | * |
||
206 | * @access public |
||
207 | * @var array |
||
208 | */ |
||
209 | public $views = array(); |
||
210 | |||
211 | /** |
||
212 | * Calendar View. |
||
213 | * |
||
214 | * The current view. |
||
215 | * |
||
216 | * @access public |
||
217 | * @var Calendar_View |
||
218 | */ |
||
219 | public $view = null; |
||
220 | |||
221 | /** |
||
222 | * Calendar settings. |
||
223 | * |
||
224 | * @access protected |
||
225 | * @var array |
||
226 | */ |
||
227 | protected $settings = array(); |
||
228 | |||
229 | /** |
||
230 | * Constructor. |
||
231 | * |
||
232 | * @since 3.0.0 |
||
233 | * |
||
234 | * @param int|object|\WP_Post|Calendar $calendar |
||
235 | * @param string $view |
||
236 | */ |
||
237 | public function __construct( $calendar, $view = '' ) { |
||
238 | |||
239 | // Set the post object. |
||
240 | $this->set_post_object( $calendar ); |
||
241 | |||
242 | if ( ! is_null( $this->post ) ) { |
||
243 | |||
244 | // Set calendar type and events source. |
||
245 | $this->set_taxonomies(); |
||
246 | |||
247 | // Set calendar default datetime properties. |
||
248 | $this->set_timezone(); |
||
249 | $this->set_start_of_week(); |
||
250 | $this->set_static(); |
||
251 | |||
252 | // Set calendar start. |
||
253 | $this->set_start(); |
||
254 | |||
255 | // Set the events template. |
||
256 | $this->set_events_template(); |
||
257 | |||
258 | // Get events source data. |
||
259 | $feed = simcal_get_feed( $this ); |
||
260 | if ( $feed instanceof Feed ) { |
||
261 | if ( ! empty( $feed->events ) ) { |
||
262 | if ( is_array( $feed->events ) ) { |
||
263 | $this->set_events( $feed->events ); |
||
264 | if ( 'use_calendar' == get_post_meta( $this->id, '_feed_timezone_setting', true ) ) { |
||
265 | $this->timezone = $feed->timezone; |
||
266 | $this->set_start( $feed->timezone ); |
||
267 | } |
||
268 | } elseif ( is_string( $feed->events ) ) { |
||
269 | $this->errors[] = $feed->events; |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | |||
274 | // Set general purpose timestamps. |
||
275 | $now = Carbon::now( $this->timezone ); |
||
276 | $this->now = $now->getTimestamp(); |
||
277 | $this->today = $now->startOfDay()->getTimestamp(); |
||
278 | $this->offset = $now->getOffset(); |
||
279 | |||
280 | // Set date time formatting. |
||
281 | $this->set_date_format(); |
||
282 | $this->set_time_format(); |
||
283 | $this->set_datetime_separator(); |
||
284 | |||
285 | // Set earliest and latest event timestamps. |
||
286 | if ( $this->events && is_array( $this->events ) ) { |
||
287 | $this->earliest_event = intval( current( array_keys( $this->events ) ) ); |
||
288 | $this->latest_event = intval( key( array_slice( $this->events, -1, 1, true ) ) ); |
||
289 | } |
||
290 | |||
291 | // Set calendar end. |
||
292 | $this->set_end(); |
||
293 | |||
294 | // Set view. |
||
295 | if ( ! $view ) { |
||
296 | |||
297 | $calendar_view = get_post_meta( $this->id, '_calendar_view', true ); |
||
298 | $calendar_view = isset( $calendar_view[ $this->type ] ) ? $calendar_view[ $this->type ] : ''; |
||
299 | |||
300 | $view = esc_attr( $calendar_view ); |
||
301 | } |
||
302 | } |
||
303 | |||
304 | // Get view. |
||
305 | $this->view = $this->get_view( $view ); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Overloading __isset function with post meta. |
||
310 | * |
||
311 | * @since 3.0.0 |
||
312 | * |
||
313 | * @param mixed $key Post meta key. |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | public function __isset( $key ) { |
||
320 | |||
321 | /** |
||
322 | * Overloading __get function with post meta. |
||
323 | * |
||
324 | * @since 3.0.0 |
||
325 | * |
||
326 | * @param string $key Post meta key. |
||
327 | * |
||
328 | * @return mixed |
||
329 | */ |
||
330 | public function __get( $key ) { |
||
331 | $value = get_post_meta( $this->id, '_' . $key, true ); |
||
332 | if ( ! empty( $value ) ) { |
||
333 | $this->$key = $value; |
||
334 | } |
||
335 | return $value; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Set post object and id. |
||
340 | * |
||
341 | * @since 3.0.0 |
||
342 | * |
||
343 | * @param int|object|\WP_Post|Calendar $calendar |
||
344 | */ |
||
345 | public function set_post_object( $calendar ) { |
||
346 | if ( is_numeric( $calendar ) ) { |
||
347 | $this->id = absint( $calendar ); |
||
348 | $this->post = get_post( $this->id ); |
||
349 | } elseif ( $calendar instanceof Calendar ) { |
||
350 | $this->id = absint( $calendar->id ); |
||
351 | $this->post = $calendar->post; |
||
352 | } elseif ( $calendar instanceof \WP_Post ) { |
||
353 | $this->id = absint( $calendar->ID ); |
||
354 | $this->post = $calendar; |
||
355 | } elseif ( isset( $calendar->id ) && isset( $calendar->post ) ) { |
||
356 | $this->id = $calendar->id; |
||
357 | $this->post = $calendar->post; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Return the calendar title. |
||
363 | * |
||
364 | * @since 3.0.0 |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function get_title() { |
||
372 | |||
373 | /** |
||
374 | * Get the calendar post data. |
||
375 | * |
||
376 | * @since 3.0.0 |
||
377 | * |
||
378 | * @return \WP_Post |
||
379 | */ |
||
380 | public function get_post_data() { |
||
383 | |||
384 | /** |
||
385 | * Set taxonomies. |
||
386 | * |
||
387 | * @since 3.0.0 |
||
388 | * @access protected |
||
389 | */ |
||
390 | protected function set_taxonomies() { |
||
391 | // Set calendar type. |
||
392 | if ( $type = wp_get_object_terms( $this->id, 'calendar_type' ) ) { |
||
393 | $this->type = sanitize_title( current( $type )->name ); |
||
394 | } else { |
||
395 | $this->type = apply_filters( 'simcal_calendar_default_type', 'default-calendar' ); |
||
396 | } |
||
397 | // Set feed type. |
||
398 | if ( $feed_type = wp_get_object_terms( $this->id, 'calendar_feed' ) ) { |
||
399 | $this->feed = sanitize_title( current( $feed_type )->name ); |
||
400 | } else { |
||
401 | $this->feed = apply_filters( 'simcal_calendar_default_feed', 'google' ); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * Get events. |
||
407 | * |
||
408 | * @since 3.0.0 |
||
409 | * |
||
410 | * @return Events |
||
411 | */ |
||
412 | public function get_events() { |
||
415 | |||
416 | /** |
||
417 | * Set events. |
||
418 | * |
||
419 | * @since 3.0.0 |
||
420 | * |
||
421 | * @param array $array |
||
422 | */ |
||
423 | public function set_events( array $array ) { |
||
437 | |||
438 | /** |
||
439 | * Get the event builder template. |
||
440 | * |
||
441 | * @since 3.0.0 |
||
442 | * |
||
443 | * @param string $template |
||
444 | * |
||
445 | * @return string |
||
|
|||
446 | */ |
||
447 | public function set_events_template( $template = '' ) { |
||
469 | |||
470 | /** |
||
471 | * Set the timezone. |
||
472 | * |
||
473 | * @since 3.0.0 |
||
474 | * |
||
475 | * @param string $tz Timezone. |
||
476 | */ |
||
477 | public function set_timezone( $tz = '' ) { |
||
504 | |||
505 | /** |
||
506 | * Set date format. |
||
507 | * |
||
508 | * @since 3.0.0 |
||
509 | * |
||
510 | * @param string $format PHP datetime format. |
||
511 | */ |
||
512 | public function set_date_format( $format = '' ) { |
||
531 | |||
532 | /** |
||
533 | * Set time format. |
||
534 | * |
||
535 | * @since 3.0.0 |
||
536 | * |
||
537 | * @param string $format PHP datetime format. |
||
538 | */ |
||
539 | public function set_time_format( $format = '' ) { |
||
558 | |||
559 | /** |
||
560 | * Set date-time separator. |
||
561 | * |
||
562 | * @since 3.0.0 |
||
563 | * |
||
564 | * @param string $separator A UTF8 character used as separator. |
||
565 | */ |
||
566 | public function set_datetime_separator( $separator = '' ) { |
||
574 | |||
575 | /** |
||
576 | * Set start of week. |
||
577 | * |
||
578 | * @since 3.0.0 |
||
579 | * |
||
580 | * @param int $weekday From 0 (Sunday) to 6 (Friday). |
||
581 | */ |
||
582 | public function set_start_of_week( $weekday = -1 ) { |
||
599 | |||
600 | /** |
||
601 | * Set calendar start. |
||
602 | * |
||
603 | * @since 3.0.0 |
||
604 | * |
||
605 | * @param int $timestamp |
||
606 | */ |
||
607 | public function set_start( $timestamp = 0 ) { |
||
655 | |||
656 | /** |
||
657 | * Set calendar end. |
||
658 | * |
||
659 | * @since 3.0.0 |
||
660 | * |
||
661 | * @param int $timestamp |
||
662 | */ |
||
663 | public function set_end( $timestamp = 0 ) { |
||
667 | |||
668 | /** |
||
669 | * Set the calendar to static. |
||
670 | * |
||
671 | * @since 3.0.0 |
||
672 | * |
||
673 | * @param string|bool $static |
||
674 | */ |
||
675 | public function set_static( $static = '' ) { |
||
689 | |||
690 | /** |
||
691 | * Input fields for settings page. |
||
692 | * |
||
693 | * @since 3.0.0 |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | public function settings_fields() { |
||
700 | |||
701 | /** |
||
702 | * Get a calendar view. |
||
703 | * |
||
704 | * @since 3.0.0 |
||
705 | * |
||
706 | * @param string $view |
||
707 | * |
||
708 | * @return Calendar_View |
||
709 | */ |
||
710 | abstract public function get_view( $view = '' ); |
||
711 | |||
712 | /** |
||
713 | * Get event HTML parsed by template. |
||
714 | * |
||
715 | * @since 3.0.0 |
||
716 | * |
||
717 | * @param Event $event Event object to be parsed. |
||
718 | * @param string $template (optional) To use another template or a partial. |
||
719 | * |
||
720 | * @return string |
||
721 | */ |
||
722 | public function get_event_html( Event $event, $template = '' ) { |
||
728 | |||
729 | /** |
||
730 | * Output the calendar markup. |
||
731 | * |
||
732 | * @since 3.0.0 |
||
733 | * |
||
734 | * @param string $view The calendar view to display. |
||
735 | */ |
||
736 | public function html( $view = '' ) { |
||
798 | |||
799 | } |
||
800 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.