Passed
Push — master ( 169926...ba6cf3 )
by Stiofan
22s
created

get_customer_invoice_data()   D

Complexity

Conditions 17
Paths 95

Size

Total Lines 97
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 74
nc 95
nop 1
dl 0
loc 97
rs 4.8361
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Personal data exporters.
4
 */
5
6
defined( 'ABSPATH' ) || exit;
7
8
/**
9
 * WPInv_Privacy_Exporters Class.
10
 */
11
class WPInv_Privacy_Exporters {
12
    /**
13
     * Finds and exports customer data by email address.
14
     *
15
     * @since 1.0.13
16
     * @param string $email_address The user email address.
17
     * @param int    $page  Page.
18
     * @return array An array of invoice data in name value pairs
19
     */
20
    public static function customer_invoice_data_exporter( $email_address, $page ) {
21
        $done           = false;
0 ignored issues
show
Unused Code introduced by
$done is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
        $page           = (int) $page;
23
        $data_to_export = array();
24
25
        $user           = get_user_by( 'email', $email_address );
26
        if ( ! $user instanceof WP_User ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
27
            return array(
28
                'data' => $data_to_export,
29
                'done' => true,
30
            );
31
        }
32
33
        $args    = array(
34
            'limit'    => 30,
35
            'page'     => $page,
36
            'user'     => $user->ID,
37
        );
38
39
        $invoices = wpinv_get_invoices( $args );
40
41
        if ( 0 < count( $invoices ) ) {
42
            foreach ( $invoices as $invoice ) {
43
                $data_to_export[] = array(
44
                    'group_id'    => 'customer_invoices',
45
                    'group_label' => __( 'Invoicing Data', 'invoicing' ),
46
                    'item_id'     => "wpinv-{$invoice->ID}",
47
                    'data'        => self::get_customer_invoice_data( $invoice ),
48
                );
49
            }
50
            $done = 30 > count( $invoices );
51
        } else {
52
            $done = true;
53
        }
54
55
        return array(
56
            'data' => $data_to_export,
57
            'done' => $done,
58
        );
59
    }
60
61
    /**
62
     * Get invoice data (key/value pairs) for a user.
63
     *
64
     * @since 1.0.13
65
     * @param WPInv_Invoice $invoice invoice object.
66
     * @return array
67
     */
68
    public static function get_customer_invoice_data( $invoice ) {
69
        $personal_data = array();
70
71
        $props_to_export = array(
72
            'number'               => __( 'Invoice Number', 'invoicing' ),
73
            'created_date'         => __( 'Invoice Date', 'invoicing' ),
74
            'status'               => __( 'Invoice Status', 'invoicing' ),
75
            'total'                => __( 'Invoice Total', 'invoicing' ),
76
            'items'                => __( 'Invoice Items', 'invoicing' ),
77
            'first_name'           => __( 'First Name', 'invoicing' ),
78
            'last_name'            => __( 'Last Name', 'invoicing' ),
79
            'email'                => __( 'Email Address', 'invoicing' ),
80
            '_wpinv_company'       => __( 'Company', 'invoicing' ),
81
            'phone'                => __( 'Phone Number', 'invoicing' ),
82
            'address'              => __( 'Address', 'invoicing' ),
83
            '_wpinv_city'          => __( 'City', 'invoicing' ),
84
            '_wpinv_country'       => __( 'Country', 'invoicing' ),
85
            '_wpinv_state'         => __( 'State', 'invoicing' ),
86
            '_wpinv_zip'           => __( 'Zip Code', 'invoicing' ),
87
        );
88
89
        $subscription = wpinv_get_subscription( $invoice );
90
        $period = $initial_amt = $bill_times = $billed = $renewal_date = '';
0 ignored issues
show
Unused Code introduced by
$billed is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
92
        if ( $invoice->is_recurring() && !empty( $subscription ) ) {
93
            $frequency = WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->period,$subscription->frequency );
94
            $period = wpinv_price( wpinv_format_amount( $subscription->recurring_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) ) . ' / ' . $frequency;
95
            $initial_amt = wpinv_price( wpinv_format_amount( $subscription->initial_amount ), wpinv_get_invoice_currency_code( $subscription->parent_payment_id ) );
96
            $bill_times = $subscription->get_times_billed() . ' / ' . ( ( $subscription->bill_times == 0 ) ? 'Until Cancelled' : $subscription->bill_times );
97
            $renewal_date = ! empty( $subscription->expiration ) ? date_i18n( get_option( 'date_format' ), strtotime( $subscription->expiration ) ) : __( 'N/A', 'invoicing' );
98
99
            $props_to_export['period'] = __( 'Billing Cycle', 'invoicing' );
100
            $props_to_export['initial_amount'] = __( 'Initial Amount', 'invoicing' );
101
            $props_to_export['bill_times'] = __( 'Times Billed', 'invoicing' );
102
            $props_to_export['renewal_date'] = __( 'Renewal Date', 'invoicing' );
103
        }
104
105
        $props_to_export['ip'] = __( 'IP Address', 'invoicing' );
106
        $props_to_export['view_url'] = __( 'Invoice Link', 'invoicing' );
107
108
        $props_to_export = apply_filters( 'wpinv_privacy_export_invoice_personal_data_props', $props_to_export, $invoice, $subscription);
109
110
        foreach ( $props_to_export as $prop => $name ) {
111
            $value = '';
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112
113
            switch ( $prop ) {
114
                case 'items':
115
                    $item_names = array();
116
                    foreach ( $invoice->get_cart_details() as $key => $cart_item ) {
117
                        $item_quantity  = $cart_item['quantity'] > 0 ? absint( $cart_item['quantity'] ) : 1;
118
                        $item_names[] = $cart_item['name'] . ' x ' . $item_quantity;
119
                    }
120
                    $value = implode( ', ', $item_names );
121
                    break;
122
                case 'status':
123
                    $value = $invoice->get_status(true);
124
                    break;
125
                case 'total':
126
                    $value = $invoice->get_total(true);
127
                    break;
128
                case 'period':
129
                    $value = $period;
130
                    break;
131
                case 'initial_amount':
132
                    $value = $initial_amt;
133
                    break;
134
                case 'bill_times':
135
                    $value = $bill_times;
136
                    break;
137
                case 'renewal_date':
138
                    $value = $renewal_date;
139
                    break;
140
                default:
141
                    if ( is_callable( array( $invoice, 'get_' . $prop ) ) ) {
142
                        $value = $invoice->{"get_$prop"}();
143
                    } else {
144
                        $value = $invoice->get_meta($prop);
145
                    }
146
                    break;
147
            }
148
149
            $value = apply_filters( 'wpi_privacy_export_invoice_personal_data_prop', $value, $prop, $invoice );
150
151
            if ( $value ) {
152
                $personal_data[] = array(
153
                    'name'  => $name,
154
                    'value' => $value,
155
                );
156
            }
157
158
        }
159
160
        $personal_data = apply_filters( 'wpinv_privacy_export_invoice_personal_data', $personal_data, $invoice );
161
162
        return $personal_data;
163
164
    }
165
166
}
167