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 WPInv_Subscription 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 WPInv_Subscription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class WPInv_Subscription { |
||
| 15 | |||
| 16 | private $subs_db; |
||
| 17 | |||
| 18 | public $id = 0; |
||
| 19 | public $customer_id = 0; |
||
| 20 | public $period = ''; |
||
| 21 | public $initial_amount = ''; |
||
| 22 | public $recurring_amount = ''; |
||
| 23 | public $bill_times = 0; |
||
| 24 | public $transaction_id = ''; |
||
| 25 | public $parent_payment_id = 0; |
||
| 26 | public $product_id = 0; |
||
| 27 | public $created = '0000-00-00 00:00:00'; |
||
| 28 | public $expiration = '0000-00-00 00:00:00'; |
||
| 29 | public $trial_period = ''; |
||
| 30 | public $status = 'pending'; |
||
| 31 | public $profile_id = ''; |
||
| 32 | public $gateway = ''; |
||
| 33 | public $customer; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Get us started |
||
| 37 | * |
||
| 38 | * @since 1.0.0 |
||
| 39 | * @return void |
||
|
|
|||
| 40 | */ |
||
| 41 | function __construct( $_id_or_object = 0, $_by_profile_id = false ) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Setup the subscription object |
||
| 62 | * |
||
| 63 | * @since 1.0.0 |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | private function setup_subscription( $id_or_object = 0 ) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Magic __get function to dispatch a call to retrieve a private property |
||
| 100 | * |
||
| 101 | * @since 1.0.0 |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function __get( $key ) { |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Creates a subscription |
||
| 119 | * |
||
| 120 | * @since 1.0.0 |
||
| 121 | * @param array $data Array of attributes for a subscription |
||
| 122 | * @return mixed false if data isn't passed and class not instantiated for creation |
||
| 123 | */ |
||
| 124 | public function create( $data = array() ) { |
||
| 125 | |||
| 126 | if ( $this->id != 0 ) { |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | |||
| 130 | $defaults = array( |
||
| 131 | 'customer_id' => 0, |
||
| 132 | 'frequency' => '', |
||
| 133 | 'period' => '', |
||
| 134 | 'initial_amount' => '', |
||
| 135 | 'recurring_amount' => '', |
||
| 136 | 'bill_times' => 0, |
||
| 137 | 'parent_payment_id' => 0, |
||
| 138 | 'product_id' => 0, |
||
| 139 | 'created' => '', |
||
| 140 | 'expiration' => '', |
||
| 141 | 'status' => '', |
||
| 142 | 'profile_id' => '', |
||
| 143 | ); |
||
| 144 | |||
| 145 | $args = wp_parse_args( $data, $defaults ); |
||
| 146 | |||
| 147 | if( $args['expiration'] && strtotime( 'NOW', current_time( 'timestamp' ) ) > strtotime( $args['expiration'], current_time( 'timestamp' ) ) ) { |
||
| 148 | |||
| 149 | if( 'active' == $args['status'] || 'trialling' == $args['status'] ) { |
||
| 150 | |||
| 151 | // Force an active subscription to expired if expiration date is in the past |
||
| 152 | $args['status'] = 'expired'; |
||
| 153 | |||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | do_action( 'wpinv_subscription_pre_create', $args ); |
||
| 158 | |||
| 159 | $id = $this->subs_db->insert( $args, 'subscription' ); |
||
| 160 | |||
| 161 | do_action( 'wpinv_subscription_post_create', $id, $args ); |
||
| 162 | |||
| 163 | return $this->setup_subscription( $id ); |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Updates a subscription |
||
| 169 | * |
||
| 170 | * @since 1.0.0 |
||
| 171 | * @param array $args Array of fields to update |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | public function update( $args = array() ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Delete the subscription |
||
| 186 | * |
||
| 187 | * @since 1.0.0 |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | public function delete() { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Retrieves the parent payment ID |
||
| 196 | * |
||
| 197 | * @since 1.0.0 |
||
| 198 | * @return int |
||
| 199 | */ |
||
| 200 | public function get_original_payment_id() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Retrieve renewal payments for a subscription |
||
| 206 | * |
||
| 207 | * @since 1.0.0 |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | public function get_child_payments() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Counts the number of payments made to the subscription |
||
| 225 | * |
||
| 226 | * @since 2.4 |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function get_total_payments() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the number of times the subscription has been billed |
||
| 242 | * |
||
| 243 | * @since 1.0.2 |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function get_times_billed() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Records a new payment on the subscription |
||
| 258 | * |
||
| 259 | * @since 2.4 |
||
| 260 | * @param array $args Array of values for the payment, including amount and transaction ID |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | public function add_payment( $args = array() ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Retrieves the transaction ID from the subscription |
||
| 371 | * |
||
| 372 | * @since 1.0.0 |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function get_transaction_id() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Stores the transaction ID for the subscription purchase |
||
| 393 | * |
||
| 394 | * @since 1.0.0.4 |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function set_transaction_id( $txn_id = '' ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Renews a subscription |
||
| 404 | * |
||
| 405 | * @since 1.0.0 |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function renew() { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Marks a subscription as completed |
||
| 461 | * |
||
| 462 | * Subscription is completed when the number of payments matches the billing_times field |
||
| 463 | * |
||
| 464 | * @since 1.0.0 |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | public function complete() { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Marks a subscription as expired |
||
| 490 | * |
||
| 491 | * Subscription is completed when the billing times is reached |
||
| 492 | * |
||
| 493 | * @since 1.0.0 |
||
| 494 | * @param $check_expiration bool True if expiration date should be checked with merchant processor before expiring |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function expire( $check_expiration = false ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Marks a subscription as failing |
||
| 528 | * |
||
| 529 | * @since 2.4.2 |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | public function failing() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Marks a subscription as cancelled |
||
| 551 | * |
||
| 552 | * @since 1.0.0 |
||
| 553 | * @return void |
||
| 554 | */ |
||
| 555 | public function cancel() { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Determines if subscription can be cancelled |
||
| 583 | * |
||
| 584 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 585 | * that can be cancelled with a profile ID through the merchant processor |
||
| 586 | * |
||
| 587 | * @since 1.0.0 |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | public function can_cancel() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Returns an array of subscription statuses that can be cancelled |
||
| 600 | * |
||
| 601 | * @access public |
||
| 602 | * @since 1.0.0 |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | public function get_cancellable_statuses() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Retrieves the URL to cancel subscription |
||
| 611 | * |
||
| 612 | * @since 1.0.0 |
||
| 613 | * @return string |
||
| 614 | */ |
||
| 615 | public function get_cancel_url() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Determines if subscription can be manually renewed |
||
| 624 | * |
||
| 625 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 626 | * that can be renewed manually |
||
| 627 | * |
||
| 628 | * @since 2.5 |
||
| 629 | * @return bool |
||
| 630 | */ |
||
| 631 | public function can_renew() { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Retrieves the URL to renew a subscription |
||
| 638 | * |
||
| 639 | * @since 2.5 |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public function get_renew_url() { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Determines if subscription can have their payment method updated |
||
| 651 | * |
||
| 652 | * @since 1.0.0 |
||
| 653 | * @return bool |
||
| 654 | */ |
||
| 655 | public function can_update() { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Retrieves the URL to update subscription |
||
| 661 | * |
||
| 662 | * @since 1.0.0 |
||
| 663 | * @return void |
||
| 664 | */ |
||
| 665 | public function get_update_url() { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Determines if subscription is active |
||
| 674 | * |
||
| 675 | * @since 1.0.0 |
||
| 676 | * @return void |
||
| 677 | */ |
||
| 678 | public function is_active() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Determines if subscription is expired |
||
| 692 | * |
||
| 693 | * @since 1.0.0 |
||
| 694 | * @return void |
||
| 695 | */ |
||
| 696 | public function is_expired() { |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Retrieves the expiration date |
||
| 725 | * |
||
| 726 | * @since 1.0.0 |
||
| 727 | * @return string |
||
| 728 | */ |
||
| 729 | public function get_expiration() { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Retrieves the expiration date in a timestamp |
||
| 735 | * |
||
| 736 | * @since 1.0.0 |
||
| 737 | * @return int |
||
| 738 | */ |
||
| 739 | public function get_expiration_time() { |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Retrieves the subscription status |
||
| 745 | * |
||
| 746 | * @since 1.0.0 |
||
| 747 | * @return int |
||
| 748 | */ |
||
| 749 | public function get_status() { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Retrieves the subscription status label |
||
| 758 | * |
||
| 759 | * @since 1.0.0 |
||
| 760 | * @return int |
||
| 761 | */ |
||
| 762 | public function get_status_label() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Retrieves the subscription status label |
||
| 803 | * |
||
| 804 | * @since 1.0.0 |
||
| 805 | * @return int |
||
| 806 | */ |
||
| 807 | public function get_status_label_html() { |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Determines if a payment exists with the specified transaction ID |
||
| 858 | * |
||
| 859 | * @since 2.4 |
||
| 860 | * @param string $txn_id The transaction ID from the merchant processor |
||
| 861 | * @return bool |
||
| 862 | */ |
||
| 863 | public function payment_exists( $txn_id = '' ) { |
||
| 880 | |||
| 881 | } |
||
| 882 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.