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 FrmAppController 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 FrmAppController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class FrmAppController { |
||
| 7 | |||
| 8 | public static function menu() { |
||
| 9 | FrmAppHelper::maybe_add_permissions(); |
||
| 10 | if ( ! current_user_can( 'frm_view_forms' ) ) { |
||
| 11 | return; |
||
| 12 | } |
||
| 13 | |||
| 14 | $menu_name = FrmAppHelper::get_menu_name(); |
||
| 15 | add_menu_page( 'Formidable', $menu_name, 'frm_view_forms', 'formidable', 'FrmFormsController::route', self::menu_icon(), self::get_menu_position() ); |
||
| 16 | } |
||
| 17 | |||
| 18 | private static function get_menu_position() { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @since 3.05 |
||
| 24 | */ |
||
| 25 | private static function menu_icon() { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @since 3.0 |
||
| 39 | */ |
||
| 40 | public static function add_admin_class( $classes ) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @since 4.0 |
||
| 68 | */ |
||
| 69 | private static function get_os() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @since 3.0 |
||
| 84 | */ |
||
| 85 | private static function is_white_page() { |
||
| 86 | $white_pages = array( |
||
| 87 | 'formidable', |
||
| 88 | 'formidable-entries', |
||
| 89 | 'formidable-views', |
||
| 90 | 'formidable-pro-upgrade', |
||
| 91 | 'formidable-addons', |
||
| 92 | 'formidable-import', |
||
| 93 | 'formidable-settings', |
||
| 94 | 'formidable-styles', |
||
| 95 | 'formidable-styles2', |
||
| 96 | 'formidable-inbox', |
||
| 97 | ); |
||
| 98 | |||
| 99 | $get_page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 100 | $is_white_page = in_array( $get_page, $white_pages ); |
||
| 101 | |||
| 102 | if ( ! $is_white_page ) { |
||
| 103 | $screen = get_current_screen(); |
||
| 104 | $is_white_page = ( $screen && strpos( $screen->id, 'frm_display' ) !== false ); |
||
| 105 | } |
||
| 106 | |||
| 107 | return $is_white_page; |
||
| 108 | } |
||
| 109 | |||
| 110 | public static function load_wp_admin_style() { |
||
| 111 | FrmAppHelper::load_font_style(); |
||
| 112 | } |
||
| 113 | |||
| 114 | public static function get_form_nav( $form, $show_nav = false, $title = 'show' ) { |
||
| 115 | $show_nav = FrmAppHelper::get_param( 'show_nav', $show_nav, 'get', 'absint' ); |
||
| 116 | if ( empty( $show_nav ) || ! $form ) { |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | FrmForm::maybe_get_form( $form ); |
||
| 121 | if ( ! is_object( $form ) ) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | $id = $form->id; |
||
| 126 | $current_page = self::get_current_page(); |
||
| 127 | $nav_items = self::get_form_nav_items( $form ); |
||
| 128 | |||
| 129 | include( FrmAppHelper::plugin_path() . '/classes/views/shared/form-nav.php' ); |
||
| 130 | } |
||
| 131 | |||
| 132 | private static function get_current_page() { |
||
| 133 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 134 | $post_type = FrmAppHelper::simple_get( 'post_type', 'sanitize_title', 'None' ); |
||
| 135 | $current_page = isset( $_GET['page'] ) ? $page : $post_type; |
||
| 136 | |||
| 137 | if ( FrmAppHelper::is_view_builder_page() ) { |
||
| 138 | $current_page = 'frm_display'; |
||
| 139 | } |
||
| 140 | |||
| 141 | return $current_page; |
||
| 142 | } |
||
| 143 | |||
| 144 | private static function get_form_nav_items( $form ) { |
||
| 145 | $id = $form->parent_form_id ? $form->parent_form_id : $form->id; |
||
| 146 | |||
| 147 | $nav_items = array( |
||
| 148 | array( |
||
| 149 | 'link' => FrmForm::get_edit_link( $id ), |
||
| 150 | 'label' => __( 'Build', 'formidable' ), |
||
| 151 | 'current' => array( 'edit', 'new', 'duplicate' ), |
||
| 152 | 'page' => 'formidable', |
||
| 153 | 'permission' => 'frm_edit_forms', |
||
| 154 | ), |
||
| 155 | array( |
||
| 156 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=settings&id=' . absint( $id ) ), |
||
| 157 | 'label' => __( 'Settings', 'formidable' ), |
||
| 158 | 'current' => array( 'settings' ), |
||
| 159 | 'page' => 'formidable', |
||
| 160 | 'permission' => 'frm_edit_forms', |
||
| 161 | ), |
||
| 162 | array( |
||
| 163 | 'link' => admin_url( 'admin.php?page=formidable-entries&frm-full=1&frm_action=list&form=' . absint( $id ) ), |
||
| 164 | 'label' => __( 'Entries', 'formidable' ), |
||
| 165 | 'current' => array(), |
||
| 166 | 'page' => 'formidable-entries', |
||
| 167 | 'permission' => 'frm_view_entries', |
||
| 168 | ), |
||
| 169 | ); |
||
| 170 | |||
| 171 | // Let people know reports and views exist. |
||
| 172 | if ( ! FrmAppHelper::pro_is_installed() ) { |
||
| 173 | $nav_items[] = array( |
||
| 174 | 'link' => admin_url( 'admin.php?page=formidable-views&frm-full=1&form=' . absint( $id ) ), |
||
| 175 | 'label' => __( 'Views', 'formidable' ), |
||
| 176 | 'current' => array(), |
||
| 177 | 'page' => 'formidable-views', |
||
| 178 | 'permission' => 'frm_view_entries', |
||
| 179 | 'atts' => array( |
||
| 180 | 'class' => 'frm_noallow', |
||
| 181 | ), |
||
| 182 | ); |
||
| 183 | $nav_items[] = array( |
||
| 184 | 'link' => admin_url( 'admin.php?page=formidable&frm_action=lite-reports&frm-full=1&form=' . absint( $id ) ), |
||
| 185 | 'label' => __( 'Reports', 'formidable' ), |
||
| 186 | 'current' => array( 'reports' ), |
||
| 187 | 'page' => 'formidable', |
||
| 188 | 'permission' => 'frm_view_entries', |
||
| 189 | 'atts' => array( |
||
| 190 | 'class' => 'frm_noallow', |
||
| 191 | ), |
||
| 192 | ); |
||
| 193 | } |
||
| 194 | |||
| 195 | $nav_args = array( |
||
| 196 | 'form_id' => $id, |
||
| 197 | 'form' => $form, |
||
| 198 | ); |
||
| 199 | |||
| 200 | return apply_filters( 'frm_form_nav_list', $nav_items, $nav_args ); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Adds a settings link to the plugins page |
||
| 204 | public static function settings_link( $links ) { |
||
| 210 | |||
| 211 | public static function pro_get_started_headline() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Add admin notices as needed for reviews |
||
| 218 | * |
||
| 219 | * @since 3.04.03 |
||
| 220 | */ |
||
| 221 | private static function review_request() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Save the request to hide the review |
||
| 228 | * |
||
| 229 | * @since 3.04.03 |
||
| 230 | */ |
||
| 231 | public static function dismiss_review() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @since 3.04.02 |
||
| 241 | */ |
||
| 242 | public static function include_upgrade_overlay() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @since 3.06.03 |
||
| 251 | */ |
||
| 252 | public static function upgrade_overlay_html() { |
||
| 253 | $is_pro = FrmAppHelper::pro_is_installed(); |
||
| 254 | $upgrade_link = array( |
||
| 269 | |||
| 270 | private static function new_form_overlay_html() { |
||
| 271 | FrmFormsController::before_list_templates(); |
||
| 272 | |||
| 273 | $plugin_path = FrmAppHelper::plugin_path(); |
||
| 274 | $path = $plugin_path . '/classes/views/frm-forms/'; |
||
| 275 | $expired = FrmFormsController::expired(); |
||
| 276 | $expiring = FrmAddonsController::is_license_expiring(); |
||
| 277 | $user = wp_get_current_user(); // $user used in leave-email.php to determine a default value for field |
||
| 278 | $view_path = $path . 'new-form-overlay/'; |
||
| 279 | $modal_class = ''; |
||
| 280 | $upgrade_link = FrmAppHelper::admin_upgrade_link( |
||
| 281 | array( |
||
| 282 | 'medium' => 'new-template', |
||
| 283 | 'content' => 'upgrade', |
||
| 284 | ) |
||
| 285 | ); |
||
| 286 | $renew_link = FrmAppHelper::admin_upgrade_link( |
||
| 287 | array( |
||
| 288 | 'medium' => 'new-template', |
||
| 289 | 'content' => 'renew', |
||
| 290 | ) |
||
| 291 | ); |
||
| 292 | $blocks_to_render = array(); |
||
| 293 | |||
| 294 | if ( ! FrmAppHelper::pro_is_installed() ) { |
||
| 295 | // avoid rendering the email and code blocks for users who have upgraded or have a free license already |
||
| 296 | $api = new FrmFormTemplateApi(); |
||
| 297 | if ( ! $api->get_free_license() ) { |
||
| 298 | array_push( $blocks_to_render, 'email', 'code' ); |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | // avoid rendering the upgrade block for users with elite |
||
| 303 | if ( 'elite' !== FrmAddonsController::license_type() ) { |
||
| 304 | $blocks_to_render[] = 'upgrade'; |
||
| 305 | } |
||
| 306 | |||
| 307 | // avoid rendering the renew block for users who are not currently expired |
||
| 308 | if ( $expired ) { |
||
| 309 | $blocks_to_render[] = 'renew'; |
||
| 310 | $modal_class = 'frm-expired'; |
||
| 311 | } elseif ( $expiring ) { |
||
| 312 | $modal_class = 'frm-expiring'; |
||
| 313 | } |
||
| 314 | |||
| 315 | include $path . 'new-form-overlay.php'; |
||
| 316 | } |
||
| 317 | |||
| 318 | public static function include_info_overlay() { |
||
| 324 | |||
| 325 | public static function info_overlay_html() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @since 3.04.02 |
||
| 331 | */ |
||
| 332 | public static function remove_upsells() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * If there are CURL problems on this server, wp_remote_post won't work for installing |
||
| 341 | * Use a javascript fallback instead. |
||
| 342 | * |
||
| 343 | * @since 2.0.3 |
||
| 344 | */ |
||
| 345 | public static function install_js_fallback() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Check if the database is outdated |
||
| 352 | * |
||
| 353 | * @since 2.0.1 |
||
| 354 | * @return boolean |
||
| 355 | */ |
||
| 356 | public static function needs_update() { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Check both version number and DB number for changes |
||
| 374 | * |
||
| 375 | * @since 3.0.04 |
||
| 376 | */ |
||
| 377 | public static function compare_for_update( $atts ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Check for database update and trigger js loading |
||
| 394 | * |
||
| 395 | * @since 2.0.1 |
||
| 396 | */ |
||
| 397 | public static function admin_init() { |
||
| 414 | |||
| 415 | public static function admin_js() { |
||
| 495 | |||
| 496 | public static function load_lang() { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Check if the styles are updated when a form is loaded on the front-end |
||
| 502 | * |
||
| 503 | * @since 3.0.1 |
||
| 504 | */ |
||
| 505 | public static function maybe_update_styles() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @since 3.0 |
||
| 513 | */ |
||
| 514 | public static function create_rest_routes() { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Make sure the install is only being run when we tell it to. |
||
| 534 | * We don't want to run manually by people calling the API. |
||
| 535 | * |
||
| 536 | * @since 4.06.02 |
||
| 537 | */ |
||
| 538 | public static function can_update_db() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Run silent upgrade on each site in the network during a network upgrade. |
||
| 544 | * Update database settings for all sites in a network during network upgrade process. |
||
| 545 | * |
||
| 546 | * @since 2.0.1 |
||
| 547 | * |
||
| 548 | * @param int $blog_id Blog ID. |
||
| 549 | */ |
||
| 550 | public static function network_upgrade_site( $blog_id = 0 ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @since 3.0 |
||
| 571 | */ |
||
| 572 | public static function api_install() { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Silent database upgrade (no redirect). |
||
| 588 | * Called via ajax request during network upgrade process. |
||
| 589 | * |
||
| 590 | * @since 2.0.1 |
||
| 591 | */ |
||
| 592 | public static function ajax_install() { |
||
| 596 | |||
| 597 | public static function install() { |
||
| 601 | |||
| 602 | public static function uninstall() { |
||
| 616 | |||
| 617 | public static function drop_tables( $tables ) { |
||
| 626 | |||
| 627 | public static function deauthorize() { |
||
| 637 | |||
| 638 | public static function set_footer_text( $text ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * @deprecated 1.07.05 |
||
| 648 | * @codeCoverageIgnore |
||
| 649 | */ |
||
| 650 | public static function get_form_shortcode( $atts ) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @deprecated 2.5.4 |
||
| 656 | * @codeCoverageIgnore |
||
| 657 | */ |
||
| 658 | public static function widget_text_filter( $content ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Deprecated in favor of wpmu_upgrade_site |
||
| 664 | * |
||
| 665 | * @deprecated 2.3 |
||
| 666 | * @codeCoverageIgnore |
||
| 667 | */ |
||
| 668 | public static function front_head() { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @deprecated 3.0.04 |
||
| 674 | * @codeCoverageIgnore |
||
| 675 | */ |
||
| 676 | public static function activation_install() { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @deprecated 3.0 |
||
| 682 | * @codeCoverageIgnore |
||
| 683 | */ |
||
| 684 | public static function page_route( $content ) { |
||
| 687 | } |
||
| 688 |
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.