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 |
||
| 59 | class EEH_Template |
||
| 60 | { |
||
| 61 | |||
| 62 | private static $_espresso_themes = array(); |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * is_espresso_theme - returns TRUE or FALSE on whether the currently active WP theme is an espresso theme |
||
| 67 | * |
||
| 68 | * @return boolean |
||
| 69 | */ |
||
| 70 | public static function is_espresso_theme() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * load_espresso_theme_functions - if current theme is an espresso theme, or uses ee theme template parts, then |
||
| 77 | * load it's functions.php file ( if not already loaded ) |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public static function load_espresso_theme_functions() |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * get_espresso_themes - returns an array of Espresso Child themes located in the /templates/ directory |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public static function get_espresso_themes() |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * EEH_Template::get_template_part |
||
| 117 | * basically a copy of the WordPress get_template_part() function but uses EEH_Template::locate_template() instead, |
||
| 118 | * and doesn't add base versions of files so not a very useful function at all except that it adds familiarity PLUS |
||
| 119 | * filtering based off of the entire template part name |
||
| 120 | * |
||
| 121 | * @param string $slug The slug name for the generic template. |
||
| 122 | * @param string $name The name of the specialised template. |
||
| 123 | * @param array $template_args |
||
| 124 | * @param bool $return_string |
||
| 125 | * @return string the html output for the formatted money value |
||
| 126 | */ |
||
| 127 | public static function get_template_part( |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * locate_template |
||
| 148 | * locate a template file by looking in the following places, in the following order: |
||
| 149 | * <server path up to>/wp-content/themes/<current active WordPress theme>/ |
||
| 150 | * <assumed full absolute server path> |
||
| 151 | * <server path up to>/wp-content/uploads/espresso/templates/<current EE theme>/ |
||
| 152 | * <server path up to>/wp-content/uploads/espresso/templates/ |
||
| 153 | * <server path up to>/wp-content/plugins/<EE4 folder>/public/<current EE theme>/ |
||
| 154 | * <server path up to>/wp-content/plugins/<EE4 folder>/core/templates/<current EE theme>/ |
||
| 155 | * <server path up to>/wp-content/plugins/<EE4 folder>/ |
||
| 156 | * as soon as the template is found in one of these locations, it will be returned or loaded |
||
| 157 | * Example: |
||
| 158 | * You are using the WordPress Twenty Sixteen theme, |
||
| 159 | * and you want to customize the "some-event.template.php" template, |
||
| 160 | * which is located in the "/relative/path/to/" folder relative to the main EE plugin folder. |
||
| 161 | * Assuming WP is installed on your server in the "/home/public_html/" folder, |
||
| 162 | * EEH_Template::locate_template() will look at the following paths in order until the template is found: |
||
| 163 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
| 164 | * /relative/path/to/some-event.template.php |
||
| 165 | * /home/public_html/wp-content/uploads/espresso/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
| 166 | * /home/public_html/wp-content/uploads/espresso/templates/relative/path/to/some-event.template.php |
||
| 167 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/public/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
| 168 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/core/templates/Espresso_Arabica_2014/relative/path/to/some-event.template.php |
||
| 169 | * /home/public_html/wp-content/plugins/event-espresso-core-reg/relative/path/to/some-event.template.php |
||
| 170 | * Had you passed an absolute path to your template that was in some other location, |
||
| 171 | * ie: "/absolute/path/to/some-event.template.php" |
||
| 172 | * then the search would have been : |
||
| 173 | * /home/public_html/wp-content/themes/twentysixteen/some-event.template.php |
||
| 174 | * /absolute/path/to/some-event.template.php |
||
| 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 |
||
| 180 | * EEH_Template::display_template() method or simply return it |
||
| 181 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a |
||
| 182 | * string |
||
| 183 | * @param boolean $check_if_custom If TRUE, this flags this method to return boolean for whether this will |
||
| 184 | * generate a custom template or not. Used in places where you don't actually |
||
| 185 | * load the template, you just want to know if there's a custom version of it. |
||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public static function locate_template( |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * _find_common_base_path |
||
| 305 | * given two paths, this determines if there is a common base path between the two |
||
| 306 | * |
||
| 307 | * @param array $paths |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | protected static function _find_common_base_path($paths) |
||
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * load and display a template |
||
| 331 | * |
||
| 332 | * @param bool|string $template_path server path to the file to be loaded, including file name and extension |
||
| 333 | * @param array $template_args an array of arguments to be extracted for use in the template |
||
| 334 | * @param boolean $return_string whether to send output immediately to screen, or capture and return as a string |
||
| 335 | * @param bool $throw_exceptions if set to true, will throw an exception if the template is either |
||
| 336 | * not found or is not readable |
||
| 337 | * @return mixed string |
||
| 338 | * @throws \DomainException |
||
| 339 | */ |
||
| 340 | public static function display_template( |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * get_object_css_class - attempts to generate a css class based on the type of EE object passed |
||
| 393 | * |
||
| 394 | * @param EE_Base_Class $object the EE object the css class is being generated for |
||
| 395 | * @param string $prefix added to the beginning of the generated class |
||
| 396 | * @param string $suffix added to the end of the generated class |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | public static function get_object_css_class($object = null, $prefix = '', $suffix = '') |
||
| 422 | |||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * EEH_Template::format_currency |
||
| 427 | * This helper takes a raw float value and formats it according to the default config country currency settings, or |
||
| 428 | * the country currency settings from the supplied country ISO code |
||
| 429 | * |
||
| 430 | * @param float $amount raw money value |
||
| 431 | * @param boolean $return_raw whether to return the formatted float value only with no currency sign or code |
||
| 432 | * @param boolean $display_code whether to display the country code (USD). Default = TRUE |
||
| 433 | * @param string $CNT_ISO 2 letter ISO code for a country |
||
| 434 | * @param string $cur_code_span_class |
||
| 435 | * @return string the html output for the formatted money value |
||
| 436 | * @throws \EE_Error |
||
| 437 | */ |
||
| 438 | public static function format_currency( |
||
| 439 | $amount = null, |
||
| 440 | $return_raw = false, |
||
| 441 | $display_code = true, |
||
| 442 | $CNT_ISO = '', |
||
| 443 | $cur_code_span_class = 'currency-code' |
||
| 444 | ) { |
||
| 445 | // ensure amount was received |
||
| 446 | View Code Duplication | if ($amount === null) { |
|
| 447 | $msg = __('In order to format currency, an amount needs to be passed.', 'event_espresso'); |
||
| 448 | EE_Error::add_error($msg, __FILE__, __FUNCTION__, __LINE__); |
||
| 449 | return ''; |
||
| 450 | } |
||
| 451 | //ensure amount is float |
||
| 452 | $amount = apply_filters('FHEE__EEH_Template__format_currency__raw_amount', (float)$amount); |
||
| 453 | $CNT_ISO = apply_filters('FHEE__EEH_Template__format_currency__CNT_ISO', $CNT_ISO, $amount); |
||
| 454 | // filter raw amount (allows 0.00 to be changed to "free" for example) |
||
| 455 | $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount', $amount, $return_raw); |
||
| 456 | // still a number or was amount converted to a string like "free" ? |
||
| 457 | if (is_float($amount_formatted)) { |
||
| 458 | // was a country ISO code passed ? if so generate currency config object for that country |
||
| 459 | $mny = $CNT_ISO !== '' ? new EE_Currency_Config($CNT_ISO) : null; |
||
| 460 | // verify results |
||
| 461 | View Code Duplication | if ( ! $mny instanceof EE_Currency_Config) { |
|
| 462 | // set default config country currency settings |
||
| 463 | $mny = EE_Registry::instance()->CFG->currency instanceof EE_Currency_Config |
||
| 464 | ? EE_Registry::instance()->CFG->currency |
||
| 465 | : new EE_Currency_Config(); |
||
| 466 | } |
||
| 467 | // format float |
||
| 468 | $amount_formatted = number_format($amount, $mny->dec_plc, $mny->dec_mrk, $mny->thsnds); |
||
| 469 | // add formatting ? |
||
| 470 | if ( ! $return_raw) { |
||
| 471 | // add currency sign |
||
| 472 | if ($mny->sign_b4) { |
||
| 473 | if ($amount >= 0) { |
||
| 474 | $amount_formatted = $mny->sign . $amount_formatted; |
||
| 475 | } else { |
||
| 476 | $amount_formatted = '-' . $mny->sign . str_replace('-', '', $amount_formatted); |
||
| 477 | } |
||
| 478 | |||
| 479 | } else { |
||
| 480 | $amount_formatted = $amount_formatted . $mny->sign; |
||
| 481 | } |
||
| 482 | |||
| 483 | // filter to allow global setting of display_code |
||
| 484 | $display_code = apply_filters('FHEE__EEH_Template__format_currency__display_code', $display_code); |
||
| 485 | |||
| 486 | // add currency code ? |
||
| 487 | $amount_formatted = $display_code ? $amount_formatted . ' <span class="' . $cur_code_span_class . '">(' . $mny->code . ')</span>' : $amount_formatted; |
||
| 488 | } |
||
| 489 | // filter results |
||
| 490 | $amount_formatted = apply_filters('FHEE__EEH_Template__format_currency__amount_formatted', |
||
| 491 | $amount_formatted, $mny, $return_raw); |
||
| 492 | } |
||
| 493 | // clean up vars |
||
| 494 | unset($mny); |
||
| 495 | // return formatted currency amount |
||
| 496 | return $amount_formatted; |
||
| 497 | } |
||
| 498 | |||
| 499 | |||
| 500 | /** |
||
| 501 | * This function is used for outputting the localized label for a given status id in the schema requested (and |
||
| 502 | * possibly plural). The intended use of this function is only for cases where wanting a label outside of a |
||
| 503 | * related status model or model object (i.e. in documentation etc.) |
||
| 504 | * |
||
| 505 | * @param string $status_id Status ID matching a registered status in the esp_status table. If there is no |
||
| 506 | * match, then 'Unknown' will be returned. |
||
| 507 | * @param boolean $plural Whether to return plural or not |
||
| 508 | * @param string $schema 'UPPER', 'lower', or 'Sentence' |
||
| 509 | * @return string The localized label for the status id. |
||
| 510 | */ |
||
| 511 | public static function pretty_status($status_id, $plural = false, $schema = 'upper') |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * This helper just returns a button or link for the given parameters |
||
| 523 | * |
||
| 524 | * @param string $url the url for the link |
||
| 525 | * @param string $label What is the label you want displayed for the button |
||
| 526 | * @param string $class what class is used for the button (defaults to 'button-primary') |
||
| 527 | * @param string $icon |
||
| 528 | * @param string $title |
||
| 529 | * @return string the html output for the button |
||
| 530 | */ |
||
| 531 | public static function get_button_or_link($url, $label, $class = 'button-primary', $icon = '', $title = '') |
||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * This returns a generated link that will load the related help tab on admin pages. |
||
| 553 | * |
||
| 554 | * @param string $help_tab_id the id for the connected help tab |
||
| 555 | * @param bool|string $page The page identifier for the page the help tab is on |
||
| 556 | * @param bool|string $action The action (route) for the admin page the help tab is on. |
||
| 557 | * @param bool|string $icon_style (optional) include css class for the style you want to use for the help icon. |
||
| 558 | * @param bool|string $help_text (optional) send help text you want to use for the link if default not to be used |
||
| 559 | * @return string generated link |
||
| 560 | */ |
||
| 561 | public static function get_help_tab_link( |
||
| 586 | |||
| 587 | |||
| 588 | /** |
||
| 589 | * This helper generates the html structure for the jquery joyride plugin with the given params. |
||
| 590 | * |
||
| 591 | * @link http://zurb.com/playground/jquery-joyride-feature-tour-plugin |
||
| 592 | * @see EE_Admin_Page->_stop_callback() for the construct expected for the $stops param. |
||
| 593 | * @param EE_Help_Tour |
||
| 594 | * @return string html |
||
| 595 | */ |
||
| 596 | public static function help_tour_stops_generator(EE_Help_Tour $tour) |
||
| 635 | |||
| 636 | |||
| 637 | /** |
||
| 638 | * This is a helper method to generate a status legend for a given status array. |
||
| 639 | * Note this will only work if the incoming statuses have a key in the EEM_Status->localized_status() methods |
||
| 640 | * status_array. |
||
| 641 | * |
||
| 642 | * @param array $status_array array of statuses that will make up the legend. In format: |
||
| 643 | * array( |
||
| 644 | * 'status_item' => 'status_name' |
||
| 645 | * ) |
||
| 646 | * @param string $active_status This is used to indicate what the active status is IF that is to be highlighted in |
||
| 647 | * the legend. |
||
| 648 | * @throws EE_Error |
||
| 649 | * @return string html structure for status. |
||
| 650 | */ |
||
| 651 | public static function status_legend($status_array, $active_status = '') |
||
| 681 | |||
| 682 | |||
| 683 | /** |
||
| 684 | * Gets HTML for laying out a deeply-nested array (and objects) in a format |
||
| 685 | * that's nice for presenting in the wp admin |
||
| 686 | * |
||
| 687 | * @param mixed $data |
||
| 688 | * @return string |
||
| 689 | */ |
||
| 690 | public static function layout_array_as_table($data) |
||
| 735 | |||
| 736 | |||
| 737 | /** |
||
| 738 | * wrapper for self::get_paging_html() that simply echos the generated paging html |
||
| 739 | * |
||
| 740 | * @since 4.4.0 |
||
| 741 | * @see self:get_paging_html() for argument docs. |
||
| 742 | * @param $total_items |
||
| 743 | * @param $current |
||
| 744 | * @param $per_page |
||
| 745 | * @param $url |
||
| 746 | * @param bool $show_num_field |
||
| 747 | * @param string $paged_arg_name |
||
| 748 | * @param array $items_label |
||
| 749 | * @return string |
||
| 750 | */ |
||
| 751 | public static function paging_html( |
||
| 763 | |||
| 764 | |||
| 765 | /** |
||
| 766 | * A method for generating paging similar to WP_List_Table |
||
| 767 | * |
||
| 768 | * @since 4.4.0 |
||
| 769 | * @see wp-admin/includes/class-wp-list-table.php WP_List_Table::pagination() |
||
| 770 | * @param integer $total_items How many total items there are to page. |
||
| 771 | * @param integer $current What the current page is. |
||
| 772 | * @param integer $per_page How many items per page. |
||
| 773 | * @param string $url What the base url for page links is. |
||
| 774 | * @param boolean $show_num_field Whether to show the input for changing page number. |
||
| 775 | * @param string $paged_arg_name The name of the key for the paged query argument. |
||
| 776 | * @param array $items_label An array of singular/plural values for the items label: |
||
| 777 | * array( |
||
| 778 | * 'single' => 'item', |
||
| 779 | * 'plural' => 'items' |
||
| 780 | * ) |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | public static function get_paging_html( |
||
| 901 | |||
| 902 | |||
| 903 | /** |
||
| 904 | * @param string $wrap_class |
||
| 905 | * @param string $wrap_id |
||
| 906 | * @return string |
||
| 907 | */ |
||
| 908 | public static function powered_by_event_espresso($wrap_class = '', $wrap_id = '', array $query_args = array()) |
||
| 955 | |||
| 956 | |||
| 957 | } //end EEH_Template class |
||
| 958 | |||
| 992 | } |
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: