Passed
Pull Request — master (#284)
by Brian
06:11
created

WPInv_Meta_Box_Details   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 178
dl 0
loc 245
rs 3.44
c 2
b 0
f 1
wmc 62

7 Methods

Rating   Name   Duplication   Size   Complexity  
C subscriptions() 0 52 12
A renewals() 0 10 3
B resend_invoice() 0 31 7
F output() 0 93 24
C payment_meta() 0 32 14
A payment_form() 0 2 1
A payment_form_items() 0 2 1

How to fix   Complexity   

Complex Class

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.

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
2
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
5
}
6
7
class WPInv_Meta_Box_Details {
8
    public static function output( $post ) {
9
        $currency_symbol    = wpinv_currency_symbol();
0 ignored issues
show
Unused Code introduced by
The assignment to $currency_symbol is dead and can be removed.
Loading history...
10
        $statuses           = wpinv_get_invoice_statuses( true );
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 ) : '';
0 ignored issues
show
Bug introduced by
Are you sure get_option('date_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $date_created       = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $datetime_created ) : '';
Loading history...
Bug introduced by
Are you sure get_option('time_format') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $date_created       = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( get_option( 'date_format' ) . ' ' . /** @scrutinizer ignore-type */ get_option( 'time_format' ), $datetime_created ) : '';
Loading history...
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' );
0 ignored issues
show
Comprehensibility Best Practice introduced by
$title was never initialized. Although not strictly required by PHP, it is generally a good practice to add $title = array(); before regardless.
Loading history...
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&#8217;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', '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
                <p class="wpi-meta-row wpi-meta-desc"><?php _e( 'Leave blank to disable sending auto reminder for this invoice.', 'invoicing' );?></p>
46
            </div>
47
        </div>
48
        <?php } ?>
49
        <?php do_action( 'wpinv_meta_box_details_after_due_date', $post_id ); ?>
50
        <?php if ( $date_completed && $date_completed != 'n/a' ) { ?>
51
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-date-completed">
52
            <div class="gdmbx-th"><label><?php _e( 'Payment Date:', 'invoicing' );?></label></div>
53
            <div class="gdmbx-td"><?php echo $date_completed;?></div>
54
        </div>
55
        <?php } ?>
56
        <?php $is_viewed = wpinv_is_invoice_viewed( $post_id ); ?>
57
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-customer-viewed">
58
            <div class="gdmbx-th"><label><?php _e( 'Viewed by Customer:', 'invoicing' );?></label></div>
59
            <div class="gdmbx-td"><?php ( 1 == $is_viewed ) ? _e( 'Yes', 'invoicing' ) : _e( 'No', 'invoicing' ); ?></div>
60
        </div>
61
        <div class="gdmbx-row gdmbx-type-select gdmbx2-id-wpinv-status">
62
            <div class="gdmbx-th"><label for="wpinv_status"><?php echo $title['status']; ?></label></div>
63
            <div class="gdmbx-td">
64
                <select required="required" id="wpinv_status" name="wpinv_status" class="gdmbx2_select wpi_select2">
65
                    <?php foreach ( $statuses as $value => $label ) { ?>
66
                    <option value="<?php echo $value;?>" <?php selected( $status, $value );?>><?php echo $label;?></option>
67
                    <?php } ?>
68
                </select>
69
            </div>
70
        </div>
71
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-number table-layout">
72
            <div class="gdmbx-th"><label for="wpinv_number"><?php echo $title['number']; ?></label></div>
73
            <div class="gdmbx-td">
74
                <input type="text" value="<?php echo esc_attr( $invoice_number );?>" id="wpinv_number" name="wpinv_number" class="regular-text" readonly>
75
            </div>
76
        </div>
77
        <?php do_action( 'wpinv_meta_box_details_inner', $post_id );
78
        $disable_discount = apply_filters('wpinv_disable_apply_discount', false, $invoice, $post_id);
79
        ?>
80
        <?php if ( !( $is_paid = ( $invoice->is_paid() || $invoice->is_refunded() ) ) && !$disable_discount || $discount_code ) { ?>
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (! $is_paid = $invoice->...ount) || $discount_code, Probably Intended Meaning: ! $is_paid = $invoice->i...ount || $discount_code)
Loading history...
81
        <div class="gdmbx-row gdmbx-type-text gdmbx2-id-wpinv-discount-code table-layout">
82
            <div class="gdmbx-th"><label for="wpinv_discount_code"><?php _e( 'Discount Code:', 'invoicing' );?></label></div>
83
            <div class="gdmbx-td">
84
                <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 && !$disable_discount ) { ?><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 } ?>
