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() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns the number of times the subscription has been billed |
||
| 239 | * |
||
| 240 | * @since 2.6 |
||
| 241 | * @return int |
||
| 242 | */ |
||
| 243 | public function get_times_billed() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Records a new payment on the subscription |
||
| 257 | * |
||
| 258 | * @since 2.4 |
||
| 259 | * @param array $args Array of values for the payment, including amount and transaction ID |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function add_payment( $args = array() ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Retrieves the transaction ID from the subscription |
||
| 291 | * |
||
| 292 | * @since 1.0.0 |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function get_transaction_id() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Stores the transaction ID for the subscription purchase |
||
| 313 | * |
||
| 314 | * @since 1.0.0.4 |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | public function set_transaction_id( $txn_id = '' ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Renews a subscription |
||
| 324 | * |
||
| 325 | * @since 1.0.0 |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function renew() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Marks a subscription as completed |
||
| 381 | * |
||
| 382 | * Subscription is completed when the number of payments matches the billing_times field |
||
| 383 | * |
||
| 384 | * @since 1.0.0 |
||
| 385 | * @return void |
||
| 386 | */ |
||
| 387 | public function complete() { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Marks a subscription as expired |
||
| 410 | * |
||
| 411 | * Subscription is completed when the billing times is reached |
||
| 412 | * |
||
| 413 | * @since 1.0.0 |
||
| 414 | * @param $check_expiration bool True if expiration date should be checked with merchant processor before expiring |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public function expire( $check_expiration = false ) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Marks a subscription as failing |
||
| 448 | * |
||
| 449 | * @since 2.4.2 |
||
| 450 | * @return void |
||
| 451 | */ |
||
| 452 | public function failing() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Marks a subscription as cancelled |
||
| 471 | * |
||
| 472 | * @since 1.0.0 |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | public function cancel() { |
||
| 476 | |||
| 477 | if( 'cancelled' === $this->status ) { |
||
| 478 | return; // Already cancelled |
||
| 479 | } |
||
| 480 | |||
| 481 | $args = array( |
||
| 482 | 'status' => 'cancelled' |
||
| 483 | ); |
||
| 484 | |||
| 485 | if( $this->subs_db->update( $this->id, $args ) ) { |
||
| 486 | |||
| 487 | if( is_user_logged_in() ) { |
||
| 488 | |||
| 489 | $userdata = get_userdata( get_current_user_id() ); |
||
| 490 | $user = $userdata->display_name; |
||
| 491 | |||
| 492 | } else { |
||
| 493 | |||
| 494 | $user = __( 'gateway', 'invoicing' ); |
||
| 495 | |||
| 496 | } |
||
| 497 | |||
| 498 | $note = sprintf( __( 'Subscription for Invoice #%d cancelled by %s', 'invoicing' ), $this->parent_payment_id, $user ); |
||
| 499 | wpinv_insert_payment_note( $this->parent_payment_id, $note, '', '', true ); |
||
| 500 | |||
| 501 | $this->status = 'cancelled'; |
||
| 502 | |||
| 503 | do_action( 'wpinv_subscription_cancelled', $this->id, $this ); |
||
| 504 | |||
| 505 | |||
| 506 | } |
||
| 507 | |||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Determines if subscription can be cancelled |
||
| 512 | * |
||
| 513 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 514 | * that can be cancelled with a profile ID through the merchant processor |
||
| 515 | * |
||
| 516 | * @since 1.0.0 |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | public function can_cancel() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns an array of subscription statuses that can be cancelled |
||
| 529 | * |
||
| 530 | * @access public |
||
| 531 | * @since 1.0.0 |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | public function get_cancellable_statuses() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Retrieves the URL to cancel subscription |
||
| 540 | * |
||
| 541 | * @since 1.0.0 |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function get_cancel_url() { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Determines if subscription can be manually renewed |
||
| 553 | * |
||
| 554 | * This method is filtered by payment gateways in order to return true on subscriptions |
||
| 555 | * that can be renewed manually |
||
| 556 | * |
||
| 557 | * @since 2.5 |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | public function can_renew() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Retrieves the URL to renew a subscription |
||
| 567 | * |
||
| 568 | * @since 2.5 |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | public function get_renew_url() { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Determines if subscription can have their payment method updated |
||
| 580 | * |
||
| 581 | * @since 1.0.0 |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function can_update() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Retrieves the URL to update subscription |
||
| 590 | * |
||
| 591 | * @since 1.0.0 |
||
| 592 | * @return void |
||
| 593 | */ |
||
| 594 | public function get_update_url() { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Determines if subscription is active |
||
| 603 | * |
||
| 604 | * @since 1.0.0 |
||
| 605 | * @return void |
||
| 606 | */ |
||
| 607 | public function is_active() { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Determines if subscription is expired |
||
| 621 | * |
||
| 622 | * @since 1.0.0 |
||
| 623 | * @return void |
||
| 624 | */ |
||
| 625 | public function is_expired() { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Retrieves the expiration date |
||
| 654 | * |
||
| 655 | * @since 1.0.0 |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | public function get_expiration() { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Retrieves the expiration date in a timestamp |
||
| 664 | * |
||
| 665 | * @since 1.0.0 |
||
| 666 | * @return int |
||
| 667 | */ |
||
| 668 | public function get_expiration_time() { |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Retrieves the subscription status |
||
| 674 | * |
||
| 675 | * @since 1.0.0 |
||
| 676 | * @return int |
||
| 677 | */ |
||
| 678 | public function get_status() { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Retrieves the subscription status label |
||
| 687 | * |
||
| 688 | * @since 1.0.0 |
||
| 689 | * @return int |
||
| 690 | */ |
||
| 691 | public function get_status_label() { |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Retrieves the subscription status label |
||
| 732 | * |
||
| 733 | * @since 1.0.0 |
||
| 734 | * @return int |
||
| 735 | */ |
||
| 736 | public function get_status_label_html() { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Determines if a payment exists with the specified transaction ID |
||
| 787 | * |
||
| 788 | * @since 2.4 |
||
| 789 | * @param string $txn_id The transaction ID from the merchant processor |
||
| 790 | * @return bool |
||
| 791 | */ |
||
| 792 | public function payment_exists( $txn_id = '' ) { |
||
| 809 | |||
| 810 | } |
||
| 811 |
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.