Conditions | 21 |
Paths | 4660 |
Total Lines | 111 |
Code Lines | 45 |
Lines | 6 |
Ratio | 5.41 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
59 | 'company', |
||
60 | 'vat_number', |
||
61 | 'email', |
||
62 | 'phone', |
||
63 | 'address', |
||
64 | 'city', |
||
65 | 'state', |
||
66 | 'country', |
||
67 | 'zip', |
||
68 | ); |
||
69 | |||
70 | $billing_details = array(); |
||
71 | $user_id = $invoice->get_user_id(); |
||
72 | |||
73 | foreach ( $address_fields as $field ) { |
||
74 | if ( isset( $data['billing_details'][ $field ] ) ) { |
||
75 | $value = sanitize_text_field( $data['billing_details'][ $field ] ); |
||
76 | |||
77 | View Code Duplication | if ( $field == 'country' && empty( $value ) ) { |
|
78 | if ( !empty( $invoice->country ) ) { |
||
79 | $value = $invoice->country; |
||
80 | } else { |
||
81 | $value = wpinv_default_billing_country( '', $user_id ); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | View Code Duplication | if ( $field == 'state' && empty( $value ) ) { |
|
86 | if ( !empty( $invoice->state ) ) { |
||
87 | $value = $invoice->state; |
||
88 | } else { |
||
89 | $value = wpinv_get_default_state(); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $invoice->set( $field, $value ); |
||
94 | |||
95 | update_post_meta( $invoice->ID, '_wpinv_' . $field, $value ); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $invoice; |
||
100 | } |
||
101 | |||
102 | protected function set_discount( $invoice, $data ) { |
||
103 | if ( isset( $data['discount'] ) ) { |
||
104 | $invoice->set( 'discount', wpinv_round_amount( $data['discount'] ) ); |
||
105 | |||
106 | update_post_meta( $invoice->ID, '_wpinv_discount', wpinv_round_amount( $data['discount'] ) ); |
||
107 | |||
108 | if ( isset( $data['discount_code'] ) ) { |
||
109 | $invoice->set( 'discount_code', $data['discount_code'] ); |
||
110 | |||
111 | update_post_meta( $invoice->ID, '_wpinv_discount_code', $data['discount_code'] ); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $invoice; |
||
116 | } |
||
117 | |||
118 | protected function set_items( $invoice, $data ) { |
||
119 | if ( !empty( $data['items'] ) && is_array( $data['items'] ) ) { |
||
120 | $items_array = array(); |
||
121 | |||
122 | if ( !empty( $invoice->country ) ) { |
||
123 | $country = $invoice->country; |
||
124 | } else if ( !empty( $data['billing_details']['country'] ) ) { |
||
125 | $country = $data['billing_details']['country']; |
||
126 | } else { |
||
127 | $country = wpinv_default_billing_country( '', $invoice->get_user_id() ); |
||
128 | } |
||
129 | |||
130 | if ( !empty( $invoice->state ) ) { |
||
131 | $state = $invoice->state; |
||
132 | } else if ( !empty( $data['billing_details']['state'] ) ) { |
||
133 | $state = $data['billing_details']['state']; |
||
134 | } else { |
||
135 | $state = wpinv_get_default_state(); |
||
136 | } |
||
137 | |||
138 | $_POST['country'] = $country; |
||
139 | $_POST['state'] = $state; |
||
140 | |||
141 | $rate = wpinv_get_tax_rate( $country, $state, 'global' ); |
||
142 | |||
143 | $total_tax = 0; |
||
144 | foreach ( $data['items'] as $item ) { |
||
145 | $id = isset( $item['id'] ) ? sanitize_text_field( $item['id'] ) : ''; |
||
146 | $title = isset( $item['title'] ) ? sanitize_text_field( $item['title'] ) : ''; |
||
147 | $desc = isset( $item['description'] ) ? sanitize_text_field( $item['description'] ) : ''; |
||
148 | $amount = isset( $item['amount'] ) ? wpinv_round_amount( $item['amount'] ) : 0; |
||
149 | |||
150 | if ( !empty( $item['vat_rates_class'] ) ) { |
||
151 | $vat_rates_class = $item['vat_rates_class']; |
||
152 | } else { |
||
153 | $vat_rates_class = '_standard'; |
||
154 | } |
||
155 | $vat_rate = wpinv_get_tax_rate( $country, $state, $id ); |
||
156 | |||
157 | $tax = $amount > 0 ? ( $amount * 0.01 * (float)$vat_rate ) : 0; |
||
158 | $total_tax += $tax; |
||
159 | |||
160 | $items_array[] = array( |
||
161 | 'id' => $id, |
||
162 | 'title' => esc_html( $title ), |
||
163 | 'description' => esc_html( $desc ), |
||
164 | 'amount' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
||
165 | 'subtotal' => $amount > 0 ? wpinv_round_amount( $amount ) : 0, |
||
166 | 'vat_rates_class' => $vat_rates_class, |
||
167 | 'vat_rate' => $vat_rate, |
||
168 | 'tax' => $tax > 0 ? wpinv_round_amount( $tax ) : 0, |
||
169 | ); |
||
170 | } |
||
206 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.