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 FrmDeprecated 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 FrmDeprecated, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class FrmDeprecated { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @deprecated 2.3 |
||
| 16 | */ |
||
| 17 | public static function deprecated( $function, $version ) { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @deprecated 4.0 |
||
| 23 | */ |
||
| 24 | public static function new_form( $values = array() ) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @deprecated 4.0 |
||
| 46 | */ |
||
| 47 | public static function update_order() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @deprecated 4.0 |
||
| 61 | * @param array $values - The form array |
||
| 62 | */ |
||
| 63 | public static function builder_submit_button( $values ) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @deprecated 3.04.03 |
||
| 81 | */ |
||
| 82 | public static function get_licenses() { |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Don't allow subsite addon licenses to be fetched |
||
| 114 | * unless the current user has super admin permissions |
||
| 115 | * |
||
| 116 | * @since 2.03.10 |
||
| 117 | * @deprecated 3.04.03 |
||
| 118 | */ |
||
| 119 | private static function allow_autofill() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @deprecated 3.04.03 |
||
| 132 | */ |
||
| 133 | private static function send_api_request( $url, $transient = array() ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @since 3.04.03 |
||
| 160 | * @deprecated 3.06 |
||
| 161 | * @codeCoverageIgnore |
||
| 162 | */ |
||
| 163 | public static function get_pro_updater() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @since 3.04.02 |
||
| 171 | * @deprecated 4.09.01 |
||
| 172 | */ |
||
| 173 | public static function ajax_install_addon() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @since 4.06.02 |
||
| 181 | * @deprecated 4.09.01 |
||
| 182 | * @codeCoverageIgnore |
||
| 183 | */ |
||
| 184 | public static function ajax_multiple_addons() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @since 3.04.03 |
||
| 192 | * @deprecated 3.06 |
||
| 193 | * @codeCoverageIgnore |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public static function error_for_license( $license ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @since 3.04.03 |
||
| 204 | * @deprecated 3.06 |
||
| 205 | */ |
||
| 206 | public static function reset_cached_addons( $license = '' ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @since 3.04.03 |
||
| 214 | * @deprecated 3.06 |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public static function get_cache_key( $license ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @since 3.04.03 |
||
| 225 | * @deprecated 3.06 |
||
| 226 | * @codeCoverageIgnore |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | public static function get_addon_info( $license = '' ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add a filter to shorten the EDD filename for Formidable plugin, and add-on, updates |
||
| 237 | * |
||
| 238 | * @since 2.03.08 |
||
| 239 | * @deprecated 3.04.03 |
||
| 240 | * |
||
| 241 | * @param boolean $return |
||
| 242 | * @param string $package |
||
| 243 | * |
||
| 244 | * @return boolean |
||
| 245 | */ |
||
| 246 | public static function add_shorten_edd_filename_filter( $return, $package ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Shorten the EDD filename for automatic updates |
||
| 258 | * Decreases size of file path so file path limit is not hit on Windows servers |
||
| 259 | * |
||
| 260 | * @since 2.03.08 |
||
| 261 | * @deprecated 3.04.03 |
||
| 262 | * |
||
| 263 | * @param string $filename |
||
| 264 | * @param string $ext |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public static function shorten_edd_filename( $filename, $ext ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Filter shortcodes in text widgets |
||
| 279 | * |
||
| 280 | * @deprecated 2.5.4 |
||
| 281 | */ |
||
| 282 | public static function widget_text_filter( $content ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Used to filter shortcode in text widgets |
||
| 290 | * |
||
| 291 | * @deprecated 2.5.4 |
||
| 292 | */ |
||
| 293 | public static function widget_text_filter_callback( $matches ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Deprecated in favor of wpmu_upgrade_site |
||
| 300 | * |
||
| 301 | * @deprecated 2.3 |
||
| 302 | */ |
||
| 303 | public static function front_head() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @deprecated 3.0.04 |
||
| 312 | */ |
||
| 313 | public static function activation_install() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Routes for wordpress pages -- we're just replacing content |
||
| 322 | * |
||
| 323 | * @deprecated 3.0 |
||
| 324 | */ |
||
| 325 | public static function page_route( $content ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @deprecated 1.07.05 |
||
| 338 | */ |
||
| 339 | public static function get_form_shortcode( $atts ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @deprecated 1.07.05 |
||
| 346 | */ |
||
| 347 | public static function show_form( $id = '', $key = '', $title = false, $description = false ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @deprecated 1.07.05 |
||
| 354 | */ |
||
| 355 | public static function get_form( $filename, $form, $title, $description ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @deprecated 3.0 |
||
| 362 | */ |
||
| 363 | public static function edit_name( $field = 'name', $id = '' ) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Load a single field in the form builder along with all needed variables |
||
| 395 | * |
||
| 396 | * @deprecated 3.0 |
||
| 397 | * |
||
| 398 | * @param int $field_id |
||
| 399 | * @param array $values |
||
| 400 | * @param int $form_id |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | public static function include_single_field( $field_id, $values, $form_id = 0 ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @deprecated 3.0 |
||
| 415 | */ |
||
| 416 | public static function bulk_create_template( $ids ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @deprecated 2.03 |
||
| 429 | */ |
||
| 430 | public static function register_pro_scripts() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @deprecated 3.0 |
||
| 439 | */ |
||
| 440 | public static function edit_key() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @deprecated 3.0 |
||
| 449 | */ |
||
| 450 | public static function edit_description() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @deprecated 3.0 |
||
| 459 | */ |
||
| 460 | private static function edit_in_place_value( $field ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @deprecated 1.07.05 |
||
| 477 | */ |
||
| 478 | public static function add_default_templates( $path, $default = true, $template = true ) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @deprecated 3.01 |
||
| 519 | */ |
||
| 520 | public static function sanitize_array( &$values ) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Prepare and save settings in styles and actions |
||
| 527 | * |
||
| 528 | * @param array $settings |
||
| 529 | * @param string $group |
||
| 530 | * |
||
| 531 | * @since 2.0.6 |
||
| 532 | * @deprecated 2.05.06 |
||
| 533 | */ |
||
| 534 | public static function save_settings( $settings, $group ) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Since actions are JSON encoded, we don't want any filters messing with it. |
||
| 541 | * Remove the filters and then add them back in case any posts or views are |
||
| 542 | * also being imported. |
||
| 543 | * |
||
| 544 | * Used when saving form actions and styles |
||
| 545 | * |
||
| 546 | * @since 2.0.4 |
||
| 547 | * @deprecated 2.05.06 |
||
| 548 | */ |
||
| 549 | public static function save_json_post( $settings ) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Check cache before fetching values and saving to cache |
||
| 556 | * |
||
| 557 | * @since 2.0 |
||
| 558 | * @deprecated 2.05.06 |
||
| 559 | * |
||
| 560 | * @param string $cache_key The unique name for this cache |
||
| 561 | * @param string $group The name of the cache group |
||
| 562 | * @param string $query If blank, don't run a db call |
||
| 563 | * @param string $type The wpdb function to use with this query |
||
| 564 | * @return mixed $results The cache or query results |
||
| 565 | */ |
||
| 566 | public static function check_cache( $cache_key, $group = '', $query = '', $type = 'get_var', $time = 300 ) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @deprecated 2.05.06 |
||
| 573 | */ |
||
| 574 | public static function set_cache( $cache_key, $results, $group = '', $time = 300 ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Keep track of the keys cached in each group so they can be deleted |
||
| 581 | * in Redis and Memcache |
||
| 582 | * @deprecated 2.05.06 |
||
| 583 | */ |
||
| 584 | public static function add_key_to_group_cache( $key, $group ) { |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @deprecated 2.05.06 |
||
| 591 | */ |
||
| 592 | public static function get_group_cached_keys( $group ) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @since 2.0 |
||
| 599 | * @deprecated 2.05.06 |
||
| 600 | * @param string $cache_key |
||
| 601 | */ |
||
| 602 | public static function delete_cache_and_transient( $cache_key, $group = 'default' ) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * @since 2.0 |
||
| 609 | * @deprecated 2.05.06 |
||
| 610 | * |
||
| 611 | * @param string $group The name of the cache group |
||
| 612 | */ |
||
| 613 | public static function cache_delete_group( $group ) { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Added for < WP 4.0 compatability |
||
| 620 | * |
||
| 621 | * @since 1.07.10 |
||
| 622 | * @deprecated 2.05.06 |
||
| 623 | * |
||
| 624 | * @param string $term The value to escape |
||
| 625 | * @return string The escaped value |
||
| 626 | */ |
||
| 627 | public static function esc_like( $term ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param string $order_query |
||
| 634 | * @deprecated 2.05.06 |
||
| 635 | */ |
||
| 636 | public static function esc_order( $order_query ) { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Make sure this is ordering by either ASC or DESC |
||
| 643 | * @deprecated 2.05.06 |
||
| 644 | */ |
||
| 645 | public static function esc_order_by( &$order_by ) { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * @param string $limit |
||
| 652 | * @deprecated 2.05.06 |
||
| 653 | */ |
||
| 654 | public static function esc_limit( $limit ) { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get an array of values ready to go through $wpdb->prepare |
||
| 661 | * @since 2.0 |
||
| 662 | * @deprecated 2.05.06 |
||
| 663 | */ |
||
| 664 | public static function prepare_array_values( $array, $type = '%s' ) { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * @deprecated 2.05.06 |
||
| 671 | */ |
||
| 672 | public static function prepend_and_or_where( $starts_with = ' WHERE ', $where = '' ) { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @deprecated 2.05.06 |
||
| 679 | */ |
||
| 680 | public static function upgrade() { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @deprecated 2.05.06 |
||
| 687 | */ |
||
| 688 | public static function collation() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * @deprecated 2.05.06 |
||
| 695 | */ |
||
| 696 | public static function uninstall() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * @deprecated 3.0 |
||
| 703 | * |
||
| 704 | * @param string $html |
||
| 705 | * @param array $field |
||
| 706 | * @param array $errors |
||
| 707 | * @param object $form |
||
| 708 | * @param array $args |
||
| 709 | * |
||
| 710 | * @return string |
||
| 711 | */ |
||
| 712 | public static function replace_shortcodes( $html, $field, $errors = array(), $form = false, $args = array() ) { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @deprecated 3.0 |
||
| 720 | */ |
||
| 721 | public static function get_default_field_opts( $type, $field = null, $limit = false ) { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @deprecated 2.02.07 |
||
| 735 | */ |
||
| 736 | public static function dropdown_categories( $args ) { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * @deprecated 3.0 |
||
| 751 | */ |
||
| 752 | public static function remove_inline_conditions( $no_vars, $code, $replace_with, &$html ) { |
||
| 756 | |||
| 757 | /** |
||
| 758 | * @deprecated 3.0 |
||
| 759 | */ |
||
| 760 | public static function get_shortcode_tag( $shortcodes, $short_key, $args ) { |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @deprecated 3.01 |
||
| 767 | */ |
||
| 768 | public static function get_sigle_label_postitions() { |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @deprecated 3.02.03 |
||
| 775 | */ |
||
| 776 | public static function jquery_themes() { |
||
| 809 | |||
| 810 | /** |
||
| 811 | * @deprecated 3.02.03 |
||
| 812 | */ |
||
| 813 | public static function enqueue_jquery_css() { |
||
| 822 | |||
| 823 | /** |
||
| 824 | * @deprecated 3.02.03 |
||
| 825 | */ |
||
| 826 | public static function jquery_css_url( $theme_css ) { |
||
| 849 | |||
| 850 | /** |
||
| 851 | * @deprecated 3.02.03 |
||
| 852 | */ |
||
| 853 | public static function get_form_for_page() { |
||
| 868 | |||
| 869 | /** |
||
| 870 | * @deprecated 3.0 |
||
| 871 | */ |
||
| 872 | public static function validate_url_field( &$errors, $field, $value, $args ) { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * @deprecated 3.0 |
||
| 884 | */ |
||
| 885 | View Code Duplication | public static function validate_email_field( &$errors, $field, $value, $args ) { |
|
| 894 | |||
| 895 | /** |
||
| 896 | * @deprecated 3.0 |
||
| 897 | */ |
||
| 898 | View Code Duplication | public static function validate_number_field( &$errors, $field, $value, $args ) { |
|
| 908 | |||
| 909 | /** |
||
| 910 | * @deprecated 3.0 |
||
| 911 | */ |
||
| 912 | View Code Duplication | public static function validate_recaptcha( &$errors, $field, $args ) { |
|
| 921 | } |
||
| 922 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.