Total Complexity | 43 |
Total Lines | 650 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GetPaid_Meta_Box_Invoice_Items 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 GetPaid_Meta_Box_Invoice_Items, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class GetPaid_Meta_Box_Invoice_Items { |
||
18 | |||
19 | public static function get_columns( $invoice ) { |
||
47 | } |
||
48 | |||
49 | public static function output( $post, $invoice = false ) { |
||
50 | |||
51 | if ( apply_filters( 'getpaid_use_new_invoice_items_metabox', false ) ) { |
||
52 | return self::output2( $post ); |
||
|
|||
53 | } |
||
54 | |||
55 | $post_id = !empty( $post->ID ) ? $post->ID : 0; |
||
56 | $invoice = $invoice instanceof WPInv_Invoice ? $invoice : new WPInv_Invoice( $post_id ); |
||
57 | $use_taxes = $invoice->is_taxable() && wpinv_use_taxes(); |
||
58 | $item_types = apply_filters( 'wpinv_item_types_for_quick_add_item', wpinv_get_item_types(), $post ); |
||
59 | $columns = self::get_columns( $invoice ); |
||
60 | $cols = count( $columns ); |
||
61 | $class = ''; |
||
62 | |||
63 | unset( $item_types['adv'] ); |
||
64 | unset( $item_types['package'] ); |
||
65 | |||
66 | if ( $invoice->is_paid() ) { |
||
67 | $class .= ' wpinv-paid'; |
||
68 | } |
||
69 | |||
70 | if ( $invoice->is_refunded() ) { |
||
71 | $class .= ' wpinv-refunded'; |
||
72 | } |
||
73 | |||
74 | if ( $invoice->is_recurring() ) { |
||
75 | $class .= ' wpi-recurring'; |
||
76 | } |
||
77 | |||
78 | ?> |
||
79 | |||
80 | <div class="wpinv-items-wrap<?php echo $class; ?>" id="wpinv_items_wrap" data-status="<?php echo esc_attr( $invoice->get_status() ); ?>"> |
||
81 | <table id="wpinv_items" class="wpinv-items" cellspacing="0" cellpadding="0"> |
||
82 | |||
83 | <thead> |
||
84 | <tr> |
||
85 | <?php foreach ( $columns as $key => $label ) : ?> |
||
86 | <th class="<?php echo esc_attr( $key ); echo 'total' == $key || 'qty' == $key ? ' hide-if-amount' : '' ?>"><?php echo wp_kses_post( $label ); ?></th> |
||
87 | <?php endforeach; ?> |
||
88 | </tr> |
||
89 | </thead> |
||
90 | |||
91 | <tbody class="wpinv-line-items"> |
||
92 | <?php |
||
93 | foreach ( $invoice->get_items() as $int => $item ) { |
||
94 | self::output_row( $columns, $item, $invoice, $int % 2 == 0 ? 'even' : 'odd' ); |
||
95 | } |
||
96 | ?> |
||
97 | </tbody> |
||
98 | |||
99 | <tfoot class="wpinv-totals"> |
||
100 | <tr> |
||
101 | <td colspan="<?php echo $cols; ?>" style="padding:0;border:0"> |
||
102 | <div id="wpinv-quick-add"> |
||
103 | <table cellspacing="0" cellpadding="0"> |
||
104 | <tr> |
||
105 | <td class="id"> </td> |
||
106 | <td class="title"> |
||
107 | |||
108 | <div class="wp-clearfix"> |
||
109 | <label class="wpi-item-name"> |
||
110 | <span class="input-text-wrap"> |
||
111 | <input type="text" style="width: 100%" placeholder="<?php esc_attr_e( 'Item Name', 'invoicing' );?>" class="wpinv-quick-item-name" name="_wpinv_quick[name]"> |
||
112 | </span> |
||
113 | </label> |
||
114 | </div> |
||
115 | |||
116 | <div class="wp-clearfix"> |
||
117 | <label class="wpi-item-price"> |
||
118 | <span class="input-text-wrap"> |
||
119 | <input type="text" style="width: 200px" placeholder="<?php esc_attr_e( 'Item Price', 'invoicing' );?>" class="wpinv-quick-item-price" name="_wpinv_quick[price]"> |
||
120 | × <input type="text" style="width: 140px" placeholder="<?php esc_attr_e( 'Item Quantity', 'invoicing' );?>" class="wpinv-quick-item-qty" name="_wpinv_quick[qty]"> |
||
121 | </span> |
||
122 | </label> |
||
123 | </div> |
||
124 | |||
125 | <div class="wp-clearfix"> |
||
126 | <label class="wpi-item-name"> |
||
127 | <span class="input-text-wrap"> |
||
128 | <textarea rows="4" style="width: 100%" placeholder="<?php esc_attr_e( 'Item Description', 'invoicing' );?>" class="wpinv-quick-item-description" name="_wpinv_quick[description]"></textarea> |
||
129 | </span> |
||
130 | </label> |
||
131 | </div> |
||
132 | |||
133 | <div class="wp-clearfix"> |
||
134 | <label class="wpi-item-type"> |
||
135 | <span class="input-text-wrap"> |
||
136 | <?php echo wpinv_html_select( array( |
||
137 | 'options' => $item_types, |
||
138 | 'name' => '_wpinv_quick[type]', |
||
139 | 'id' => '_wpinv_quick_type', |
||
140 | 'selected' => 'custom', |
||
141 | 'show_option_all' => false, |
||
142 | 'show_option_none' => false, |
||
143 | 'class' => 'gdmbx2-text-medium wpinv-quick-type', |
||
144 | ) ); ?> |
||
145 | </span> |
||
146 | </label> |
||
147 | </div> |
||
148 | |||
149 | <?php if ( $use_taxes ) : ?> |
||
150 | <div class="wp-clearfix"> |
||
151 | <label class="wpi-vat-rule"> |
||
152 | <span class="input-text-wrap"> |
||
153 | <?php |
||
154 | echo wpinv_html_select( array( |
||
155 | 'options' => array_merge( |
||
156 | array( '' => __( 'Select VAT Rule', 'invoicing' ) ), |
||
157 | getpaid_get_tax_rules() |
||
158 | ), |
||
159 | 'name' => '_wpinv_quick[vat_rule]', |
||
160 | 'id' => '_wpinv_quick_vat_rule', |
||
161 | 'show_option_all' => false, |
||
162 | 'show_option_none' => false, |
||
163 | 'class' => 'gdmbx2-text-medium wpinv-quick-vat-rule', |
||
164 | ) ); |
||
165 | ?> |
||
166 | </span> |
||
167 | </label> |
||
168 | </div> |
||
169 | <div class="wp-clearfix"> |
||
170 | <label class="wpi-vat-class"> |
||
171 | <span class="input-text-wrap"> |
||
172 | <?php |
||
173 | echo wpinv_html_select( array( |
||
174 | 'options' => array_merge( |
||
175 | array( '' => __( 'Select VAT Class', 'invoicing' ) ), |
||
176 | getpaid_get_tax_classes() |
||
177 | ), |
||
178 | 'name' => '_wpinv_quick[vat_class]', |
||
179 | 'id' => '_wpinv_quick_vat_class', |
||
180 | 'show_option_all' => false, |
||
181 | 'show_option_none' => false, |
||
182 | 'class' => 'gdmbx2-text-medium wpinv-quick-vat-class', |
||
183 | ) ); |
||
184 | ?> |
||
185 | </span> |
||
186 | </label> |
||
187 | </div> |
||
188 | <?php endif; ?> |
||
189 | |||
190 | <div class="wp-clearfix"> |
||
191 | <label class="wpi-item-actions"> |
||
192 | <span class="input-text-wrap"> |
||
193 | <input type="button" value="Save" class="button button-primary" id="wpinv-save-item"><input type="button" value="Cancel" class="button button-secondary" id="wpinv-cancel-item"> |
||
194 | </span> |
||
195 | </label> |
||
196 | </div> |
||
197 | </td> |
||
198 | </tr> |
||
199 | </table> |
||
200 | </div> |
||
201 | </td> |
||
202 | </tr> |
||
203 | <tr class="totals"> |
||
204 | <td colspan="<?php echo ( $cols - 4 ); ?>"></td> |
||
205 | <td colspan="4"> |
||
206 | <table cellspacing="0" cellpadding="0"> |
||
207 | <tr class="subtotal"> |
||
208 | <td class="name"><?php _e( 'Sub Total:', 'invoicing' );?></td> |
||
209 | <td class="total"><?php echo wpinv_price( $invoice->get_subtotal(), $invoice->get_currency() );?></td> |
||
210 | <td class="action"></td> |
||
211 | </tr> |
||
212 | <tr class="discount"> |
||
213 | <td class="name"><?php _e( 'Discount:', 'invoicing' ) ; ?></td> |
||
214 | <td class="total"><?php echo wpinv_price( $invoice->get_total_discount(), $invoice->get_currency() );?></td> |
||
215 | <td class="action"></td> |
||
216 | </tr> |
||
217 | <?php if ( $use_taxes ) : ?> |
||
218 | <tr class="tax"> |
||
219 | <td class="name"><?php _e( 'Tax:', 'invoicing' );?></td> |
||
220 | <td class="total"><?php echo wpinv_price( $invoice->get_total_tax(), $invoice->get_currency() );?></td> |
||
221 | <td class="action"></td> |
||
222 | </tr> |
||
223 | <?php endif; ?> |
||
224 | <tr class="total"> |
||
225 | <td class="name"><?php _e( 'Total:', 'invoicing' );?></td> |
||
226 | <td class="total"><?php echo wpinv_price( $invoice->get_total(), $invoice->get_currency() );?></td> |
||
227 | <td class="action"></td> |
||
228 | </tr> |
||
229 | </table> |
||
230 | </td> |
||
231 | </tr> |
||
232 | </tfoot> |
||
233 | |||
234 | </table> |
||
235 | <div class="wpinv-actions"> |
||
236 | <?php |
||
237 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
238 | echo wpinv_item_dropdown( |
||
239 | array( |
||
240 | 'name' => 'wpinv_invoice_item', |
||
241 | 'id' => 'wpinv_invoice_item', |
||
242 | 'show_recurring' => true, |
||
243 | 'class' => 'wpi_select2', |
||
244 | ) |
||
245 | ); |
||
246 | |||
247 | echo " " . '<button class="button button-primary" id="wpinv-add-item">' . sprintf( esc_html__( 'Add item to %s', 'invoicing' ), $invoice->get_label() ) . '</button>'; |
||
248 | echo " " . '<button class="button button-primary" id="wpinv-new-item">' . esc_html__( 'Create new item', 'invoicing' ) . '</button>'; |
||
249 | echo " " . '<button class="button button-primary wpinv-flr" id="wpinv-recalc-totals">' . esc_html__( 'Recalculate Totals', 'invoicing' ) . '</button>'; |
||
250 | |||
251 | } |
||
252 | ?> |
||
253 | <?php do_action( 'wpinv_invoice_items_actions', $invoice ); ?> |
||
254 | </div> |
||
255 | </div> |
||
256 | <?php |
||
257 | } |
||
258 | |||
259 | public static function output_row( $columns, $item, $invoice, $class='even' ) { |
||
260 | |||
261 | ?> |
||
262 | <tr class="item item-<?php echo esc_attr( $class ); ?>" data-item-id="<?php echo esc_attr( $item->get_id() ); ?>"> |
||
263 | <?php foreach ( array_keys( $columns ) as $column ) : ?> |
||
264 | <td class="<?php echo esc_attr( $column ); echo 'total' == $column || 'qty' == $column ? ' hide-if-amount' : '' ?>"> |
||
265 | <?php |
||
266 | switch ( $column ) { |
||
267 | case 'id': |
||
268 | echo (int) $item->get_id(); |
||
269 | break; |
||
270 | case 'title': |
||
271 | printf( |
||
272 | '<a href="%s" target="_blank">%s</a>', |
||
273 | get_edit_post_link( $item->get_id() ), |
||
274 | esc_html( $item->get_raw_name() ) |
||
275 | ); |
||
276 | |||
277 | $summary = apply_filters( 'getpaid_admin_invoice_line_item_summary', $item->get_description(), $item, $invoice ); |
||
278 | if ( $summary !== '' ) { |
||
279 | printf( |
||
280 | '<span class="meta">%s</span>', |
||
281 | wpautop( wp_kses_post( $summary ) ) |
||
282 | ); |
||
283 | } |
||
284 | |||
285 | printf( |
||
286 | '<input type="hidden" value="%s" name="getpaid_items[%s][name]" class="getpaid-recalculate-prices-on-change" />', |
||
287 | esc_attr( $item->get_raw_name() ), |
||
288 | (int) $item->get_id() |
||
289 | ); |
||
290 | |||
291 | printf( |
||
292 | '<textarea style="display: none;" name="getpaid_items[%s][description]" class="getpaid-recalculate-prices-on-change">%s</textarea>', |
||
293 | (int) $item->get_id(), |
||
294 | esc_attr( $item->get_description() ) |
||
295 | ); |
||
296 | |||
297 | break; |
||
298 | case 'price': |
||
299 | printf( |
||
300 | '<input type="text" value="%s" name="getpaid_items[%s][price]" style="width: 100px;" class="getpaid-admin-invoice-item-price getpaid-recalculate-prices-on-change" />', |
||
301 | esc_attr( getpaid_unstandardize_amount( $item->get_price() ) ), |
||
302 | (int) $item->get_id() |
||
303 | ); |
||
304 | |||
305 | break; |
||
306 | case 'qty': |
||
307 | printf( |
||
308 | '<input type="text" style="width: 100px;" value="%s" name="getpaid_items[%s][quantity]" class="getpaid-admin-invoice-item-quantity getpaid-recalculate-prices-on-change" />', |
||
309 | floatval( $item->get_quantity() ), |
||
310 | (int) $item->get_id() |
||
311 | ); |
||
312 | |||
313 | break; |
||
314 | case 'total': |
||
315 | echo wpinv_price( $item->get_sub_total(), $invoice->get_currency() ); |
||
316 | |||
317 | break; |
||
318 | case 'tax': |
||
319 | echo wpinv_round_amount( getpaid_get_invoice_tax_rate( $invoice, $item ), 2 ) . '%'; |
||
320 | |||
321 | break; |
||
322 | case 'action': |
||
323 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
324 | echo '<i class="fa fa-trash wpinv-item-remove"></i>'; |
||
325 | } |
||
326 | break; |
||
327 | } |
||
328 | do_action( 'getpaid_admin_edit_invoice_item_' . $column, $item, $invoice ); |
||
329 | ?> |
||
330 | </td> |
||
331 | <?php endforeach; ?> |
||
332 | </tr> |
||
333 | <?php |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Output the metabox. |
||
338 | * |
||
339 | * @param WP_Post $post |
||
340 | */ |
||
341 | public static function output2( $post ) { |
||
667 | </div> |
||
668 | </div> |
||
669 | </div> |
||
676 |
This check looks for function or method calls that always return null and whose return value is used.
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.