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() ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Retrieves the transaction ID from the subscription |
||
| 299 | * |
||
| 300 | * @since 1.0.0 |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | public function get_transaction_id() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Stores the transaction ID for the subscription purchase |
||
| 321 | * |
||
| 322 | * @since 1.0.0.4 |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | public function set_transaction_id( $txn_id = '' ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Renews a subscription |
||
| 332 | * |
||
| 333 | * @since 1.0.0 |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function renew() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Marks a subscription as completed |
||
| 389 | * |
||
| 390 | * Subscription is completed when the number of payments matches the billing_times field |
||
| 391 | * |
||
| 392 | * @since 1.0.0 |
||
| 393 | * @return void |
||
| 394 | */ |
||
| 395 | public function complete() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Marks a subscription as expired |
||
| 418 | * |
||
| 419 | * Subscription is completed when the billing times is reached |
||
| 420 | * |
||
| 421 | * @since 1.0.0 |
||
| 422 | * @param $check_expiration bool True if expiration date should be checked with merchant processor before expiring |
||
| 423 | * @return void |
||
| 424 | */ |
||
| 425 | public function expire( $check_expiration = false ) { |
||
| 426 | |||
| 427 | $expiration = $this->expiration; |
||
| 428 | |||
| 429 | if( $check_expiration ) { |
||
| 430 | |||
| 431 | // check_expiration() updates $this->expiration so compare to $expiration above |
||
| 432 | |||
| 433 | if( $expiration < $this->get_expiration() && current_time( 'timestamp' ) < $this->get_expiration_time() ) { |
||
| 434 | |||
| 435 | return false; // Do not mark as expired since real expiration date is in the future |
||
| 436 | } |
||
| 437 | |||
| 438 | } |
||
| 439 | |||
| 440 | $args = array( |
||
| 441 | 'status' => 'expired' |
||
| 442 | ); |
||
| 443 | |||
| 444 | if( $this->subs_db->update( $this->id, $args ) ) { |
||
| 445 | |||
| 446 | $this->status = 'expired'; |
||
| 447 | |||
| 448 | do_action( 'wpinv_subscription_expired', $this->id, $this ); |
||
| 449 | |||
| 450 | } |
||
| 451 | |||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Marks a subscription as failing |
||
| 456 | * |
||
| 457 | * @since 2.4.2 |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | public function failing() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Marks a subscription as cancelled |
||
| 479 | * |
||
| 480 | * @since 1.0.0 |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | public function cancel() { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Determines if subscription can be cancelled |
||
| 520 | * |
||
| 521 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 522 | * that can be cancelled with a profile ID through the merchant processor |
||
| 523 | * |
||
| 524 | * @since 1.0.0 |
||
| 525 | * @return bool |
||
| 526 | */ |
||
| 527 | public function can_cancel() { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns an array of subscription statuses that can be cancelled |
||
| 537 | * |
||
| 538 | * @access public |
||
| 539 | * @since 1.0.0 |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | public function get_cancellable_statuses() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Retrieves the URL to cancel subscription |
||
| 548 | * |
||
| 549 | * @since 1.0.0 |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | public function get_cancel_url() { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Determines if subscription can be manually renewed |
||
| 561 | * |
||
| 562 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 563 | * that can be renewed manually |
||
| 564 | * |
||
| 565 | * @since 2.5 |
||
| 566 | * @return bool |
||
| 567 | */ |
||
| 568 | public function can_renew() { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Retrieves the URL to renew a subscription |
||
| 575 | * |
||
| 576 | * @since 2.5 |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | public function get_renew_url() { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Determines if subscription can have their payment method updated |
||
| 588 | * |
||
| 589 | * @since 1.0.0 |
||
| 590 | * @return bool |
||
| 591 | */ |
||
| 592 | public function can_update() { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Retrieves the URL to update subscription |
||
| 598 | * |
||
| 599 | * @since 1.0.0 |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | public function get_update_url() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Determines if subscription is active |
||
| 611 | * |
||
| 612 | * @since 1.0.0 |
||
| 613 | * @return void |
||
| 614 | */ |
||
| 615 | public function is_active() { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Determines if subscription is expired |
||
| 629 | * |
||
| 630 | * @since 1.0.0 |
||
| 631 | * @return void |
||
| 632 | */ |
||
| 633 | public function is_expired() { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Retrieves the expiration date |
||
| 662 | * |
||
| 663 | * @since 1.0.0 |
||
| 664 | * @return string |
||
| 665 | */ |
||
| 666 | public function get_expiration() { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Retrieves the expiration date in a timestamp |
||
| 672 | * |
||
| 673 | * @since 1.0.0 |
||
| 674 | * @return int |
||
| 675 | */ |
||
| 676 | public function get_expiration_time() { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Retrieves the subscription status |
||
| 682 | * |
||
| 683 | * @since 1.0.0 |
||
| 684 | * @return int |
||
| 685 | */ |
||
| 686 | public function get_status() { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Retrieves the subscription status label |
||
| 695 | * |
||
| 696 | * @since 1.0.0 |
||
| 697 | * @return int |
||
| 698 | */ |
||
| 699 | public function get_status_label() { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Retrieves the subscription status label |
||
| 740 | * |
||
| 741 | * @since 1.0.0 |
||
| 742 | * @return int |
||
| 743 | */ |
||
| 744 | public function get_status_label_html() { |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Determines if a payment exists with the specified transaction ID |
||
| 795 | * |
||
| 796 | * @since 2.4 |
||
| 797 | * @param string $txn_id The transaction ID from the merchant processor |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | public function payment_exists( $txn_id = '' ) { |
||
| 817 | |||
| 818 | } |
||
| 819 |
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.