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() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Retrieve renewal payments for a subscription |
||
| 208 | * |
||
| 209 | * @since 1.0.0 |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | public function get_child_payments() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Counts the number of payments made to the subscription |
||
| 227 | * |
||
| 228 | * @since 2.4 |
||
| 229 | * @return int |
||
| 230 | */ |
||
| 231 | public function get_total_payments() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the number of times the subscription has been billed |
||
| 243 | * |
||
| 244 | * @since 2.6 |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | public function get_times_billed() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Records a new payment on the subscription |
||
| 265 | * |
||
| 266 | * @since 2.4 |
||
| 267 | * @param array $args Array of values for the payment, including amount and transaction ID |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function add_payment( $args = array() ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Retrieves the transaction ID from the subscription |
||
| 301 | * |
||
| 302 | * @since 1.0.0 |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function get_transaction_id() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Stores the transaction ID for the subscription purchase |
||
| 323 | * |
||
| 324 | * @since 1.0.0.4 |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function set_transaction_id( $txn_id = '' ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Renews a subscription |
||
| 334 | * |
||
| 335 | * @since 1.0.0 |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function renew() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Marks a subscription as completed |
||
| 391 | * |
||
| 392 | * Subscription is completed when the number of payments matches the billing_times field |
||
| 393 | * |
||
| 394 | * @since 1.0.0 |
||
| 395 | * @return void |
||
| 396 | */ |
||
| 397 | public function complete() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Marks a subscription as expired |
||
| 420 | * |
||
| 421 | * Subscription is completed when the billing times is reached |
||
| 422 | * |
||
| 423 | * @since 1.0.0 |
||
| 424 | * @param $check_expiration bool True if expiration date should be checked with merchant processor before expiring |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | public function expire( $check_expiration = false ) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Marks a subscription as failing |
||
| 458 | * |
||
| 459 | * @since 2.4.2 |
||
| 460 | * @return void |
||
| 461 | */ |
||
| 462 | public function failing() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Marks a subscription as cancelled |
||
| 481 | * |
||
| 482 | * @since 1.0.0 |
||
| 483 | * @return void |
||
| 484 | */ |
||
| 485 | public function cancel() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Determines if subscription can be cancelled |
||
| 522 | * |
||
| 523 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 524 | * that can be cancelled with a profile ID through the merchant processor |
||
| 525 | * |
||
| 526 | * @since 1.0.0 |
||
| 527 | * @return bool |
||
| 528 | */ |
||
| 529 | public function can_cancel() { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns an array of subscription statuses that can be cancelled |
||
| 539 | * |
||
| 540 | * @access public |
||
| 541 | * @since 1.0.0 |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | public function get_cancellable_statuses() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Retrieves the URL to cancel subscription |
||
| 550 | * |
||
| 551 | * @since 1.0.0 |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | public function get_cancel_url() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Determines if subscription can be manually renewed |
||
| 563 | * |
||
| 564 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 565 | * that can be renewed manually |
||
| 566 | * |
||
| 567 | * @since 2.5 |
||
| 568 | * @return bool |
||
| 569 | */ |
||
| 570 | public function can_renew() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Retrieves the URL to renew a subscription |
||
| 577 | * |
||
| 578 | * @since 2.5 |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | public function get_renew_url() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Determines if subscription can have their payment method updated |
||
| 590 | * |
||
| 591 | * @since 1.0.0 |
||
| 592 | * @return bool |
||
| 593 | */ |
||
| 594 | public function can_update() { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Retrieves the URL to update subscription |
||
| 600 | * |
||
| 601 | * @since 1.0.0 |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | public function get_update_url() { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Determines if subscription is active |
||
| 613 | * |
||
| 614 | * @since 1.0.0 |
||
| 615 | * @return void |
||
| 616 | */ |
||
| 617 | public function is_active() { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Determines if subscription is expired |
||
| 631 | * |
||
| 632 | * @since 1.0.0 |
||
| 633 | * @return void |
||
| 634 | */ |
||
| 635 | public function is_expired() { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Retrieves the expiration date |
||
| 664 | * |
||
| 665 | * @since 1.0.0 |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | public function get_expiration() { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Retrieves the expiration date in a timestamp |
||
| 674 | * |
||
| 675 | * @since 1.0.0 |
||
| 676 | * @return int |
||
| 677 | */ |
||
| 678 | public function get_expiration_time() { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Retrieves the subscription status |
||
| 684 | * |
||
| 685 | * @since 1.0.0 |
||
| 686 | * @return int |
||
| 687 | */ |
||
| 688 | public function get_status() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Retrieves the subscription status label |
||
| 697 | * |
||
| 698 | * @since 1.0.0 |
||
| 699 | * @return int |
||
| 700 | */ |
||
| 701 | public function get_status_label() { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Retrieves the subscription status label |
||
| 742 | * |
||
| 743 | * @since 1.0.0 |
||
| 744 | * @return int |
||
| 745 | */ |
||
| 746 | public function get_status_label_html() { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Determines if a payment exists with the specified transaction ID |
||
| 797 | * |
||
| 798 | * @since 2.4 |
||
| 799 | * @param string $txn_id The transaction ID from the merchant processor |
||
| 800 | * @return bool |
||
| 801 | */ |
||
| 802 | public function payment_exists( $txn_id = '' ) { |
||
| 819 | |||
| 820 | } |
||
| 821 |
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.