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 EED_Events_Archive 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 EED_Events_Archive, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class EED_Events_Archive extends EED_Module { |
||
30 | |||
31 | |||
32 | public static $espresso_event_list_ID = 0; |
||
33 | public static $espresso_grid_event_lists = array(); |
||
34 | |||
35 | /** |
||
36 | * @type bool $using_get_the_excerpt |
||
37 | */ |
||
38 | protected static $using_get_the_excerpt = false; |
||
39 | |||
40 | /** |
||
41 | * Used to flag when the event list is being called from an external iframe. |
||
42 | * |
||
43 | * @var bool $iframe |
||
44 | */ |
||
45 | protected static $iframe = false; |
||
46 | |||
47 | /** |
||
48 | * @var \EventEspresso\core\libraries\iframe_display\EventListIframeEmbedButton $_iframe_embed_button |
||
49 | */ |
||
50 | private static $_iframe_embed_button; |
||
51 | |||
52 | /** |
||
53 | * @type EE_Template_Part_Manager $template_parts |
||
54 | */ |
||
55 | protected $template_parts; |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * @return EED_Events_Archive |
||
61 | */ |
||
62 | public static function instance() { |
||
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * set_hooks - for hooking into EE Core, other modules, etc |
||
70 | * |
||
71 | * @access public |
||
72 | * @return void |
||
73 | */ |
||
74 | public static function set_hooks() { |
||
80 | |||
81 | /** |
||
82 | * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
||
83 | * |
||
84 | * @access public |
||
85 | * @return void |
||
86 | */ |
||
87 | public static function set_hooks_admin() { |
||
97 | |||
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * set_definitions |
||
103 | * |
||
104 | * @access public |
||
105 | * @return void |
||
106 | */ |
||
107 | public static function set_definitions() { |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * set up EE_Events_Archive_Config |
||
116 | */ |
||
117 | protected function set_config(){ |
||
122 | |||
123 | |||
124 | |||
125 | /** |
||
126 | * @return EventListIframeEmbedButton |
||
127 | */ |
||
128 | public static function get_iframe_embed_button() { |
||
134 | |||
135 | |||
136 | |||
137 | /** |
||
138 | * event_list_iframe_embed_button |
||
139 | * |
||
140 | * @return void |
||
141 | * @throws \EE_Error |
||
142 | */ |
||
143 | public static function event_list_iframe_embed_button() { |
||
147 | |||
148 | /** |
||
149 | * initialize_template_parts |
||
150 | * |
||
151 | * @access public |
||
152 | * @param \EE_Events_Archive_Config $config |
||
153 | * @return \EE_Template_Part_Manager |
||
154 | */ |
||
155 | View Code Duplication | public function initialize_template_parts( EE_Events_Archive_Config $config = null ) { |
|
186 | |||
187 | |||
188 | |||
189 | /** |
||
190 | * run - initial module setup - this gets called by the EE_Front_Controller if the module route is found in the incoming request |
||
191 | * |
||
192 | * @access public |
||
193 | * @param WP $WP |
||
194 | * @return void |
||
195 | */ |
||
196 | public function run( $WP ) { |
||
218 | |||
219 | |||
220 | |||
221 | /** |
||
222 | * event_list - most likely called by the EES_Espresso_Events shortcode which uses this module to do some of it's lifting |
||
223 | * |
||
224 | * @access public |
||
225 | * @return void |
||
226 | */ |
||
227 | public function event_list() { |
||
233 | |||
234 | |||
235 | |||
236 | /** |
||
237 | * @access public |
||
238 | * @return void |
||
239 | * @throws \EE_Error |
||
240 | * @throws \DomainException |
||
241 | */ |
||
242 | public function event_list_iframe() { |
||
247 | |||
248 | |||
249 | |||
250 | /** |
||
251 | * @access public |
||
252 | * @return string |
||
253 | */ |
||
254 | public static function is_iframe() { |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * @access public |
||
262 | * @return string |
||
263 | */ |
||
264 | public static function link_target() { |
||
267 | |||
268 | |||
269 | |||
270 | |||
271 | |||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * template_include |
||
277 | * |
||
278 | * @access public |
||
279 | * @param string $template |
||
280 | * @return string |
||
281 | */ |
||
282 | public function template_include( $template = '' ) { |
||
312 | |||
313 | |||
314 | |||
315 | /** |
||
316 | * get_the_excerpt - kinda hacky, but if a theme is using get_the_excerpt(), then we need to remove our filters on the_content() |
||
317 | * |
||
318 | * @access public |
||
319 | * @param string $excerpt |
||
320 | * @return string |
||
321 | */ |
||
322 | public static function get_the_excerpt( $excerpt = '' ) { |
||
336 | |||
337 | |||
338 | |||
339 | /** |
||
340 | * end_get_the_excerpt |
||
341 | * |
||
342 | * @access public |
||
343 | * @param string $text |
||
344 | * @return string |
||
345 | */ |
||
346 | public static function end_get_the_excerpt( $text = '' ) { |
||
350 | |||
351 | |||
352 | |||
353 | /** |
||
354 | * the_title |
||
355 | * |
||
356 | * @access public |
||
357 | * @param string $title |
||
358 | * @param string $id |
||
359 | * @return string |
||
360 | */ |
||
361 | public static function the_title( $title = '', $id = '' ) { |
||
368 | |||
369 | |||
370 | |||
371 | /** |
||
372 | * event_details |
||
373 | * |
||
374 | * @access public |
||
375 | * @param string $content |
||
376 | * @return string |
||
377 | */ |
||
378 | public static function event_details( $content ) { |
||
406 | |||
407 | |||
408 | |||
409 | /** |
||
410 | * use_sortable_display_order |
||
411 | * |
||
412 | * @access protected |
||
413 | * @return string |
||
414 | */ |
||
415 | protected static function use_sortable_display_order() { |
||
433 | |||
434 | |||
435 | |||
436 | /** |
||
437 | * use_filterable_display_order |
||
438 | * |
||
439 | * @access protected |
||
440 | * @return string |
||
441 | */ |
||
442 | protected static function use_filterable_display_order() { |
||
465 | |||
466 | |||
467 | |||
468 | /** |
||
469 | * event_datetimes - adds datetimes ABOVE content |
||
470 | * |
||
471 | * @access public |
||
472 | * @param string $content |
||
473 | * @return string |
||
474 | */ |
||
475 | public static function event_datetimes( $content ) { |
||
481 | |||
482 | /** |
||
483 | * event_tickets - adds tickets ABOVE content (which includes datetimes) |
||
484 | * |
||
485 | * @access public |
||
486 | * @param string $content |
||
487 | * @return string |
||
488 | */ |
||
489 | public static function event_tickets( $content ) { |
||
495 | |||
496 | |||
497 | |||
498 | /** |
||
499 | * event_venues - adds venues BELOW content |
||
500 | * |
||
501 | * @access public |
||
502 | * @param string $content |
||
503 | * @return string |
||
504 | */ |
||
505 | public static function event_venue( $content ) { |
||
508 | |||
509 | /** |
||
510 | * event_venues - adds venues BELOW content |
||
511 | * |
||
512 | * @access public |
||
513 | * @param string $content |
||
514 | * @return string |
||
515 | */ |
||
516 | public static function event_venues( $content ) { |
||
522 | |||
523 | |||
524 | |||
525 | /** |
||
526 | * _add_additional_content_filters |
||
527 | * |
||
528 | * @access private |
||
529 | * @return void |
||
530 | */ |
||
531 | private static function _add_additional_excerpt_filters() { |
||
536 | |||
537 | |||
538 | |||
539 | /** |
||
540 | * _add_additional_content_filters |
||
541 | * |
||
542 | * @access private |
||
543 | * @return void |
||
544 | */ |
||
545 | private static function _add_additional_content_filters() { |
||
550 | |||
551 | |||
552 | |||
553 | /** |
||
554 | * _remove_additional_events_archive_filters |
||
555 | * |
||
556 | * @access private |
||
557 | * @return void |
||
558 | */ |
||
559 | private static function _remove_additional_events_archive_filters() { |
||
567 | |||
568 | |||
569 | |||
570 | /** |
||
571 | * remove_all_events_archive_filters |
||
572 | * |
||
573 | * @access public |
||
574 | * @return void |
||
575 | */ |
||
576 | public static function remove_all_events_archive_filters() { |
||
590 | |||
591 | |||
592 | |||
593 | |||
594 | |||
595 | |||
596 | /** |
||
597 | * load_event_list_assets |
||
598 | * |
||
599 | * @access public |
||
600 | * @return void |
||
601 | */ |
||
602 | View Code Duplication | public function load_event_list_assets() { |
|
611 | |||
612 | |||
613 | |||
614 | |||
615 | |||
616 | |||
617 | /** |
||
618 | * wp_enqueue_scripts |
||
619 | * |
||
620 | * @access public |
||
621 | * @return void |
||
622 | */ |
||
623 | public function wp_enqueue_scripts() { |
||
635 | |||
636 | |||
637 | |||
638 | |||
639 | |||
640 | /** |
||
641 | * template_settings_form |
||
642 | * |
||
643 | * @access public |
||
644 | * @static |
||
645 | * @return string |
||
646 | */ |
||
647 | public static function template_settings_form() { |
||
662 | |||
663 | |||
664 | |||
665 | |||
666 | |||
667 | |||
668 | /** |
||
669 | * update_template_settings |
||
670 | * |
||
671 | * @access public |
||
672 | * @param EE_Template_Config $CFG |
||
673 | * @param EE_Request_Handler $REQ |
||
674 | * @return EE_Template_Config |
||
675 | */ |
||
676 | public static function update_template_settings( $CFG, $REQ ) { |
||
688 | |||
689 | |||
690 | |||
691 | /** |
||
692 | * event_list_css |
||
693 | * |
||
694 | * @access public |
||
695 | * @param string $extra_class |
||
696 | * @return string |
||
697 | */ |
||
698 | public static function event_list_css( $extra_class = '' ) { |
||
703 | |||
704 | |||
705 | |||
706 | |||
707 | |||
708 | |||
709 | /** |
||
710 | * event_categories |
||
711 | * |
||
712 | * @access public |
||
713 | * @return array |
||
714 | */ |
||
715 | public static function event_categories() { |
||
718 | |||
719 | |||
720 | |||
721 | /** |
||
722 | * display_description |
||
723 | * |
||
724 | * @access public |
||
725 | * @param $value |
||
726 | * @return bool |
||
727 | */ |
||
728 | public static function display_description( $value ) { |
||
733 | |||
734 | |||
735 | /** |
||
736 | * display_ticket_selector |
||
737 | * |
||
738 | * @access public |
||
739 | * @return bool |
||
740 | */ |
||
741 | public static function display_ticket_selector() { |
||
745 | |||
746 | |||
747 | |||
748 | /** |
||
749 | * display_venue |
||
750 | * |
||
751 | * @access public |
||
752 | * @return bool |
||
753 | */ |
||
754 | public static function display_venue() { |
||
758 | |||
759 | |||
760 | /** |
||
761 | * display_datetimes |
||
762 | * |
||
763 | * @access public |
||
764 | * @return bool |
||
765 | */ |
||
766 | public static function display_datetimes() { |
||
770 | |||
771 | |||
772 | |||
773 | |||
774 | |||
775 | |||
776 | /** |
||
777 | * event_list_title |
||
778 | * |
||
779 | * @access public |
||
780 | * @return string |
||
781 | */ |
||
782 | public static function event_list_title() { |
||
785 | |||
786 | |||
787 | // GRAVEYARD |
||
788 | |||
789 | /** |
||
790 | * @since 4.4.0 |
||
791 | */ |
||
792 | public static function _doing_it_wrong_notice( $function = '' ) { |
||
804 | |||
805 | |||
806 | |||
807 | /** |
||
808 | * @deprecated |
||
809 | * @since 4.4.0 |
||
810 | */ |
||
811 | public function get_post_data() { |
||
814 | /** |
||
815 | * @deprecated |
||
816 | * @since 4.4.0 |
||
817 | */ |
||
818 | public function posts_fields( $SQL, WP_Query $wp_query ) { |
||
822 | /** |
||
823 | * @deprecated |
||
824 | * @since 4.4.0 |
||
825 | */ |
||
826 | public static function posts_fields_sql_for_orderby( $orderby_params = array() ) { |
||
830 | /** |
||
831 | * @deprecated |
||
832 | * @since 4.4.0 |
||
833 | */ |
||
834 | public function posts_join( $SQL, WP_Query $wp_query ) { |
||
838 | /** |
||
839 | * @deprecated |
||
840 | * @since 4.4.0 |
||
841 | */ |
||
842 | public static function posts_join_sql_for_terms( $join_terms = NULL ) { |
||
846 | /** |
||
847 | * @deprecated |
||
848 | * @since 4.4.0 |
||
849 | */ |
||
850 | public static function posts_join_for_orderby( $orderby_params = array() ) { |
||
854 | /** |
||
855 | * @deprecated |
||
856 | * @since 4.4.0 |
||
857 | */ |
||
858 | public function posts_where( $SQL, WP_Query $wp_query ) { |
||
862 | /** |
||
863 | * @deprecated |
||
864 | * @since 4.4.0 |
||
865 | */ |
||
866 | public static function posts_where_sql_for_show_expired( $show_expired = FALSE ) { |
||
870 | /** |
||
871 | * @deprecated |
||
872 | * @since 4.4.0 |
||
873 | */ |
||
874 | public static function posts_where_sql_for_event_category_slug( $event_category_slug = NULL ) { |
||
878 | /** |
||
879 | * @deprecated |
||
880 | * @since 4.4.0 |
||
881 | */ |
||
882 | public static function posts_where_sql_for_event_list_month( $month = NULL ) { |
||
886 | /** |
||
887 | * @deprecated |
||
888 | * @since 4.4.0 |
||
889 | */ |
||
890 | public function posts_orderby( $SQL, WP_Query $wp_query ) { |
||
894 | /** |
||
895 | * @deprecated |
||
896 | * @since 4.4.0 |
||
897 | */ |
||
898 | public static function posts_orderby_sql( $orderby_params = array(), $sort = 'ASC' ) { |
||
902 | |||
903 | |||
904 | |||
905 | } |
||
906 | |||
985 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.