0 ignored issues
show
Bug introduced by
Are you sure the usage of esc_attr_e('Remove', 'invoicing') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the usage of esc_attr_e('Apply', 'invoicing') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
            </div>
86
        </div>
87
        <?php } ?>
88
    </div>
89
</div>
90
<div class="gdmbx-row gdmbx-type-text gdmbx-wpinv-save-send table-layout">
91
    <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>
92
        <select id="wpi_save_send" name="wpi_save_send" class="wpi_select2">
93
            <option value="1"><?php _e( 'Yes', 'invoicing' ); ?></option>
94
            <option value="" selected="selected"><?php _e( 'No', 'invoicing' ); ?></option>
95
        </select>
96
    </p>
97
    <p class="wpi-meta-row wpi-send-info"><?php echo $mail_notice; ?></p>
98
</div>
99
<?php wp_nonce_field( 'wpinv_details', 'wpinv_details_nonce' ) ;?>
100
        <?php
101
    }
102
    
103
    public static function resend_invoice( $post ) {
104
        global $wpi_mb_invoice;
105
        
106
        if ( empty( $wpi_mb_invoice ) ) {
107
            return;
108
        }
109
        
110
        $text = array(
111
            'message'       => esc_attr__( 'This will send a copy of the invoice to the customer&#8217;s email address.', 'invoicing' ),
112
            'button_text'   =>  __( 'Resend Invoice', 'invoicing' ),
113
        );
114
            
115
        $text = apply_filters('wpinv_resend_invoice_metabox_text', $text);
116
        do_action( 'wpinv_metabox_resend_invoice_before', $wpi_mb_invoice );
117
        
118
        if ( $email = $wpi_mb_invoice->get_email() ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $email is dead and can be removed.
Loading history...
119
            $email_actions = array();
120
            $email_actions['email_url']      = remove_query_arg( 'wpinv-message', add_query_arg( array( 'wpi_action' => 'send_invoice', 'invoice_id' => $post->ID ) ) );
121
            $email_actions['reminder_url']   = add_query_arg( array( 'wpi_action' => 'send_reminder', 'invoice_id' => $post->ID ) );
122
            
123
            $email_actions = apply_filters('wpinv_resend_invoice_email_actions', $email_actions );
124
        ?>
125
        <p class="wpi-meta-row wpi-resend-info"><?php echo $text['message']; ?></p>
126
        <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>
127
        <?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() ) ) { ?>
0 ignored issues
show
Unused Code introduced by
The assignment to $due_date is dead and can be removed.
Loading history...
128
        <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>
129
        <?php } ?>
130
        <?php
131
        }
132
        
133
        do_action( 'wpinv_metabox_resend_invoice_after', $wpi_mb_invoice );
134
    }
135
    
