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 | * initial txn status at the beginning of this request. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $_old_txn_status; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * txn status at the end of the request after all processing. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $_new_txn_status; |
||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | *@singleton method used to instantiate class object |
||
| 51 | *@access public |
||
| 52 | * @param array $registration_query_params |
||
| 53 | *@return EE_Transaction_Processor instance |
||
| 54 | */ |
||
| 55 | public static function instance( $registration_query_params = array() ) { |
||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @param array $registration_query_params |
||
| 67 | * @return EE_Transaction_Processor |
||
| 68 | */ |
||
| 69 | private function __construct( $registration_query_params = array() ) { |
||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * @access private |
||
| 78 | * @param array $registration_query_params |
||
| 79 | */ |
||
| 80 | private function _set_registration_query_params( $registration_query_params ) { |
||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function old_txn_status() { |
||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $old_txn_status |
||
| 97 | */ |
||
| 98 | public function set_old_txn_status( $old_txn_status ) { |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function new_txn_status() { |
||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $new_txn_status |
||
| 118 | */ |
||
| 119 | public function set_new_txn_status( $new_txn_status ) { |
||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * reg_status_updated |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function txn_status_updated() { |
||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * _reg_steps_completed |
||
| 138 | * |
||
| 139 | * if $check_all is TRUE, then returns TRUE if ALL reg steps have been marked as completed, |
||
| 140 | * if a $reg_step_slug is provided, then this step will be skipped when testing for completion |
||
| 141 | * |
||
| 142 | * if $check_all is FALSE and a $reg_step_slug is provided, then ONLY that reg step will be tested for completion |
||
| 143 | * |
||
| 144 | * @access private |
||
| 145 | * @param EE_Transaction $transaction |
||
| 146 | * @param string $reg_step_slug |
||
| 147 | * @param bool $check_all |
||
| 148 | * @return boolean | int |
||
| 149 | */ |
||
| 150 | private function _reg_steps_completed( EE_Transaction $transaction, $reg_step_slug = '', $check_all = TRUE ) { |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * all_reg_steps_completed |
||
| 190 | * |
||
| 191 | * returns: |
||
| 192 | * true if ALL reg steps have been marked as completed |
||
| 193 | * or false if any step is not completed |
||
| 194 | * |
||
| 195 | * @param EE_Transaction $transaction |
||
| 196 | * @return boolean |
||
| 197 | */ |
||
| 198 | public function all_reg_steps_completed( EE_Transaction $transaction ) { |
||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * all_reg_steps_completed_except |
||
| 206 | * |
||
| 207 | * returns: |
||
| 208 | * true if ALL reg steps, except a particular step that you wish to skip over, have been marked as completed |
||
| 209 | * or false if any other step is not completed |
||
| 210 | * or false if ALL steps are completed including the exception you are testing !!! |
||
| 211 | * |
||
| 212 | * @param EE_Transaction $transaction |
||
| 213 | * @param string $exception |
||
| 214 | * @return boolean |
||
| 215 | */ |
||
| 216 | public function all_reg_steps_completed_except( EE_Transaction $transaction, $exception = '' ) { |
||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * all_reg_steps_completed_except |
||
| 224 | * |
||
| 225 | * returns: |
||
| 226 | * true if ALL reg steps, except the final step, have been marked as completed |
||
| 227 | * or false if any step is not completed |
||
| 228 | * or false if ALL steps are completed including the final step !!! |
||
| 229 | * |
||
| 230 | * @param EE_Transaction $transaction |
||
| 231 | * @return boolean |
||
| 232 | */ |
||
| 233 | public function all_reg_steps_completed_except_final_step( EE_Transaction $transaction ) { |
||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * reg_step_completed |
||
| 241 | * |
||
| 242 | * returns: |
||
| 243 | * true if a specific reg step has been marked as completed |
||
| 244 | * a Unix timestamp if it has been initialized but not yet completed, |
||
| 245 | * or false if it has not yet been initialized |
||
| 246 | * |
||
| 247 | * @param EE_Transaction $transaction |
||
| 248 | * @param string $reg_step_slug |
||
| 249 | * @return boolean | int |
||
| 250 | */ |
||
| 251 | public function reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * completed_final_reg_step |
||
| 259 | * |
||
| 260 | * returns: |
||
| 261 | * true if the finalize_registration reg step has been marked as completed |
||
| 262 | * a Unix timestamp if it has been initialized but not yet completed, |
||
| 263 | * or false if it has not yet been initialized |
||
| 264 | * |
||
| 265 | * @param EE_Transaction $transaction |
||
| 266 | * @return boolean | int |
||
| 267 | */ |
||
| 268 | public function final_reg_step_completed( EE_Transaction $transaction ) { |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * set_reg_step_initiated |
||
| 276 | * given a valid TXN_reg_step, this sets it's value to a unix timestamp |
||
| 277 | * |
||
| 278 | * @access public |
||
| 279 | * @param \EE_Transaction $transaction |
||
| 280 | * @param string $reg_step_slug |
||
| 281 | * @return boolean |
||
| 282 | * @throws \EE_Error |
||
| 283 | */ |
||
| 284 | public function set_reg_step_initiated( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * set_reg_step_completed |
||
| 292 | * given a valid TXN_reg_step, this sets the step as completed |
||
| 293 | * |
||
| 294 | * @access public |
||
| 295 | * @param \EE_Transaction $transaction |
||
| 296 | * @param string $reg_step_slug |
||
| 297 | * @return boolean |
||
| 298 | * @throws \EE_Error |
||
| 299 | */ |
||
| 300 | public function set_reg_step_completed( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * set_reg_step_completed |
||
| 308 | * given a valid TXN_reg_step slug, this sets the step as NOT completed |
||
| 309 | * |
||
| 310 | * @access public |
||
| 311 | * @param \EE_Transaction $transaction |
||
| 312 | * @param string $reg_step_slug |
||
| 313 | * @return boolean |
||
| 314 | * @throws \EE_Error |
||
| 315 | */ |
||
| 316 | public function set_reg_step_not_completed( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * set_reg_step_completed |
||
| 324 | * given a valid reg step slug, this sets the TXN_reg_step completed status which is either: |
||
| 325 | * |
||
| 326 | * @access private |
||
| 327 | * @param \EE_Transaction $transaction |
||
| 328 | * @param string $reg_step_slug |
||
| 329 | * @param boolean|int $status |
||
| 330 | * @return boolean |
||
| 331 | * @throws \EE_Error |
||
| 332 | */ |
||
| 333 | private function _set_reg_step_completed_status( EE_Transaction $transaction, $reg_step_slug, $status ) { |
||
| 376 | |||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * remove_reg_step |
||
| 381 | * given a valid TXN_reg_step slug, this will remove (unset) |
||
| 382 | * the reg step from the TXN reg step array |
||
| 383 | * |
||
| 384 | * @access public |
||
| 385 | * @param \EE_Transaction $transaction |
||
| 386 | * @param string $reg_step_slug |
||
| 387 | * @return void |
||
| 388 | */ |
||
| 389 | public function remove_reg_step( EE_Transaction $transaction, $reg_step_slug ) { |
||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * toggle_failed_transaction_status |
||
| 400 | * upgrades a TXNs status from failed to abandoned, |
||
| 401 | * meaning that contact information has been captured for at least one registrant |
||
| 402 | * |
||
| 403 | * @access public |
||
| 404 | * @param EE_Transaction $transaction |
||
| 405 | * @return boolean |
||
| 406 | * @throws \EE_Error |
||
| 407 | */ |
||
| 408 | public function toggle_failed_transaction_status( EE_Transaction $transaction ) { |
||
| 422 | |||
| 423 | |||
| 424 | |||
| 425 | /** |
||
| 426 | * toggle_abandoned_transaction_status |
||
| 427 | * upgrades a TXNs status from failed or abandoned to incomplete |
||
| 428 | * |
||
| 429 | * @access public |
||
| 430 | * @param EE_Transaction $transaction |
||
| 431 | * @return boolean |
||
| 432 | */ |
||
| 433 | public function toggle_abandoned_transaction_status( EE_Transaction $transaction ) { |
||
| 459 | |||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * manually_update_registration_statuses |
||
| 464 | * |
||
| 465 | * @access public |
||
| 466 | * @param EE_Transaction $transaction |
||
| 467 | * @param string $new_reg_status |
||
| 468 | * @param array $registration_query_params array of query WHERE params to use |
||
| 469 | * when retrieving cached registrations from a transaction |
||
| 470 | * @return boolean |
||
| 471 | * @throws \EE_Error |
||
| 472 | */ |
||
| 473 | public function manually_update_registration_statuses( |
||
| 498 | |||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * toggle_registration_statuses_for_default_approved_events |
||
| 503 | * |
||
| 504 | * @access public |
||
| 505 | * @param EE_Transaction $transaction |
||
| 506 | * @param array $registration_query_params array of query WHERE params to use |
||
| 507 | * when retrieving cached registrations from a transaction |
||
| 508 | * @return boolean |
||
| 509 | * @throws \EE_Error |
||
| 510 | */ |
||
| 511 | public function toggle_registration_statuses_for_default_approved_events( |
||
| 527 | |||
| 528 | |||
| 529 | |||
| 530 | /** |
||
| 531 | * toggle_registration_statuses_if_no_monies_owing |
||
| 532 | * |
||
| 533 | * @access public |
||
| 534 | * @param EE_Transaction $transaction |
||
| 535 | * @param array $registration_query_params array of query WHERE params to use |
||
| 536 | * when retrieving cached registrations from a transaction |
||
| 537 | * @return boolean |
||
| 538 | * @throws \EE_Error |
||
| 539 | */ |
||
| 540 | public function toggle_registration_statuses_if_no_monies_owing( |
||
| 556 | |||
| 557 | |||
| 558 | |||
| 559 | /** |
||
| 560 | * update_transaction_and_registrations_after_checkout_or_payment |
||
| 561 | * cycles thru related registrations and calls update_registration_after_checkout_or_payment() on each |
||
| 562 | * |
||
| 563 | * @param EE_Transaction $transaction |
||
| 564 | * @param \EE_Payment | NULL $payment |
||
| 565 | * @param array $registration_query_params array of query WHERE params to use |
||
| 566 | * when retrieving cached registrations from a transaction |
||
| 567 | * @throws \EE_Error |
||
| 568 | * @return array |
||
| 569 | */ |
||
| 570 | public function update_transaction_and_registrations_after_checkout_or_payment( |
||
| 619 | |||
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * update_transaction_after_registration_reopened |
||
| 624 | * readjusts TXN and Line Item totals after a registration is changed from |
||
| 625 | * cancelled or declined to another reg status such as pending payment or approved |
||
| 626 | * |
||
| 627 | * @param \EE_Registration $registration |
||
| 628 | * @param array $closed_reg_statuses |
||
| 629 | * @param bool $update_txn |
||
| 630 | * @return bool |
||
| 631 | */ |
||
| 632 | public function update_transaction_after_reinstating_canceled_registration( |
||
| 667 | |||
| 668 | |||
| 669 | |||
| 670 | /** |
||
| 671 | * update_transaction_after_canceled_or_declined_registration |
||
| 672 | * readjusts TXN and Line Item totals after a registration is cancelled or declined |
||
| 673 | * |
||
| 674 | * @param \EE_Registration $registration |
||
| 675 | * @param array $closed_reg_statuses |
||
| 676 | * @param bool $update_txn |
||
| 677 | * @return bool |
||
| 678 | * @throws \EE_Error |
||
| 679 | */ |
||
| 680 | public function update_transaction_after_canceled_or_declined_registration( |
||
| 711 | |||
| 712 | |||
| 713 | |||
| 714 | /** |
||
| 715 | * get_transaction_for_registration |
||
| 716 | * |
||
| 717 | * @access public |
||
| 718 | * @param EE_Registration $registration |
||
| 719 | * @return EE_Transaction |
||
| 720 | * @throws EE_Error |
||
| 721 | */ |
||
| 722 | public function get_transaction_for_registration( EE_Registration $registration ) { |
||
| 734 | |||
| 735 | |||
| 736 | |||
| 737 | /** |
||
| 738 | * get_ticket_line_item_for_transaction_registration |
||
| 739 | * |
||
| 740 | * @access public |
||
| 741 | * @param EE_Transaction $transaction |
||
| 742 | * @param EE_Registration $registration |
||
| 743 | * @return EE_Line_Item |
||
| 744 | * @throws EE_Error |
||
| 745 | */ |
||
| 746 | public function get_ticket_line_item_for_transaction_registration( |
||
| 767 | |||
| 768 | |||
| 769 | |||
| 770 | /** |
||
| 771 | * cancel_transaction_if_all_registrations_canceled |
||
| 772 | * cycles thru related registrations and checks their statuses |
||
| 773 | * if ALL registrations are Cancelled or Declined, then this sets the TXN status to |
||
| 774 | * |
||
| 775 | * @access public |
||
| 776 | * @param EE_Transaction $transaction |
||
| 777 | * @param string $new_TXN_status |
||
| 778 | * @param array $registration_query_params - array of query WHERE params to use when |
||
| 779 | * retrieving cached registrations from a transaction |
||
| 780 | * @param array $closed_reg_statuses |
||
| 781 | * @param bool $update_txn |
||
| 782 | * @return bool true if TXN status was updated, false if not |
||
| 783 | */ |
||
| 784 | public function toggle_transaction_status_if_all_registrations_canceled_or_declined( |
||
| 816 | |||
| 817 | |||
| 818 | |||
| 819 | /** |
||
| 820 | * _call_method_on_registrations_via_Registration_Processor |
||
| 821 | * cycles thru related registrations and calls the requested method on each |
||
| 822 | * |
||
| 823 | * @access private |
||
| 824 | * @param string $method_name |
||
| 825 | * @param EE_Transaction $transaction |
||
| 826 | * @param array $registration_query_params array of query WHERE params to use |
||
| 827 | * when retrieving cached registrations from a transaction |
||
| 828 | * @param string $additional_param |
||
| 829 | * @throws \EE_Error |
||
| 830 | * @return boolean |
||
| 831 | */ |
||
| 832 | private function _call_method_on_registrations_via_Registration_Processor( |
||
| 863 | |||
| 864 | |||
| 865 | |||
| 866 | /** |
||
| 867 | * set_transaction_payment_method_based_on_registration_statuses |
||
| 868 | * sets or unsets the PMD_ID field on the TXN based on the related REG statuses |
||
| 869 | * basically if ALL Registrations are "Not Approved", then the EE_Transaction.PMD_ID is set to null, |
||
| 870 | * but if any Registration has a different status, then EE_Transaction.PMD_ID is set to either: |
||
| 871 | * the first "default" Payment Method |
||
| 872 | * the first active Payment Method |
||
| 873 | * whichever is found first. |
||
| 874 | * |
||
| 875 | * @param EE_Registration $edited_registration |
||
| 876 | * @return void |
||
| 877 | * @throws \EE_Error |
||
| 878 | */ |
||
| 879 | public function set_transaction_payment_method_based_on_registration_statuses( |
||
| 935 | |||
| 936 | } |
||
| 937 | |||
| 942 |
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..