Conditions | 46 |
Paths | > 20000 |
Total Lines | 217 |
Lines | 14 |
Ratio | 6.45 % |
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 |
||
317 | public function get_data() { |
||
318 | |||
319 | $data = array(); |
||
320 | $i = 0; |
||
321 | // Payment query. |
||
322 | $payments = give_get_payments( $this->get_donation_argument() ); |
||
323 | |||
324 | if ( $payments ) { |
||
325 | |||
326 | foreach ( $payments as $payment ) { |
||
327 | |||
328 | $columns = $this->csv_cols(); |
||
329 | $payment = new Give_Payment( $payment->ID ); |
||
330 | $payment_meta = $payment->payment_meta; |
||
331 | $address = $payment->address; |
||
332 | |||
333 | // Set columns. |
||
334 | if ( ! empty( $columns['donation_id'] ) ) { |
||
335 | $data[ $i ]['donation_id'] = $payment->ID; |
||
336 | } |
||
337 | |||
338 | if ( ! empty( $columns['seq_id'] ) ) { |
||
339 | $data[ $i ]['seq_id'] = Give()->seq_donation_number->get_serial_code( $payment->ID ); |
||
340 | } |
||
341 | |||
342 | if ( ! empty( $columns['title_prefix'] ) ) { |
||
343 | $data[ $i ]['title_prefix'] = ! empty( $payment->title_prefix ) ? $payment->title_prefix : ''; |
||
344 | } |
||
345 | |||
346 | View Code Duplication | if ( ! empty( $columns['first_name'] ) ) { |
|
347 | $data[ $i ]['first_name'] = isset( $payment->first_name ) ? $payment->first_name : ''; |
||
348 | } |
||
349 | |||
350 | View Code Duplication | if ( ! empty( $columns['last_name'] ) ) { |
|
351 | $data[ $i ]['last_name'] = isset( $payment->last_name ) ? $payment->last_name : ''; |
||
352 | } |
||
353 | |||
354 | if ( ! empty( $columns['email'] ) ) { |
||
355 | $data[ $i ]['email'] = $payment->email; |
||
356 | } |
||
357 | |||
358 | if ( ! empty( $columns['company'] ) ) { |
||
359 | $data[ $i ]['company'] = empty( $payment_meta['_give_donation_company'] ) ? '' : str_replace( "\'", "'", $payment_meta['_give_donation_company'] ); |
||
360 | } |
||
361 | |||
362 | View Code Duplication | if ( ! empty( $columns['address_line1'] ) ) { |
|
363 | $data[ $i ]['address_line1'] = isset( $address['line1'] ) ? $address['line1'] : ''; |
||
364 | $data[ $i ]['address_line2'] = isset( $address['line2'] ) ? $address['line2'] : ''; |
||
365 | $data[ $i ]['address_city'] = isset( $address['city'] ) ? $address['city'] : ''; |
||
366 | $data[ $i ]['address_state'] = isset( $address['state'] ) ? $address['state'] : ''; |
||
367 | $data[ $i ]['address_zip'] = isset( $address['zip'] ) ? $address['zip'] : ''; |
||
368 | $data[ $i ]['address_country'] = isset( $address['country'] ) ? $address['country'] : ''; |
||
369 | } |
||
370 | |||
371 | if ( ! empty( $columns['comment'] ) ) { |
||
372 | $comment = give_get_donor_donation_comment( $payment->ID, $payment->donor_id ); |
||
373 | $data[ $i ]['comment'] = ! empty( $comment ) ? $comment->comment_content : ''; |
||
374 | } |
||
375 | |||
376 | if ( ! empty( $columns['donation_total'] ) ) { |
||
377 | $data[ $i ]['donation_total'] = give_format_amount( give_donation_amount( $payment->ID ) ); |
||
378 | } |
||
379 | |||
380 | if ( ! empty( $columns['currency_code'] ) ) { |
||
381 | $data[ $i ]['currency_code'] = empty( $payment_meta['_give_payment_currency'] ) ? give_get_currency() : $payment_meta['_give_payment_currency']; |
||
382 | } |
||
383 | |||
384 | if ( ! empty( $columns['currency_symbol'] ) ) { |
||
385 | $currency_code = $data[ $i ]['currency_code']; |
||
386 | $data[ $i ]['currency_symbol'] = give_currency_symbol( $currency_code, true ); |
||
387 | } |
||
388 | |||
389 | if ( ! empty( $columns['donation_status'] ) ) { |
||
390 | $data[ $i ]['donation_status'] = give_get_payment_status( $payment, true ); |
||
391 | } |
||
392 | |||
393 | if ( ! empty( $columns['payment_gateway'] ) ) { |
||
394 | $data[ $i ]['payment_gateway'] = $payment->gateway; |
||
395 | } |
||
396 | |||
397 | if ( ! empty( $columns['payment_mode'] ) ) { |
||
398 | $data[ $i ]['payment_mode'] = $payment->mode; |
||
399 | } |
||
400 | |||
401 | if ( ! empty( $columns['form_id'] ) ) { |
||
402 | $data[ $i ]['form_id'] = $payment->form_id; |
||
403 | } |
||
404 | |||
405 | if ( ! empty( $columns['form_title'] ) ) { |
||
406 | $data[ $i ]['form_title'] = get_the_title( $payment->form_id ); |
||
407 | } |
||
408 | |||
409 | if ( ! empty( $columns['form_level_id'] ) ) { |
||
410 | $data[ $i ]['form_level_id'] = $payment->price_id; |
||
411 | } |
||
412 | |||
413 | if ( ! empty( $columns['form_level_title'] ) ) { |
||
414 | $var_prices = give_has_variable_prices( $payment->form_id ); |
||
415 | if ( empty( $var_prices ) ) { |
||
416 | $data[ $i ]['form_level_title'] = ''; |
||
417 | } else { |
||
418 | if ( 'custom' === $payment->price_id ) { |
||
419 | $custom_amount_text = give_get_meta( $payment->form_id, '_give_custom_amount_text', true ); |
||
420 | |||
421 | if ( empty( $custom_amount_text ) ) { |
||
422 | $custom_amount_text = esc_html__( 'Custom', 'give' ); |
||
423 | } |
||
424 | $data[ $i ]['form_level_title'] = $custom_amount_text; |
||
425 | } else { |
||
426 | $data[ $i ]['form_level_title'] = give_get_price_option_name( $payment->form_id, $payment->price_id ); |
||
427 | } |
||
428 | } |
||
429 | } |
||
430 | |||
431 | if ( ! empty( $columns['donation_date'] ) ) { |
||
432 | $payment_date = strtotime( $payment->date ); |
||
433 | $data[ $i ]['donation_date'] = date( give_date_format(), $payment_date ); |
||
434 | } |
||
435 | |||
436 | if ( ! empty( $columns['donation_time'] ) ) { |
||
437 | $payment_date = strtotime( $payment->date ); |
||
438 | $data[ $i ]['donation_time'] = date_i18n( 'H', $payment_date ) . ':' . date( 'i', $payment_date ); |
||
439 | } |
||
440 | |||
441 | if ( ! empty( $columns['userid'] ) ) { |
||
442 | $data[ $i ]['userid'] = $payment->user_id; |
||
443 | } |
||
444 | |||
445 | if ( ! empty( $columns['donorid'] ) ) { |
||
446 | $data[ $i ]['donorid'] = $payment->customer_id; |
||
447 | } |
||
448 | |||
449 | if ( ! empty( $columns['donor_ip'] ) ) { |
||
450 | $data[ $i ]['donor_ip'] = give_get_payment_user_ip( $payment->ID ); |
||
451 | } |
||
452 | |||
453 | // Add custom field data. |
||
454 | // First we remove the standard included keys from above. |
||
455 | $remove_keys = array( |
||
456 | 'donation_id', |
||
457 | 'seq_id', |
||
458 | 'first_name', |
||
459 | 'last_name', |
||
460 | 'email', |
||
461 | 'address_line1', |
||
462 | 'address_line2', |
||
463 | 'address_city', |
||
464 | 'address_state', |
||
465 | 'address_zip', |
||
466 | 'address_country', |
||
467 | 'donation_total', |
||
468 | 'payment_gateway', |
||
469 | 'payment_mode', |
||
470 | 'form_id', |
||
471 | 'form_title', |
||
472 | 'form_level_id', |
||
473 | 'form_level_title', |
||
474 | 'donation_date', |
||
475 | 'donation_time', |
||
476 | 'userid', |
||
477 | 'donorid', |
||
478 | 'donor_ip', |
||
479 | ); |
||
480 | |||
481 | // Removing above keys... |
||
482 | foreach ( $remove_keys as $key ) { |
||
483 | unset( $columns[ $key ] ); |
||
484 | } |
||
485 | |||
486 | // Now loop through remaining meta fields. |
||
487 | foreach ( $columns as $col ) { |
||
488 | $field_data = get_post_meta( $payment->ID, $col, true ); |
||
489 | $data[ $i ][ $col ] = $field_data; |
||
490 | unset( $columns[ $col ] ); |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Filter to modify Donation CSV data when exporting donation |
||
495 | * |
||
496 | * @since 2.1 |
||
497 | * |
||
498 | * @param array Donation data |
||
499 | * @param Give_Payment $payment Instance of Give_Payment |
||
500 | * @param array $columns Donation data $columns that are not being merge |
||
501 | * @param Give_Export_Donations_CSV $this Instance of Give_Export_Donations_CSV |
||
502 | * |
||
503 | * @return array Donation data |
||
504 | */ |
||
505 | $data[ $i ] = apply_filters( 'give_export_donation_data', $data[ $i ], $payment, $columns, $this ); |
||
506 | |||
507 | $new_data = array(); |
||
508 | $old_data = $data[ $i ]; |
||
509 | |||
510 | // sorting the columns bas on row |
||
511 | foreach ( $this->csv_cols() as $key => $value ) { |
||
512 | if ( array_key_exists( $key, $old_data ) ) { |
||
513 | $new_data[ $key ] = $old_data[ $key ]; |
||
514 | } |
||
515 | } |
||
516 | |||
517 | $data[ $i ] = $new_data; |
||
518 | |||
519 | // Increment iterator. |
||
520 | $i ++; |
||
521 | |||
522 | } |
||
523 | |||
524 | $data = apply_filters( 'give_export_get_data', $data ); |
||
525 | $data = apply_filters( "give_export_get_data_{$this->export_type}", $data ); |
||
526 | |||
527 | return $data; |
||
528 | |||
529 | } |
||
530 | |||
531 | return array(); |
||
532 | |||
533 | } |
||
534 | |||
599 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.