136
    public static function subscriptions( $post ) {
137
        $invoice = wpinv_get_invoice( $post->ID );
138
139
        if ( ! empty( $invoice ) && $invoice->is_recurring() && $invoice->is_parent() ) {
140
            $subscription = wpinv_get_subscription( $invoice );
141
142
            if ( empty( $subscription ) ) {
143
                ?>
144
                <p class="wpi-meta-row"><?php echo wp_sprintf( __( 'New Subscription will be created when customer will checkout and pay the invoice. Go to: %sSubscriptions%s', 'invoicing' ), '<a href="' . admin_url( 'admin.php?page=wpinv-subscriptions' ).'">', '</a>' ); ?></p>
145
                <?php
146
                return;
147
            }
148
            $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->period, $subscription->frequency );
149
            $billing = wpinv_price(wpinv_format_amount( $subscription->recurring_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) ) . ' / ' . $frequency;
150
            $initial = wpinv_price(wpinv_format_amount( $subscription->initial_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) );
151
            $payments = $subscription->get_child_payments();
152
            ?>
153
            <p class="wpi-meta-row wpi-sub-label <?php echo 'status-' . $subscription->status; ?>"><?php _e('Recurring Payment', 'invoicing'); ?></p>
154
            <?php if ( ! empty( $subscription ) && ! empty( $subscription->id ) ) { ?>
155
                <p class="wpi-meta-row wpi-sub-id">
156
                    <label><?php _e( 'Subscription ID:', 'invoicing' ); ?> </label><a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $subscription->id ) ); ?>" title="<?php esc_attr_e( 'View or edit subscription', 'invoicing' ); ?>" target="_blank"><?php echo $subscription->id; ?></a></p>
157
            <?php } ?>
158
            <p class="wpi-meta-row wpi-bill-cycle">
159
                <label><?php _e( 'Billing Cycle:', 'invoicing'); ?> </label><?php printf( _x( '%s then %s', 'Initial subscription amount then billing cycle and amount', 'invoicing' ), $initial, $billing ); ?>
160
            </p>
161
            <p class="wpi-meta-row wpi-billed-times">
162
                <label><?php _e( 'Times Billed:', 'invoicing' ); ?> </label><?php echo $subscription->get_times_billed() . ' / ' . ( ( $subscription->bill_times == 0 ) ? 'Until Cancelled' : $subscription->bill_times ); ?>
163
            </p>
164
            <p class="wpi-meta-row wpi-start-date">
165
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date_i18n() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
                <label><?php _e( 'Start Date:', 'invoicing' ); ?> </label><?php echo date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $subscription->created, current_time( 'timestamp' ) ) ); ?>
Loading history...
166
            </p>
167
            <p class="wpi-meta-row wpi-end-date">
168
                <label><?php echo ( 'trialling' == $subscription->status ? __( 'Trialling Until:', 'invoicing' ) : __( 'Expiration Date:', 'invoicing' ) ); ?> </label><?php echo date_i18n( get_option( 'date_format' ), strtotime( $subscription->expiration, current_time( 'timestamp' ) ) ); ?>
169
            </p>
170
            <?php if ( $subscription->status ) { ?>
171
                <p class="wpi-meta-row wpi-sub-status">
172
                    <label><?php _e( 'Subscription Status:', 'invoicing'); ?> </label><?php echo $subscription->get_status_label(); ?>
173
                </p>
174
            <?php } ?>
175
            <?php if ( !empty( $payments ) ) { ?>
176
                <p><strong><?php _e( 'Renewal Payments:', 'invoicing' ); ?></strong></p>
177
                <ul id="wpi-sub-payments">
178
                <?php foreach ( $payments as $payment ) {
179
                    $invoice_id = $payment->ID;
180
                    ?>
181
                    <li>
182
                        <a href="<?php echo esc_url( get_edit_post_link( $invoice_id ) ); ?>"><?php echo wpinv_get_invoice_number( $invoice_id ); ?></a>&nbsp;&ndash;&nbsp;
183
                        <span><?php echo wpinv_get_invoice_date( $invoice_id ); ?>&nbsp;&ndash;&nbsp;</span>
184
                        <span><?php echo wpinv_payment_total( $invoice_id, true ); ?></span>
185
                    </li>
186
                <?php } ?>
187
                </ul>
188
            <?php }
189
        }
190
    }
191
    
