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 EEH_Template 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 EEH_Template, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class EEH_Template { |
||
63 | |||
64 | private static $_espresso_themes = array(); |
||
65 | |||
66 | |||
67 | /** |
||
68 | * is_espresso_theme - returns TRUE or FALSE on whether the currently active WP theme is an espresso theme |
||
69 | * |
||
70 | * @return boolean |
||
71 | */ |
||
72 | public static function is_espresso_theme() { |
||
73 | return wp_get_theme()->get( 'TextDomain' ) == 'event_espresso' ? TRUE : FALSE; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * load_espresso_theme_functions - if current theme is an espresso theme, or uses ee theme template parts, then load it's functions.php file ( if not already loaded ) |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | public static function load_espresso_theme_functions() { |
||
82 | if ( ! defined( 'EE_THEME_FUNCTIONS_LOADED' )) { |
||
83 | if ( is_readable( EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php' )) { |
||
84 | require_once( EE_PUBLIC . EE_Config::get_current_theme() . DS . 'functions.php' ); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * get_espresso_themes - returns an array of Espresso Child themes located in the /templates/ directory |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public static function get_espresso_themes() { |
||
96 | if ( empty( EEH_Template::$_espresso_themes )) { |
||
97 | $espresso_themes = glob( EE_PUBLIC . '*', GLOB_ONLYDIR ); |
||
98 | if ( empty( $espresso_themes ) ) { |
||
99 | return array(); |
||
100 | } |
||
101 | if (( $key = array_search( 'global_assets', $espresso_themes )) !== FALSE ) { |
||
102 | unset( $espresso_themes[ $key ] ); |
||
103 | } |
||
104 | EEH_Template::$_espresso_themes = array(); |
||
105 | foreach ( $espresso_themes as $espresso_theme ) { |
||
106 | EEH_Template::$_espresso_themes[ basename( $espresso_theme ) ] = $espresso_theme; |
||
107 | } |
||
108 | } |
||
109 | return EEH_Template::$_espresso_themes; |
||
110 | } |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * EEH_Template::get_template_part |
||
116 | * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, and doesn't add base versions of files |
||
117 | * so not a very useful function at all except that it adds familiarity PLUS filtering based off of the entire template part name |
||
118 | * |
||
119 | * @param string $slug The slug name for the generic template. |
||
120 | * @param string $name The name of the specialised template. |
||
121 | * @param array $template_args |
||
122 | * @param bool $return_string |
||
123 | * @return string the html output for the formatted money value |
||
124 | */ |
||
125 | public static function get_template_part( $slug = NULL, $name = NULL, $template_args = array(), $return_string = FALSE ) { |
||
126 | do_action( "get_template_part_{$slug}-{$name}", $slug, $name ); |
||
127 | $templates = array(); |
||
128 | $name = (string) $name; |
||
129 | if ( $name != '' ) { |
||
130 | $templates[] = "{$slug}-{$name}.php"; |
||
131 | } |
||
132 | // allow template parts to be turned off via something like: add_filter( 'FHEE__content_espresso_events_tickets_template__display_datetimes', '__return_false' ); |
||
133 | if ( apply_filters( "FHEE__EEH_Template__get_template_part__display__{$slug}_{$name}", TRUE )) { |
||
134 | EEH_Template::locate_template( $templates, $template_args, TRUE, $return_string ); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * locate_template |
||
142 | * |
||
143 | * locate a template file by looking in the following places, in the following order: |
||
144 | * <server path up to>/wp-content/themes/<current active WordPress theme>/ |
||
145 | * <assumed full absolute server path> |
||
146 | * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ |
||
147 | * <server path up to>/wp-content/uploads/espresso/templates/ |
||
148 | * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ |
||
149 | * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ |
||
150 | * <server path up to>/wp-content/plugins/<EE4 folder>/ |
||
151 | * as soon as the template is found in one of these locations, it will be returned or loaded |
||
152 | * |
||
153 | * Example: |
||
154 | * You are using the WordPress Twenty Sixteen theme, |
||
155 | * and you want to customize the "some-event.template.php" template, |
||
156 | * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. |
||
157 | * Assuming WP is installed on your server in the "/home/public_html/" folder, |
||
158 | * EEH_Template::locate_template() will look at the following paths in order until the template is found: |
||
159 | * |
||
160 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
161 | * /relative/path/to/some-event.template.php |
||
162 | * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
163 | * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php |
||
164 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
165 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
166 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php |
||
167 | * |
||
168 | * Had you passed an absolute path to your template that was in some other location, |
||
169 | * ie: "/absolute/path/to/some-event.template.php" |
||
170 | * then the search would have been : |
||
171 | * |
||
172 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
173 | * /absolute/path/to/some-event.template.php |
||
174 | * |
||
175 | * and stopped there upon finding it in the second location |
||
176 | * |
||
177 | * @param array|string $templates array of template file names including extension (or just a single string) |
||
178 | * @param array $template_args an array of arguments to be extracted for use in the template |
||
179 | * @param boolean $load whether to pass the located template path on to the EEH_Template::display_template() method or simply return it |
||
180 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
||
181 | * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will generate a custom template or not. |
||
182 | * Used in places where you don't actually load the template, you just want to know if there's a custom version of it. |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public static function locate_template( $templates = array(), $template_args = array(), $load = TRUE, $return_string = TRUE, $check_if_custom = FALSE ) { |
||
290 | |||
291 | |||
292 | |||
293 | /** |
||
294 | * _find_common_base_path |
||
295 | * |
||
296 | * given two paths, this determines if there is a common base path between the two |
||
297 | * |
||
298 | * @param array $paths |
||
299 | * @return string |
||
300 | */ |
||
301 | protected static function _find_common_base_path( $paths ) { |
||
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * load and display a template |
||
322 | * @param bool|string $template_path server path to the file to be loaded, including file name and extension |
||
323 | * @param array $template_args an array of arguments to be extracted for use in the template |
||
324 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
||
325 | * @return mixed string |
||
326 | */ |
||
327 | public static function display_template( $template_path = FALSE, $template_args = array(), $return_string = FALSE ) { |
||
363 | |||
364 | |||
365 | |||
366 | |||
367 | |||
368 | /** |
||
369 | * get_object_css_class - attempts to generate a css class based on the type of EE object passed |
||
370 | * |
||
371 | * |
||
372 | * @param EE_Base_Class $object the EE object the css class is being generated for |
||
373 | * @param string $prefix added to the beginning of the generated class |
||
374 | * @param string $suffix added to the end of the generated class |
||
375 | * @return string |
||
376 | */ |
||
377 | public static function get_object_css_class( $object = NULL, $prefix = '', $suffix = '' ) { |
||
399 | |||
400 | |||
401 | |||
402 | /** |
||
403 | * EEH_Template::format_currency |
||
404 | * This helper takes a raw float value and formats it according to the default config country currency settings, or the country currency settings from the supplied country ISO code |
||
405 | * |
||
406 | * @param float $amount raw money value |
||
407 | * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
||
408 | * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
||
409 | * @param string $CNT_ISO 2 letter ISO code for a country |
||
410 | * @param string $cur_code_span_class |
||
411 | * @return string the html output for the formatted money value |
||
412 | */ |
||
413 | public static function format_currency( $amount = NULL, $return_raw = FALSE, $display_code = TRUE, $CNT_ISO = '', $cur_code_span_class = 'currency-code' ) { |
||
460 | |||
461 | |||
462 | |||
463 | |||
464 | /** |
||
465 | * This function is used for outputting the localized label for a given status id in the schema requested (and possibly plural). The intended use of this function is only for cases where wanting a label outside of a related status model or model object (i.e. in documentation etc.) |
||
466 | * @param string $status_id Status ID matching a registered status in the esp_status table. If there is no match, then 'Unknown' will be returned. |
||
467 | * @param boolean $plural Whether to return plural or not |
||
468 | * @param string $schema 'UPPER', 'lower', or 'Sentence' |
||
469 | * @return string The localized label for the status id. |
||
470 | */ |
||
471 | public static function pretty_status( $status_id, $plural = FALSE, $schema = 'upper' ) { |
||
477 | |||
478 | |||
479 | |||
480 | /** |
||
481 | * This helper just returns a button or link for the given parameters |
||
482 | * @param string $url the url for the link |
||
483 | * @param string $label What is the label you want displayed for the button |
||
484 | * @param string $class what class is used for the button (defaults to 'button-primary') |
||
485 | * @param string $icon |
||
486 | * @return string the html output for the button |
||
487 | */ |
||
488 | public static function get_button_or_link( $url, $label, $class = 'button-primary', $icon = '' ) { |
||
493 | |||
494 | |||
495 | |||
496 | /** |
||
497 | * This returns a generated link that will load the related help tab on admin pages. |
||
498 | * |
||
499 | * |
||
500 | * @param string $help_tab_id the id for the connected help tab |
||
501 | * @param bool|string $page The page identifier for the page the help tab is on |
||
502 | * @param bool|string $action The action (route) for the admin page the help tab is on. |
||
503 | * @param bool|string $icon_style (optional) include css class for the style you want to use for the help icon. |
||
504 | * @param bool|string $help_text (optional) send help text you want to use for the link if default not to be used |
||
505 | * @return string generated link |
||
506 | */ |
||
507 | public static function get_help_tab_link( $help_tab_id, $page = FALSE, $action = FALSE, $icon_style = FALSE, $help_text = FALSE ) { |
||
523 | |||
524 | |||
525 | |||
526 | /** |
||
527 | * This helper generates the html structure for the jquery joyride plugin with the given params. |
||
528 | * @link http://zurb.com/playground/jquery-joyride-feature-tour-plugin |
||
529 | * @see EE_Admin_Page->_stop_callback() for the construct expected for the $stops param. |
||
530 | * @param EE_Help_Tour |
||
531 | * @return string html |
||
532 | */ |
||
533 | public static function help_tour_stops_generator( EE_Help_Tour $tour ) { |
||
571 | |||
572 | |||
573 | |||
574 | /** |
||
575 | * This is a helper method to generate a status legend for a given status array. |
||
576 | * Note this will only work if the incoming statuses have a key in the EEM_Status->localized_status() methods status_array. |
||
577 | * |
||
578 | * @param array $status_array array of statuses that will make up the legend. In format: |
||
579 | * array( |
||
580 | * 'status_item' => 'status_name' |
||
581 | * ) |
||
582 | * @param string $active_status This is used to indicate what the active status is IF that is to be highlighted in the legend. |
||
583 | * @throws EE_Error |
||
584 | * @return string html structure for status. |
||
585 | */ |
||
586 | public static function status_legend( $status_array, $active_status = '' ) { |
||
613 | |||
614 | |||
615 | |||
616 | /** |
||
617 | * Gets HTML for laying out a deeply-nested array (and objects) in a format |
||
618 | * that's nice for presenting in the wp admin |
||
619 | * @param mixed $data |
||
620 | * @return string |
||
621 | */ |
||
622 | public static function layout_array_as_table($data) { |
||
667 | |||
668 | |||
669 | |||
670 | /** |
||
671 | * wrapper for self::get_paging_html() that simply echos the generated paging html |
||
672 | * |
||
673 | * @since 4.4.0 |
||
674 | * @see self:get_paging_html() for argument docs. |
||
675 | * @param $total_items |
||
676 | * @param $current |
||
677 | * @param $per_page |
||
678 | * @param $url |
||
679 | * @param bool $show_num_field |
||
680 | * @param string $paged_arg_name |
||
681 | * @param array $items_label |
||
682 | * |
||
683 | * @return string |
||
684 | */ |
||
685 | public static function paging_html( $total_items, $current, $per_page, $url, $show_num_field = TRUE, $paged_arg_name = 'paged', $items_label = array() ) { |
||
688 | |||
689 | |||
690 | |||
691 | /** |
||
692 | * A method for generating paging similar to WP_List_Table |
||
693 | * |
||
694 | * @since 4.4.0 |
||
695 | * @see wp-admin/includes/class-wp-list-table.php WP_List_Table::pagination() |
||
696 | * |
||
697 | * @param integer $total_items How many total items there are to page. |
||
698 | * @param integer $current What the current page is. |
||
699 | * @param integer $per_page How many items per page. |
||
700 | * @param string $url What the base url for page links is. |
||
701 | * @param boolean $show_num_field Whether to show the input for changing page number. |
||
702 | * @param string $paged_arg_name The name of the key for the paged query argument. |
||
703 | * @param array $items_label An array of singular/plural values for the items label: |
||
704 | * array( |
||
705 | * 'single' => 'item', |
||
706 | * 'plural' => 'items' |
||
707 | * ) |
||
708 | * @return string |
||
709 | */ |
||
710 | public static function get_paging_html( $total_items, $current, $per_page, $url, $show_num_field = TRUE, $paged_arg_name = 'paged', $items_label = array() ) { |
||
818 | |||
819 | |||
820 | } //end EEH_Template class |
||
821 | |||
861 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: