Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EE_Transaction_Processor 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 EE_Transaction_Processor, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) { exit('No direct script access allowed'); } |
||
| 17 | class EE_Transaction_Processor extends EE_Processor_Base { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var EE_Registration_Processor $_instance |
||
| 21 | * @access private |
||
| 22 | */ |
||
| 23 | private static $_instance; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * array of query WHERE params to use when retrieving cached registrations from a transaction |
||
| 27 | * |
||
| 28 | * @var array $registration_query_params |
||
| 29 | * @access private |
||
| 30 | */ |
||
| 31 | private $_registration_query_params = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @deprecated |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $_old_txn_status; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @deprecated |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $_new_txn_status; |
||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | *@singleton method used to instantiate class object |
||
| 49 | *@access public |
||
| 50 | * @param array $registration_query_params |
||
| 51 | *@return EE_Transaction_Processor instance |
||
| 52 | */ |
||
| 53 | public static function instance( $registration_query_params = array() ) { |
||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * @param array $registration_query_params |
||
| 65 | */ |
||
| 66 | private function __construct( $registration_query_params = array() ) { |
||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * @access private |
||
| 75 | * @param array $registration_query_params |
||
| 76 | */ |
||
| 77 | private function _set_registration_query_params( $registration_query_params ) { |
||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * manually_update_registration_statuses |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | * @param EE_Transaction $transaction |
||
| 88 | * @param string $new_reg_status |
||
| 89 | * @param array $registration_query_params array of query WHERE params to use |
||
| 90 | * when retrieving cached registrations from a transaction |
||
| 91 | * @return boolean |
||
| 92 | * @throws \EE_Error |
||
| 93 | */ |
||
| 94 | public function manually_update_registration_statuses( |
||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * toggle_registration_statuses_for_default_approved_events |
||
| 124 | * |
||
| 125 | * @access public |
||
| 126 | * @param EE_Transaction $transaction |
||
| 127 | * @param array $registration_query_params array of query WHERE params to use |
||
| 128 | * when retrieving cached registrations from a transaction |
||
| 129 | * @return boolean |
||
| 130 | * @throws \EE_Error |
||
| 131 | */ |
||
| 132 | public function toggle_registration_statuses_for_default_approved_events( |
||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * toggle_registration_statuses_if_no_monies_owing |
||
| 153 | * |
||
| 154 | * @access public |
||
| 155 | * @param EE_Transaction $transaction |
||
| 156 | * @param array $registration_query_params array of query WHERE params to use |
||
| 157 | * when retrieving cached registrations from a transaction |
||
| 158 | * @return boolean |
||
| 159 | * @throws \EE_Error |
||
| 160 | */ |
||
| 161 | public function toggle_registration_statuses_if_no_monies_owing( |
||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * update_transaction_and_registrations_after_checkout_or_payment |
||
| 182 | * cycles thru related registrations and calls update_registration_after_checkout_or_payment() on each |
||
| 183 | * |
||
| 184 | * @param EE_Transaction $transaction |
||
| 185 | * @param \EE_Payment | NULL $payment |
||
| 186 | * @param array $registration_query_params array of query WHERE params to use |
||
| 187 | * when retrieving cached registrations from a transaction |
||
| 188 | * @throws \EE_Error |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | public function update_transaction_and_registrations_after_checkout_or_payment( |
||
| 238 | |||
| 239 | |||
| 240 | |||
| 241 | /** |
||
| 242 | * update_transaction_after_registration_reopened |
||
| 243 | * readjusts TXN and Line Item totals after a registration is changed from |
||
| 244 | * cancelled or declined to another reg status such as pending payment or approved |
||
| 245 | * |
||
| 246 | * @param \EE_Registration $registration |
||
| 247 | * @param array $closed_reg_statuses |
||
| 248 | * @param bool $update_txn |
||
| 249 | * @return bool |
||
| 250 | */ |
||
| 251 | public function update_transaction_after_reinstating_canceled_registration( |
||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * update_transaction_after_canceled_or_declined_registration |
||
| 291 | * readjusts TXN and Line Item totals after a registration is cancelled or declined |
||
| 292 | * |
||
| 293 | * @param \EE_Registration $registration |
||
| 294 | * @param array $closed_reg_statuses |
||
| 295 | * @param bool $update_txn |
||
| 296 | * @return bool |
||
| 297 | * @throws \EE_Error |
||
| 298 | */ |
||
| 299 | public function update_transaction_after_canceled_or_declined_registration( |
||
| 330 | |||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * get_transaction_for_registration |
||
| 335 | * |
||
| 336 | * @access public |
||
| 337 | * @param EE_Registration $registration |
||
| 338 | * @return EE_Transaction |
||
| 339 | * @throws EE_Error |
||
| 340 | */ |
||
| 341 | public function get_transaction_for_registration( EE_Registration $registration ) { |
||
| 353 | |||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * get_ticket_line_item_for_transaction_registration |
||
| 358 | * |
||
| 359 | * @access public |
||
| 360 | * @param EE_Transaction $transaction |
||
| 361 | * @param EE_Registration $registration |
||
| 362 | * @return EE_Line_Item |
||
| 363 | * @throws EE_Error |
||
| 364 | */ |
||
| 365 | public function get_ticket_line_item_for_transaction_registration( |
||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * cancel_transaction_if_all_registrations_canceled |
||
| 391 | * cycles thru related registrations and checks their statuses |
||
| 392 | * if ALL registrations are Cancelled or Declined, then this sets the TXN status to |
||
| 393 | * |
||
| 394 | * @access public |
||
| 395 | * @param EE_Transaction $transaction |
||
| 396 | * @param string $new_TXN_status |
||
| 397 | * @param array $registration_query_params - array of query WHERE params to use when |
||
| 398 | * retrieving cached registrations from a transaction |
||
| 399 | * @param array $closed_reg_statuses |
||
| 400 | * @param bool $update_txn |
||
| 401 | * @return bool true if TXN status was updated, false if not |
||
| 402 | */ |
||
| 403 | public function toggle_transaction_status_if_all_registrations_canceled_or_declined( |
||
| 431 | |||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * _call_method_on_registrations_via_Registration_Processor |
||
| 436 | * cycles thru related registrations and calls the requested method on each |
||
| 437 | * |
||
| 438 | * @access private |
||
| 439 | * @param string $method_name |
||
| 440 | * @param EE_Transaction $transaction |
||
| 441 | * @param array $registration_query_params array of query WHERE params to use |
||
| 442 | * when retrieving cached registrations from a transaction |
||
| 443 | * @param string $additional_param |
||
| 444 | * @throws \EE_Error |
||
| 445 | * @return boolean |
||
| 446 | */ |
||
| 447 | private function _call_method_on_registrations_via_Registration_Processor( |
||
| 478 | |||
| 479 | |||
| 480 | |||
| 481 | /** |
||
| 482 | * set_transaction_payment_method_based_on_registration_statuses |
||
| 483 | * sets or unsets the PMD_ID field on the TXN based on the related REG statuses |
||
| 484 | * basically if ALL Registrations are "Not Approved", then the EE_Transaction.PMD_ID is set to null, |
||
| 485 | * but if any Registration has a different status, then EE_Transaction.PMD_ID is set to either: |
||
| 486 | * the first "default" Payment Method |
||
| 487 | * the first active Payment Method |
||
| 488 | * whichever is found first. |
||
| 489 | * |
||
| 490 | * @param EE_Registration $edited_registration |
||
| 491 | * @return void |
||
| 492 | * @throws \EE_Error |
||
| 493 | */ |
||
| 494 | public function set_transaction_payment_method_based_on_registration_statuses( |
||
| 550 | |||
| 551 | |||
| 552 | |||
| 553 | /********************************** DEPRECATED METHODS **********************************/ |
||
| 554 | |||
| 555 | |||
| 556 | |||
| 557 | /** |
||
| 558 | * @deprecated 4.9.12 |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function old_txn_status() { |
||
| 572 | |||
| 573 | |||
| 574 | |||
| 575 | /** |
||
| 576 | * @deprecated 4.9.12 |
||
| 577 | * @param string $old_txn_status |
||
| 578 | */ |
||
| 579 | View Code Duplication | public function set_old_txn_status( $old_txn_status ) { |
|
| 593 | |||
| 594 | |||
| 595 | |||
| 596 | /** |
||
| 597 | * @deprecated 4.9.12 |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | public function new_txn_status() { |
||
| 611 | |||
| 612 | |||
| 613 | |||
| 614 | /** |
||
| 615 | * @deprecated 4.9.12 |
||
| 616 | * @param string $new_txn_status |
||
| 617 | */ |
||
| 618 | public function set_new_txn_status( $new_txn_status ) { |
||
| 629 | |||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * reg_status_updated |
||
| 634 | * |
||
| 635 | * @deprecated 4.9.12 |
||
| 636 | * @return bool |
||
| 637 | */ |
||
| 638 | View Code Duplication | public function txn_status_updated() { |
|
| 649 | |||
| 650 | |||
| 651 | |||
| 652 | /** |
||
| 653 | * all_reg_steps_completed |
||
| 654 | * returns: |
||
| 655 | * true if ALL reg steps have been marked as completed |
||
| 656 | * or false if any step is not completed |
||
| 657 | * |
||
| 658 | * @deprecated 4.9.12 |
||
| 659 | * @param EE_Transaction $transaction |
||
| 660 | * @return boolean |
||
| 661 | */ |
||
| 662 | public function all_reg_steps_completed( EE_Transaction $transaction ) { |
||
| 674 | |||
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * all_reg_steps_completed_except |
||
| 679 | * returns: |
||
| 680 | * true if ALL reg steps, except a particular step that you wish to skip over, have been marked as completed |
||
| 681 | * or false if any other step is not completed |
||
| 682 | * or false if ALL steps are completed including the exception you are testing !!! |
||
| 683 | * |
||
| 684 | * @deprecated 4.9.12 |
||
| 685 | * @param EE_Transaction $transaction |
||
| 686 | * @param string $exception |
||
| 687 | * @return boolean |
||
| 688 | */ |
||
| 689 | public function all_reg_steps_completed_except( EE_Transaction $transaction, $exception = '' ) { |
||
| 701 | |||
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * all_reg_steps_completed_except |
||
| 706 | * returns: |
||
| 707 | * true if ALL reg steps, except the final step, have been marked as completed |
||
| 708 | * or false if any step is not completed |
||
| 709 | * or false if ALL steps are completed including the final step !!! |
||
| 710 | * |
||
| 711 | * @deprecated 4.9.12 |
||
| 712 | * @param EE_Transaction $transaction |
||
| 713 | * @return boolean |
||
| 714 | */ |
||
| 715 | public function all_reg_steps_completed_except_final_step( EE_Transaction $transaction ) { |
||
| 727 | |||
| 728 | |||
| 729 | |||
| 730 | /** |
||
| 731 | * reg_step_completed |
||
| 732 | * returns: |
||
| 733 | * true if a specific reg step has been marked as completed |
||
| 734 | * a Unix timestamp if it has been initialized but not yet completed, |
||
| 735 | * or false if it has not yet been initialized |
||
| 736 | * |
||
| 737 | * @deprecated 4.9.12 |
||
| 738 | * @param EE_Transaction $transaction |
||
| 739 | * @param string $reg_step_slug |
||
| 740 | * @return boolean | int |
||
| 741 | */ |
||
| 742 | public function reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 754 | |||
| 755 | |||
| 756 | |||
| 757 | /** |
||
| 758 | * completed_final_reg_step |
||
| 759 | * returns: |
||
| 760 | * true if the finalize_registration reg step has been marked as completed |
||
| 761 | * a Unix timestamp if it has been initialized but not yet completed, |
||
| 762 | * or false if it has not yet been initialized |
||
| 763 | * |
||
| 764 | * @deprecated 4.9.12 |
||
| 765 | * @param EE_Transaction $transaction |
||
| 766 | * @return boolean | int |
||
| 767 | */ |
||
| 768 | public function final_reg_step_completed( EE_Transaction $transaction ) { |
||
| 780 | |||
| 781 | |||
| 782 | |||
| 783 | /** |
||
| 784 | * set_reg_step_initiated |
||
| 785 | * given a valid TXN_reg_step, this sets it's value to a unix timestamp |
||
| 786 | * |
||
| 787 | * @deprecated 4.9.12 |
||
| 788 | * @access public |
||
| 789 | * @param \EE_Transaction $transaction |
||
| 790 | * @param string $reg_step_slug |
||
| 791 | * @return boolean |
||
| 792 | * @throws \EE_Error |
||
| 793 | */ |
||
| 794 | public function set_reg_step_initiated( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 806 | |||
| 807 | |||
| 808 | |||
| 809 | /** |
||
| 810 | * set_reg_step_completed |
||
| 811 | * given a valid TXN_reg_step, this sets the step as completed |
||
| 812 | * |
||
| 813 | * @deprecated 4.9.12 |
||
| 814 | * @access public |
||
| 815 | * @param \EE_Transaction $transaction |
||
| 816 | * @param string $reg_step_slug |
||
| 817 | * @return boolean |
||
| 818 | * @throws \EE_Error |
||
| 819 | */ |
||
| 820 | public function set_reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 832 | |||
| 833 | |||
| 834 | |||
| 835 | /** |
||
| 836 | * set_reg_step_completed |
||
| 837 | * given a valid TXN_reg_step slug, this sets the step as NOT completed |
||
| 838 | * |
||
| 839 | * @deprecated 4.9.12 |
||
| 840 | * @access public |
||
| 841 | * @param \EE_Transaction $transaction |
||
| 842 | * @param string $reg_step_slug |
||
| 843 | * @return boolean |
||
| 844 | * @throws \EE_Error |
||
| 845 | */ |
||
| 846 | public function set_reg_step_not_completed( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 858 | |||
| 859 | |||
| 860 | |||
| 861 | |||
| 862 | /** |
||
| 863 | * remove_reg_step |
||
| 864 | * given a valid TXN_reg_step slug, this will remove (unset) |
||
| 865 | * the reg step from the TXN reg step array |
||
| 866 | * |
||
| 867 | * @deprecated 4.9.12 |
||
| 868 | * @access public |
||
| 869 | * @param \EE_Transaction $transaction |
||
| 870 | * @param string $reg_step_slug |
||
| 871 | * @return void |
||
| 872 | */ |
||
| 873 | public function remove_reg_step( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 885 | |||
| 886 | |||
| 887 | |||
| 888 | /** |
||
| 889 | * toggle_failed_transaction_status |
||
| 890 | * upgrades a TXNs status from failed to abandoned, |
||
| 891 | * meaning that contact information has been captured for at least one registrant |
||
| 892 | * |
||
| 893 | * @deprecated 4.9.12 |
||
| 894 | * @access public |
||
| 895 | * @param EE_Transaction $transaction |
||
| 896 | * @return boolean |
||
| 897 | * @throws \EE_Error |
||
| 898 | */ |
||
| 899 | public function toggle_failed_transaction_status( EE_Transaction $transaction ) { |
||
| 911 | |||
| 912 | |||
| 913 | |||
| 914 | /** |
||
| 915 | * toggle_abandoned_transaction_status |
||
| 916 | * upgrades a TXNs status from failed or abandoned to incomplete |
||
| 917 | * |
||
| 918 | * @deprecated 4.9.12 |
||
| 919 | * @access public |
||
| 920 | * @param EE_Transaction $transaction |
||
| 921 | * @return boolean |
||
| 922 | */ |
||
| 923 | public function toggle_abandoned_transaction_status( EE_Transaction $transaction ) { |
||
| 935 | |||
| 936 | |||
| 937 | |||
| 938 | } |
||
| 939 | // End of file EE_Transaction_Processor.class.php |
||
| 941 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..