192
    public static function renewals( $post ) {
193
        $invoice = wpinv_get_invoice( $post->ID );
194
        
195
        if ( wpinv_is_subscription_payment( $invoice ) ) {
196
            $parent_url = get_edit_post_link( $invoice->parent_invoice );
197
            $parent_id  = wpinv_get_invoice_number( $invoice->parent_invoice );
198
            $subscription = wpinv_get_subscription( $invoice );
199
        ?>
200
        <?php if ( ! empty( $subscription ) ) { ?><p class="wpi-meta-row wpi-sub-id"><label><?php _e('Subscription ID:', 'invoicing'); ?> </label><a href="<?php echo esc_url( admin_url( 'admin.php?page=wpinv-subscriptions&id=' . $subscription->id ) ); ?>" title="<?php esc_attr_e( 'View or edit subscription', 'invoicing' ); ?>" target="_blank"><?php echo $subscription->id; ?></a></p><?php } ?>
201
        <p class="wpi-meta-row wpi-parent-id"><label><?php _e( 'Parent Invoice:', 'invoicing' );?> </label><a href="<?php echo esc_url( $parent_url ); ?>"><?php echo $parent_id; ?></a></p>
202
        <?php
203
        }
204
    }
205
206
    /**
207
     * Renders a metabox to edit a payment form.
208
     */
209
    public static function payment_form( $post ) {
210
        WPInv_Meta_Box_Form_Items::output_options( $post );
0 ignored issues
show
Bug introduced by
The type WPInv_Meta_Box_Form_Items was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
211
    }
212
213
    /**
214
     * Renders a metabox to select items.
215
     */
216
    public static function payment_form_items( $post ) {
217
        WPInv_Meta_Box_Form_Items::output( $post );
218
    }
219
    
220
    public static function payment_meta( $post ) {
221
        global $wpi_mb_invoice;
222
223
        $set_dateway = empty( $wpi_mb_invoice->gateway ) ? true : false;
224
        if ( !$set_dateway && !$wpi_mb_invoice->get_meta( '_wpinv_checkout', true ) && !$wpi_mb_invoice->is_paid() && !$wpi_mb_invoice->is_refunded() ) {
225
            $set_dateway = true;
226
        }
227
        
228
        ?>
229
        <p class="wpi-meta-row">
230
        <?php if ( $set_dateway ) { $gateways = wpinv_get_enabled_payment_gateways( true ); ?>
231
            <label for="wpinv_gateway"><?php _e( 'Gateway:', 'invoicing' ) ; ?></label>
232
            <select required="required" id="wpinv_gateway" class="wpi_select2" name="wpinv_gateway">
233
                <?php foreach ( $gateways as $name => $gateway ) {
234
                    if ( $wpi_mb_invoice->is_recurring() && !wpinv_gateway_support_subscription( $name ) ) {
235
                        continue;
236
                    }
237
                    ?>
238
                <option value="<?php echo $name;?>" <?php selected( $wpi_mb_invoice->gateway, $name );?>><?php echo !empty( $gateway['admin_label'] ) ? $gateway['admin_label'] : $gateway['checkout_label']; ?></option>
239
                <?php } ?>
240
            </select>
241
        <?php } else { 
242
            echo wp_sprintf( __( '<label>Gateway:</label> %s', 'invoicing' ), wpinv_get_gateway_admin_label( $wpi_mb_invoice->gateway ) );
243
        } ?>
244
        </p>
245
        <?php if ( $key = $wpi_mb_invoice->get_key() ) { ?>
246
        <p class="wpi-meta-row"><?php echo wp_sprintf( __( '<label>Key:</label> %s', 'invoicing' ), $key ); ?></p>
247
        <?php } ?>
248
        <?php if ( $wpi_mb_invoice->is_paid() || $wpi_mb_invoice->is_refunded() ) { ?>
249
        <p class="wpi-meta-row"><?php echo wp_sprintf( __( '<label>Transaction ID:</label> %s', 'invoicing' ), wpinv_payment_link_transaction_id( $wpi_mb_invoice ) ); ?></p>
250
        <?php } ?>
251
        <?php
252
    }
253
}
254