Complex classes like WPInv_Meta_Box_Details 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 WPInv_Meta_Box_Details, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class WPInv_Meta_Box_Details { |
||
| 8 | public static function output( $post ) { |
||
| 9 | $currency_symbol = wpinv_currency_symbol(); |
||
|
|
|||
| 10 | $statuses = wpinv_get_invoice_statuses(); |
||
| 11 | |||
| 12 | $post_id = !empty( $post->ID ) ? $post->ID : 0; |
||
| 13 | $invoice = new WPInv_Invoice( $post_id ); |
||
| 14 | |||
| 15 | $status = $invoice->get_status( false ); // Current status |
||
| 16 | $discount = $invoice->get_discount(); |
||
| 17 | $discount_code = $discount > 0 ? $invoice->get_discount_code() : ''; |
||
| 18 | $invoice_number = $invoice->get_number(); |
||
| 19 | |||
| 20 | $date_created = $invoice->get_created_date(); |
||
| 21 | $datetime_created = strtotime( $date_created ); |
||
| 22 | $date_created = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $datetime_created ) : ''; |
||
| 23 | $date_completed = $invoice->get_completed_date(); |
||
| 24 | $date_completed = $date_completed != '' && $date_completed != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $date_completed ) ) : 'n/a'; |
||
| 25 | $title['status'] = __( 'Invoice Status:', 'invoicing' ); |
||
| 26 | $title['number'] = __( 'Invoice Number:', 'invoicing' ); |
||
| 27 | $mail_notice = esc_attr__( 'After saving invoice, this will send a copy of the invoice to the user’s email address.', 'invoicing' ); |
||
| 28 | |||
| 29 | $title = apply_filters('wpinv_details_metabox_titles', $title, $invoice); |
||
| 30 | $statuses = apply_filters('wpinv_invoice_statuses', $statuses, $invoice); |
||
| 31 | $mail_notice = apply_filters('wpinv_metabox_mail_notice', $mail_notice, $invoice); |
||
| 32 | $post_obj = get_post_type_object($invoice->post_type); |
||
| 33 | ?> |
||
| 34 | <div class="gdmbx2-wrap form-table"> |
||
| 35 | <div class="gdmbx2-metabox gdmbx-field-list" id="gdmbx2-metabox-wpinv_details"> |
||
| 36 | <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-created"> |
||
| 37 | <div class="gdmbx-th"><label><?php _e( 'Date Created:', 'invoicing' );?></label></div> |
||
| 38 | <div class="gdmbx-td"><?php echo $date_created;?></div> |
||
| 39 | </div> |
||
| 40 | <?php if ( $invoice->post_type == 'wpi_invoice' && wpinv_get_option( 'overdue_active' ) && ( $invoice->needs_payment() || $invoice->has_status( array( 'auto-draft' ) ) ) ) { ?> |
||
| 41 | <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-overdue"> |
||
| 42 | <div class="gdmbx-th"><label for="wpinv_due_date"><?php _e( 'Due Date:', 'invoicing' );?></label></div> |
||
| 43 | <div class="gdmbx-td"> |
||
| 44 | <input type="text" placeholder="<?php esc_attr_e( 'Y-m-d', 'invoicing' );?>" value="<?php echo esc_attr( $invoice->get_due_date() );?>" id="wpinv_due_date" name="wpinv_due_date" class="regular-text wpiDatepicker" data-minDate="<?php echo esc_attr( date_i18n( 'Y-m-d', $datetime_created ) );?>" data-dateFormat="yy-mm-dd"> |
||
| 45 | </div> |
||
| 46 | </div> |
||
| 47 | <?php } ?> |
||
| 48 | <?php if ( $date_completed && $date_completed != 'n/a' ) { ?> |
||
| 49 | <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-completed"> |
||
| 50 | <div class="gdmbx-th"><label><?php _e( 'Payment Date:', 'invoicing' );?></label></div> |
||
| 51 | <div class="gdmbx-td"><?php echo $date_completed;?></div> |
||
| 52 | </div> |
||
| 53 | <?php } ?> |
||
| 54 | <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-status"> |
||
| 55 | <div class="gdmbx-th"><label for="wpinv_status"><?php echo $title['status']; ?></label></div> |
||
| 56 | <div class="gdmbx-td"> |
||
| 57 | <select required="required" id="wpinv_status" name="wpinv_status" class="gdmbx2_select"> |
||
| 58 | <?php foreach ( $statuses as $value => $label ) { ?> |
||
| 59 | <option value="<?php echo $value;?>" <?php selected( $status, $value );?>><?php echo $label;?></option> |
||
| 60 | <?php } ?> |
||
| 61 | </select> |
||
| 62 | </div> |
||
| 63 | </div> |
||
| 64 | <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-number table-layout"> |
||
| 65 | <div class="gdmbx-th"><label for="wpinv_number"><?php echo $title['number']; ?></label></div> |
||
| 66 | <div class="gdmbx-td"> |
||
| 67 | <input type="text" value="<?php echo esc_attr( $invoice_number );?>" id="wpinv_number" name="wpinv_number" class="regular-text" readonly> |
||
| 68 | </div> |
||
| 69 | </div> |
||
| 70 | <?php do_action( 'wpinv_meta_box_details_inner', $post_id ); ?> |
||
| 71 | <?php if ( !( $is_paid = ( $invoice->is_paid() || $invoice->is_refunded() ) ) || $discount_code ) { ?> |
||
| 72 | <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-discount-code table-layout"> |
||
| 73 | <div class="gdmbx-th"><label for="wpinv_discount_code"><?php _e( 'Discount Code:', 'invoicing' );?></label></div> |
||
| 74 | <div class="gdmbx-td"> |
||
| 75 | <input type="text" value="<?php echo esc_attr( $discount_code ); ?>" id="wpinv_discount" class="medium-text" <?php echo ( $discount_code ? 'readonly' : '' ); ?> /><?php if ( !$is_paid ) { ?><input value="<?php echo esc_attr_e( 'Apply', 'invoicing' ); ?>" class="button button-small button-primary <?php echo ( $discount_code ? 'wpi-hide' : 'wpi-inlineb' ); ?>" id="wpinv-apply-code" type="button" /><input value="<?php echo esc_attr_e( 'Remove', 'invoicing' ); ?>" class="button button-small button-primary <?php echo ( $discount_code ? 'wpi-inlineb' : 'wpi-hide' ); ?>" id="wpinv-remove-code" type="button" /><?php } ?> |
||
| 76 | </div> |
||
| 77 | </div> |
||
| 78 | <?php } ?> |
||
| 79 | </div> |
||
| 80 | </div> |
||
| 81 | <div class="gdmbx-row gdmbx-type-text gdmbx-wpinv-save-send table-layout"> |
||
| 82 | <p class="wpi-meta-row wpi-save-send"><label for="wpi_save_send"><?php echo sprintf(__( 'Send %s:', 'invoicing' ),$post_obj->labels->singular_name) ; ?></label> |
||
| 83 | <select id="wpi_save_send" name="wpi_save_send"> |
||
| 84 | <option value="1"><?php _e( 'Yes', 'invoicing' ); ?></option> |
||
| 85 | <option value="" selected="selected"><?php _e( 'No', 'invoicing' ); ?></option> |
||
| 86 | </select> |
||
| 87 | </p> |
||
| 88 | <p class="wpi-meta-row wpi-send-info"><?php echo $mail_notice; ?></p> |
||
| 89 | </div> |
||
| 90 | <?php wp_nonce_field( 'wpinv_details', 'wpinv_details_nonce' ) ;?> |
||
| 91 | <?php |
||
| 92 | } |
||
| 93 | |||
| 94 | public static function resend_invoice( $post ) { |
||
| 95 | global $wpi_mb_invoice; |
||
| 96 | |||
| 97 | if ( empty( $wpi_mb_invoice ) ) { |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | $text = array( |
||
| 102 | 'message' => esc_attr__( 'This will send a copy of the invoice to the customer’s email address.', 'invoicing' ), |
||
| 103 | 'button_text' => __( 'Resend Invoice', 'invoicing' ), |
||
| 104 | ); |
||
| 105 | |||
| 106 | $text = apply_filters('wpinv_resend_invoice_metabox_text', $text); |
||
| 107 | do_action( 'wpinv_metabox_resend_invoice_before', $wpi_mb_invoice ); |
||
| 108 | |||
| 109 | if ( $email = $wpi_mb_invoice->get_email() ) { |
||
| 110 | $email_actions = array(); |
||
| 111 | $email_actions['email_url'] = add_query_arg( array( 'wpi_action' => 'send_invoice', 'invoice_id' => $post->ID ) ); |
||
| 112 | $email_actions['reminder_url'] = add_query_arg( array( 'wpi_action' => 'send_reminder', 'invoice_id' => $post->ID ) ); |
||
| 113 | |||
| 114 | $email_actions = apply_filters('wpinv_resend_invoice_email_actions', $email_actions ); |
||
| 115 | ?> |
||
| 116 | <p class="wpi-meta-row wpi-resend-info"><?php echo $text['message']; ?></p> |
||
| 117 | <p class="wpi-meta-row wpi-resend-email"><a href="<?php echo esc_url( $email_actions['email_url'] ); ?>" class="button button-secondary"><?php echo $text['button_text']; ?></a></p> |
||
| 118 | <?php if ( wpinv_get_option( 'overdue_active' ) && "wpi_invoice" === $wpi_mb_invoice->post_type && $wpi_mb_invoice->needs_payment() && ( $due_date = $wpi_mb_invoice->get_due_date() ) ) { ?> |
||
| 119 | <p class="wpi-meta-row wpi-send-reminder"><a title="<?php esc_attr_e( 'Send overdue reminder notification to customer', 'invoicing' ); ?>" href="<?php echo esc_url( $email_actions['reminder_url'] ); ?>" class="button button-secondary"><?php esc_attr_e( 'Send Reminder', 'invoicing' ); ?></a></p> |
||
| 120 | <?php } ?> |
||
| 121 | <?php |
||
| 122 | } |
||
| 123 | |||
| 124 | do_action( 'wpinv_metabox_resend_invoice_after', $wpi_mb_invoice ); |
||
| 125 | } |
||
| 126 | |||
| 127 | public static function subscriptions( $post ) { |
||
| 174 | |||
| 175 | public static function renewals( $post ) { |
||
| 187 | |||
| 188 | public static function payment_meta( $post ) { |
||
| 221 | } |
||
| 222 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.