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 if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 24 | class EED_Events_Archive extends EED_Module { |
||
| 25 | |||
| 26 | |||
| 27 | public static $espresso_event_list_ID = 0; |
||
| 28 | public static $espresso_grid_event_lists = array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @type bool $using_get_the_excerpt |
||
| 32 | */ |
||
| 33 | protected static $using_get_the_excerpt = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @type EE_Template_Part_Manager $template_parts |
||
| 37 | */ |
||
| 38 | protected $template_parts; |
||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * @return EED_Events_Archive |
||
| 44 | */ |
||
| 45 | public static function instance() { |
||
| 46 | return parent::get_instance( __CLASS__ ); |
||
|
|
|||
| 47 | } |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * set_hooks - for hooking into EE Core, other modules, etc |
||
| 53 | * |
||
| 54 | * @access public |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public static function set_hooks() { |
||
| 58 | EE_Config::register_route( EE_Registry::instance()->CFG->core->event_cpt_slug, 'Events_Archive', 'run' ); |
||
| 59 | EE_Config::register_route( 'event_list', 'Events_Archive', 'event_list' ); |
||
| 60 | add_action( 'wp_loaded', array( 'EED_Events_Archive', 'set_definitions' ), 2 ); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * set_hooks_admin - for hooking into EE Admin Core, other modules, etc |
||
| 65 | * |
||
| 66 | * @access public |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | public static function set_hooks_admin() { |
||
| 70 | add_action( 'wp_loaded', array( 'EED_Events_Archive', 'set_definitions' ), 2 ); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * set_definitions |
||
| 78 | * |
||
| 79 | * @access public |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public static function set_definitions() { |
||
| 83 | define( 'EVENTS_ARCHIVE_ASSETS_URL', plugin_dir_url( __FILE__ ) . 'assets' . DS ); |
||
| 84 | define( 'EVENTS_ARCHIVE_TEMPLATES_PATH', str_replace( '\\', DS, plugin_dir_path( __FILE__ )) . 'templates' . DS ); |
||
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * set_config |
||
| 91 | * |
||
| 92 | * @return \EE_Events_Archive_Config |
||
| 93 | */ |
||
| 94 | protected function set_config(){ |
||
| 95 | $this->set_config_section( 'template_settings' ); |
||
| 96 | $this->set_config_class( 'EE_Events_Archive_Config' ); |
||
| 97 | $this->set_config_name( 'EED_Events_Archive' ); |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * initialize_template_parts |
||
| 104 | * |
||
| 105 | * @access public |
||
| 106 | * @param \EE_Events_Archive_Config $config |
||
| 107 | * @return \EE_Template_Part_Manager |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function initialize_template_parts( EE_Events_Archive_Config $config = null ) { |
|
| 110 | $config = $config instanceof EE_Events_Archive_Config ? $config : $this->config(); |
||
| 111 | EEH_Autoloader::instance()->register_template_part_autoloaders(); |
||
| 112 | $template_parts = new EE_Template_Part_Manager(); |
||
| 113 | $template_parts->add_template_part( |
||
| 114 | 'tickets', |
||
| 115 | __( 'Ticket Selector', 'event_espresso' ), |
||
| 116 | 'content-espresso_events-tickets.php', |
||
| 117 | $config->display_order_tickets |
||
| 118 | ); |
||
| 119 | $template_parts->add_template_part( |
||
| 120 | 'datetimes', |
||
| 121 | __( 'Dates and Times', 'event_espresso' ), |
||
| 122 | 'content-espresso_events-datetimes.php', |
||
| 123 | $config->display_order_datetimes |
||
| 124 | ); |
||
| 125 | $template_parts->add_template_part( |
||
| 126 | 'event', |
||
| 127 | __( 'Event Description', 'event_espresso' ), |
||
| 128 | 'content-espresso_events-details.php', |
||
| 129 | $config->display_order_event |
||
| 130 | ); |
||
| 131 | $template_parts->add_template_part( |
||
| 132 | 'venue', |
||
| 133 | __( 'Venue Information', 'event_espresso' ), |
||
| 134 | 'content-espresso_events-venues.php', |
||
| 135 | $config->display_order_venue |
||
| 136 | ); |
||
| 137 | do_action( 'AHEE__EED_Event_Archive__initialize_template_parts', $template_parts ); |
||
| 138 | return $template_parts; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * run - initial module setup - this gets called by the EE_Front_Controller if the module route is found in the incoming request |
||
| 145 | * |
||
| 146 | * @access public |
||
| 147 | * @param WP $WP |
||
| 148 | * @return void |
||
| 149 | */ |
||
| 150 | public function run( $WP ) { |
||
| 151 | do_action( 'AHEE__EED_Events_Archive__before_run' ); |
||
| 152 | // ensure valid EE_Events_Archive_Config() object exists |
||
| 153 | $this->set_config(); |
||
| 154 | /** @type EE_Events_Archive_Config $config */ |
||
| 155 | $config = $this->config(); |
||
| 156 | // load other required components |
||
| 157 | $this->load_event_list_assets(); |
||
| 158 | // filter the WP posts_join, posts_where, and posts_orderby SQL clauses |
||
| 159 | EE_Registry::instance()->load_helper( 'Event_Query' ); |
||
| 160 | //add query filters |
||
| 161 | EEH_Event_Query::add_query_filters(); |
||
| 162 | // set params that will get used by the filters |
||
| 163 | EEH_Event_Query::set_query_params( |
||
| 164 | '', // month |
||
| 165 | '', // category |
||
| 166 | $config->display_expired_events, // show_expired |
||
| 167 | 'start_date', // orderby |
||
| 168 | 'ASC' // sort |
||
| 169 | ); |
||
| 170 | // check what template is loaded |
||
| 171 | add_filter( 'template_include', array( $this, 'template_include' ), 999, 1 ); |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * event_list - most likely called by the EES_Espresso_Events shortcode which uses this module to do some of it's lifting |
||
| 178 | * |
||
| 179 | * @access public |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | public function event_list() { |
||
| 183 | // ensure valid EE_Events_Archive_Config() object exists |
||
| 184 | $this->set_config(); |
||
| 185 | // load other required components |
||
| 186 | $this->load_event_list_assets(); |
||
| 187 | } |
||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * template_include |
||
| 198 | * |
||
| 199 | * @access public |
||
| 200 | * @param string $template |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function template_include( $template = '' ) { |
||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * get_the_excerpt - kinda hacky, but if a theme is using get_the_excerpt(), then we need to remove our filters on the_content() |
||
| 238 | * |
||
| 239 | * @access public |
||
| 240 | * @param string $excerpt |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public static function get_the_excerpt( $excerpt = '' ) { |
||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * end_get_the_excerpt |
||
| 262 | * |
||
| 263 | * @access public |
||
| 264 | * @param string $text |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public static function end_get_the_excerpt( $text = '' ) { |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * the_title |
||
| 276 | * |
||
| 277 | * @access public |
||
| 278 | * @param string $title |
||
| 279 | * @param string $id |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public static function the_title( $title = '', $id = '' ) { |
||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * event_details |
||
| 294 | * |
||
| 295 | * @access public |
||
| 296 | * @param string $content |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | public static function event_details( $content ) { |
||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * use_sortable_display_order |
||
| 332 | * |
||
| 333 | * @access protected |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | protected static function use_sortable_display_order() { |
||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * use_filterable_display_order |
||
| 359 | * |
||
| 360 | * @access protected |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected static function use_filterable_display_order() { |
||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * event_datetimes - adds datetimes ABOVE content |
||
| 391 | * |
||
| 392 | * @access public |
||
| 393 | * @param string $content |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public static function event_datetimes( $content ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * event_tickets - adds tickets ABOVE content (which includes datetimes) |
||
| 405 | * |
||
| 406 | * @access public |
||
| 407 | * @param string $content |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public static function event_tickets( $content ) { |
||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * event_venues - adds venues BELOW content |
||
| 421 | * |
||
| 422 | * @access public |
||
| 423 | * @param string $content |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public static function event_venue( $content ) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * event_venues - adds venues BELOW content |
||
| 432 | * |
||
| 433 | * @access public |
||
| 434 | * @param string $content |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | public static function event_venues( $content ) { |
||
| 443 | |||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * _add_additional_content_filters |
||
| 448 | * |
||
| 449 | * @access private |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | private static function _add_additional_excerpt_filters() { |
||
| 457 | |||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * _add_additional_content_filters |
||
| 462 | * |
||
| 463 | * @access private |
||
| 464 | * @return void |
||
| 465 | */ |
||
| 466 | private static function _add_additional_content_filters() { |
||
| 471 | |||
| 472 | |||
| 473 | |||
| 474 | /** |
||
| 475 | * _remove_additional_events_archive_filters |
||
| 476 | * |
||
| 477 | * @access private |
||
| 478 | * @return void |
||
| 479 | */ |
||
| 480 | private static function _remove_additional_events_archive_filters() { |
||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * remove_all_events_archive_filters |
||
| 493 | * |
||
| 494 | * @access public |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public static function remove_all_events_archive_filters() { |
||
| 511 | |||
| 512 | |||
| 513 | |||
| 514 | |||
| 515 | |||
| 516 | |||
| 517 | /** |
||
| 518 | * load_event_list_assets |
||
| 519 | * |
||
| 520 | * @access public |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | View Code Duplication | public function load_event_list_assets() { |
|
| 534 | |||
| 535 | |||
| 536 | |||
| 537 | |||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * wp_enqueue_scripts |
||
| 542 | * |
||
| 543 | * @access public |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | public function wp_enqueue_scripts() { |
||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | |||
| 563 | |||
| 564 | /** |
||
| 565 | * template_settings_form |
||
| 566 | * |
||
| 567 | * @access public |
||
| 568 | * @static |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public static function template_settings_form() { |
||
| 586 | |||
| 587 | |||
| 588 | |||
| 589 | |||
| 590 | |||
| 591 | |||
| 592 | /** |
||
| 593 | * update_template_settings |
||
| 594 | * |
||
| 595 | * @access public |
||
| 596 | * @param EE_Template_Config $CFG |
||
| 597 | * @param EE_Request_Handler $REQ |
||
| 598 | * @return EE_Template_Config |
||
| 599 | */ |
||
| 600 | public static function update_template_settings( $CFG, $REQ ) { |
||
| 612 | |||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * event_list_css |
||
| 617 | * |
||
| 618 | * @access public |
||
| 619 | * @param string $extra_class |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | public static function event_list_css( $extra_class = '' ) { |
||
| 627 | |||
| 628 | |||
| 629 | |||
| 630 | |||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * event_categories |
||
| 635 | * |
||
| 636 | * @access public |
||
| 637 | * @return array |
||
| 638 | */ |
||
| 639 | public static function event_categories() { |
||
| 642 | |||
| 643 | |||
| 644 | |||
| 645 | /** |
||
| 646 | * display_description |
||
| 647 | * |
||
| 648 | * @access public |
||
| 649 | * @param $value |
||
| 650 | * @return bool |
||
| 651 | */ |
||
| 652 | public static function display_description( $value ) { |
||
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * display_ticket_selector |
||
| 661 | * |
||
| 662 | * @access public |
||
| 663 | * @return bool |
||
| 664 | */ |
||
| 665 | public static function display_ticket_selector() { |
||
| 669 | |||
| 670 | |||
| 671 | |||
| 672 | /** |
||
| 673 | * display_venue |
||
| 674 | * |
||
| 675 | * @access public |
||
| 676 | * @return bool |
||
| 677 | */ |
||
| 678 | public static function display_venue() { |
||
| 683 | |||
| 684 | |||
| 685 | /** |
||
| 686 | * display_datetimes |
||
| 687 | * |
||
| 688 | * @access public |
||
| 689 | * @return bool |
||
| 690 | */ |
||
| 691 | public static function display_datetimes() { |
||
| 695 | |||
| 696 | |||
| 697 | |||
| 698 | |||
| 699 | |||
| 700 | |||
| 701 | /** |
||
| 702 | * event_list_title |
||
| 703 | * |
||
| 704 | * @access public |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | public static function event_list_title() { |
||
| 710 | |||
| 711 | |||
| 712 | // GRAVEYARD |
||
| 713 | |||
| 714 | /** |
||
| 715 | * @since 4.4.0 |
||
| 716 | */ |
||
| 717 | public static function _doing_it_wrong_notice( $function = '' ) { |
||
| 729 | |||
| 730 | |||
| 731 | |||
| 732 | /** |
||
| 733 | * @deprecated |
||
| 734 | * @since 4.4.0 |
||
| 735 | */ |
||
| 736 | public function get_post_data() { |
||
| 740 | /** |
||
| 741 | * @deprecated |
||
| 742 | * @since 4.4.0 |
||
| 743 | */ |
||
| 744 | public function posts_fields( $SQL, WP_Query $wp_query ) { |
||
| 749 | /** |
||
| 750 | * @deprecated |
||
| 751 | * @since 4.4.0 |
||
| 752 | */ |
||
| 753 | public static function posts_fields_sql_for_orderby( $orderby_params = array() ) { |
||
| 758 | /** |
||
| 759 | * @deprecated |
||
| 760 | * @since 4.4.0 |
||
| 761 | */ |
||
| 762 | public function posts_join( $SQL, WP_Query $wp_query ) { |
||
| 767 | /** |
||
| 768 | * @deprecated |
||
| 769 | * @since 4.4.0 |
||
| 770 | */ |
||
| 771 | public static function posts_join_sql_for_terms( $join_terms = NULL ) { |
||
| 776 | /** |
||
| 777 | * @deprecated |
||
| 778 | * @since 4.4.0 |
||
| 779 | */ |
||
| 780 | public static function posts_join_for_orderby( $orderby_params = array() ) { |
||
| 785 | /** |
||
| 786 | * @deprecated |
||
| 787 | * @since 4.4.0 |
||
| 788 | */ |
||
| 789 | public function posts_where( $SQL, WP_Query $wp_query ) { |
||
| 794 | /** |
||
| 795 | * @deprecated |
||
| 796 | * @since 4.4.0 |
||
| 797 | */ |
||
| 798 | public static function posts_where_sql_for_show_expired( $show_expired = FALSE ) { |
||
| 803 | /** |
||
| 804 | * @deprecated |
||
| 805 | * @since 4.4.0 |
||
| 806 | */ |
||
| 807 | public static function posts_where_sql_for_event_category_slug( $event_category_slug = NULL ) { |
||
| 812 | /** |
||
| 813 | * @deprecated |
||
| 814 | * @since 4.4.0 |
||
| 815 | */ |
||
| 816 | public static function posts_where_sql_for_event_list_month( $month = NULL ) { |
||
| 821 | /** |
||
| 822 | * @deprecated |
||
| 823 | * @since 4.4.0 |
||
| 824 | */ |
||
| 825 | public function posts_orderby( $SQL, WP_Query $wp_query ) { |
||
| 830 | /** |
||
| 831 | * @deprecated |
||
| 832 | * @since 4.4.0 |
||
| 833 | */ |
||
| 834 | public static function posts_orderby_sql( $orderby_params = array(), $sort = 'ASC' ) { |
||
| 839 | |||
| 840 | |||
| 841 | |||
| 842 | } |
||
| 843 | |||
| 922 |
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 theSoncalls the wrong method in the parent class.