Conditions | 33 |
Paths | > 20000 |
Total Lines | 199 |
Code Lines | 110 |
Lines | 0 |
Ratio | 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 |
||
181 | function wc_save_order_items( $order_id, $items ) { |
||
182 | global $wpdb; |
||
183 | |||
184 | // Order items + fees |
||
185 | $subtotal = 0; |
||
186 | $total = 0; |
||
187 | $subtotal_tax = 0; |
||
188 | $total_tax = 0; |
||
189 | $taxes = array( 'items' => array(), 'shipping' => array() ); |
||
190 | |||
191 | if ( isset( $items['order_item_id'] ) ) { |
||
192 | $line_total = $line_subtotal = $line_tax = $line_subtotal_tax = array(); |
||
193 | |||
194 | foreach ( $items['order_item_id'] as $item_id ) { |
||
195 | |||
196 | $item_id = absint( $item_id ); |
||
197 | |||
198 | if ( isset( $items['order_item_name'][ $item_id ] ) ) { |
||
199 | $wpdb->update( |
||
200 | $wpdb->prefix . 'woocommerce_order_items', |
||
201 | array( 'order_item_name' => wc_clean( wp_unslash( $items['order_item_name'][ $item_id ] ) ) ), |
||
202 | array( 'order_item_id' => $item_id ), |
||
203 | array( '%s' ), |
||
204 | array( '%d' ) |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | if ( isset( $items['order_item_qty'][ $item_id ] ) ) { |
||
209 | wc_update_order_item_meta( $item_id, '_qty', wc_stock_amount( $items['order_item_qty'][ $item_id ] ) ); |
||
210 | } |
||
211 | |||
212 | if ( isset( $items['order_item_tax_class'][ $item_id ] ) ) { |
||
213 | wc_update_order_item_meta( $item_id, '_tax_class', wc_clean( $items['order_item_tax_class'][ $item_id ] ) ); |
||
214 | } |
||
215 | |||
216 | // Get values. Subtotals might not exist, in which case copy value from total field |
||
217 | $line_total[ $item_id ] = isset( $items['line_total'][ $item_id ] ) ? $items['line_total'][ $item_id ] : 0; |
||
218 | $line_subtotal[ $item_id ] = isset( $items['line_subtotal'][ $item_id ] ) ? $items['line_subtotal'][ $item_id ] : $line_total[ $item_id ]; |
||
219 | $line_tax[ $item_id ] = isset( $items['line_tax'][ $item_id ] ) ? $items['line_tax'][ $item_id ] : array(); |
||
220 | $line_subtotal_tax[ $item_id ] = isset( $items['line_subtotal_tax'][ $item_id ] ) ? $items['line_subtotal_tax'][ $item_id ] : $line_tax[ $item_id ]; |
||
221 | |||
222 | // Format taxes |
||
223 | $line_taxes = array_map( 'wc_format_decimal', $line_tax[ $item_id ] ); |
||
224 | $line_subtotal_taxes = array_map( 'wc_format_decimal', $line_subtotal_tax[ $item_id ] ); |
||
225 | |||
226 | // Update values |
||
227 | wc_update_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $line_subtotal[ $item_id ] ) ); |
||
228 | wc_update_order_item_meta( $item_id, '_line_total', wc_format_decimal( $line_total[ $item_id ] ) ); |
||
229 | wc_update_order_item_meta( $item_id, '_line_subtotal_tax', array_sum( $line_subtotal_taxes ) ); |
||
230 | wc_update_order_item_meta( $item_id, '_line_tax', array_sum( $line_taxes ) ); |
||
231 | |||
232 | // Save line tax data - Since 2.2 |
||
233 | wc_update_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $line_taxes, 'subtotal' => $line_subtotal_taxes ) ); |
||
234 | $taxes['items'][] = $line_taxes; |
||
235 | |||
236 | // Total up |
||
237 | $subtotal += wc_format_decimal( $line_subtotal[ $item_id ] ); |
||
238 | $total += wc_format_decimal( $line_total[ $item_id ] ); |
||
239 | $subtotal_tax += array_sum( $line_subtotal_taxes ); |
||
240 | $total_tax += array_sum( $line_taxes ); |
||
241 | |||
242 | // Clear meta cache |
||
243 | wp_cache_delete( $item_id, 'order_item_meta' ); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | // Save meta |
||
248 | $meta_keys = isset( $items['meta_key'] ) ? $items['meta_key'] : array(); |
||
249 | $meta_values = isset( $items['meta_value'] ) ? $items['meta_value'] : array(); |
||
250 | |||
251 | foreach ( $meta_keys as $id => $meta_key ) { |
||
252 | $meta_value = ( empty( $meta_values[ $id ] ) && ! is_numeric( $meta_values[ $id ] ) ) ? '' : $meta_values[ $id ]; |
||
253 | |||
254 | // Delele blank item meta entries |
||
255 | if ( $meta_key === '' && $meta_value === '' ) { |
||
256 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_id = %d", $id ) ); |
||
257 | } else { |
||
258 | |||
259 | $wpdb->update( |
||
260 | $wpdb->prefix . 'woocommerce_order_itemmeta', |
||
261 | array( |
||
262 | 'meta_key' => wp_unslash( $meta_key ), |
||
263 | 'meta_value' => wp_unslash( $meta_value ) |
||
264 | ), |
||
265 | array( 'meta_id' => $id ), |
||
266 | array( '%s', '%s' ), |
||
267 | array( '%d' ) |
||
268 | ); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | // Shipping Rows |
||
273 | $order_shipping = 0; |
||
274 | |||
275 | if ( isset( $items['shipping_method_id'] ) ) { |
||
276 | |||
277 | foreach ( $items['shipping_method_id'] as $item_id ) { |
||
278 | $item_id = absint( $item_id ); |
||
279 | $method_id = isset( $items['shipping_method'][ $item_id ] ) ? wc_clean( $items['shipping_method'][ $item_id ] ) : ''; |
||
280 | $method_title = isset( $items['shipping_method_title'][ $item_id ] ) ? wc_clean( wp_unslash( $items['shipping_method_title'][ $item_id ] ) ) : ''; |
||
281 | $cost = isset( $items['shipping_cost'][ $item_id ] ) ? wc_format_decimal( $items['shipping_cost'][ $item_id ] ) : ''; |
||
282 | $ship_taxes = isset( $items['shipping_taxes'][ $item_id ] ) ? array_map( 'wc_format_decimal', $items['shipping_taxes'][ $item_id ] ) : array(); |
||
283 | |||
284 | $wpdb->update( |
||
285 | $wpdb->prefix . 'woocommerce_order_items', |
||
286 | array( 'order_item_name' => $method_title ), |
||
287 | array( 'order_item_id' => $item_id ), |
||
288 | array( '%s' ), |
||
289 | array( '%d' ) |
||
290 | ); |
||
291 | |||
292 | wc_update_order_item_meta( $item_id, 'method_id', $method_id ); |
||
293 | wc_update_order_item_meta( $item_id, 'cost', $cost ); |
||
294 | wc_update_order_item_meta( $item_id, 'taxes', $ship_taxes ); |
||
295 | |||
296 | $taxes['shipping'][] = $ship_taxes; |
||
297 | |||
298 | $order_shipping += $cost; |
||
299 | } |
||
300 | } |
||
301 | |||
302 | // Taxes |
||
303 | $order_taxes = isset( $items['order_taxes'] ) ? $items['order_taxes'] : array(); |
||
304 | $taxes_items = array(); |
||
305 | $taxes_shipping = array(); |
||
306 | $total_tax = 0; |
||
307 | $total_shipping_tax = 0; |
||
308 | |||
309 | // Sum items taxes |
||
310 | foreach ( $taxes['items'] as $rates ) { |
||
311 | |||
312 | foreach ( $rates as $id => $value ) { |
||
313 | |||
314 | if ( isset( $taxes_items[ $id ] ) ) { |
||
315 | $taxes_items[ $id ] += $value; |
||
316 | } else { |
||
317 | $taxes_items[ $id ] = $value; |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | |||
322 | // Sum shipping taxes |
||
323 | foreach ( $taxes['shipping'] as $rates ) { |
||
324 | |||
325 | foreach ( $rates as $id => $value ) { |
||
326 | |||
327 | if ( isset( $taxes_shipping[ $id ] ) ) { |
||
328 | $taxes_shipping[ $id ] += $value; |
||
329 | } else { |
||
330 | $taxes_shipping[ $id ] = $value; |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | // Update order taxes |
||
336 | foreach ( $order_taxes as $item_id => $rate_id ) { |
||
337 | |||
338 | if ( isset( $taxes_items[ $rate_id ] ) ) { |
||
339 | $_total = wc_format_decimal( $taxes_items[ $rate_id ] ); |
||
340 | wc_update_order_item_meta( $item_id, 'tax_amount', $_total ); |
||
341 | |||
342 | $total_tax += $_total; |
||
343 | } |
||
344 | |||
345 | if ( isset( $taxes_shipping[ $rate_id ] ) ) { |
||
346 | $_total = wc_format_decimal( $taxes_shipping[ $rate_id ] ); |
||
347 | wc_update_order_item_meta( $item_id, 'shipping_tax_amount', $_total ); |
||
348 | |||
349 | $total_shipping_tax += $_total; |
||
350 | } |
||
351 | } |
||
352 | |||
353 | // Update order shipping total |
||
354 | update_post_meta( $order_id, '_order_shipping', $order_shipping ); |
||
355 | |||
356 | // Update cart discount from item totals |
||
357 | update_post_meta( $order_id, '_cart_discount', $subtotal - $total ); |
||
358 | update_post_meta( $order_id, '_cart_discount_tax', $subtotal_tax - $total_tax ); |
||
359 | |||
360 | // Update totals |
||
361 | update_post_meta( $order_id, '_order_total', wc_format_decimal( $items['_order_total'] ) ); |
||
362 | |||
363 | // Update tax |
||
364 | update_post_meta( $order_id, '_order_tax', wc_format_decimal( $total_tax ) ); |
||
365 | update_post_meta( $order_id, '_order_shipping_tax', wc_format_decimal( $total_shipping_tax ) ); |
||
366 | |||
367 | // Remove old values |
||
368 | delete_post_meta( $order_id, '_shipping_method' ); |
||
369 | delete_post_meta( $order_id, '_shipping_method_title' ); |
||
370 | |||
371 | // Set the currency |
||
372 | add_post_meta( $order_id, '_order_currency', get_woocommerce_currency(), true ); |
||
373 | |||
374 | // Update version after saving |
||
375 | update_post_meta( $order_id, '_order_version', WC_VERSION ); |
||
376 | |||
377 | // inform other plugins that the items have been saved |
||
378 | do_action( 'woocommerce_saved_order_items', $order_id, $items ); |
||
379 | } |
||
380 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.