Complex classes like Give_License 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 Give_License, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Give_License { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * File |
||
| 30 | * |
||
| 31 | * @access private |
||
| 32 | * @since 1.0 |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $file; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * License |
||
| 40 | * |
||
| 41 | * @access private |
||
| 42 | * @since 1.0 |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $license; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Item name |
||
| 50 | * |
||
| 51 | * @access private |
||
| 52 | * @since 1.0 |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $item_name; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * License Information object. |
||
| 60 | * |
||
| 61 | * @access private |
||
| 62 | * @since 1.7 |
||
| 63 | * |
||
| 64 | * @var object |
||
| 65 | */ |
||
| 66 | private $license_data; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Item shortname |
||
| 70 | * |
||
| 71 | * @access private |
||
| 72 | * @since 1.0 |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $item_shortname; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Version |
||
| 80 | * |
||
| 81 | * @access private |
||
| 82 | * @since 1.0 |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $version; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Author |
||
| 90 | * |
||
| 91 | * @access private |
||
| 92 | * @since 1.0 |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $author; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * API URL |
||
| 100 | * |
||
| 101 | * @access private |
||
| 102 | * @since 1.0 |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private $api_url = 'https://givewp.com/edd-sl-api/'; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Account URL |
||
| 110 | * |
||
| 111 | * @access private |
||
| 112 | * @since 1.7 |
||
| 113 | * |
||
| 114 | * @var null|string |
||
| 115 | */ |
||
| 116 | private $account_url = 'https://givewp.com/my-account/'; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Ccheckout URL |
||
| 120 | * |
||
| 121 | * @access private |
||
| 122 | * @since 1.7 |
||
| 123 | * |
||
| 124 | * @var null|string |
||
| 125 | */ |
||
| 126 | private $checkout_url = 'https://givewp.com/checkout/'; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Class Constructor |
||
| 130 | * |
||
| 131 | * Set up the Give License Class. |
||
| 132 | * |
||
| 133 | * @access public |
||
| 134 | * @since 1.0 |
||
| 135 | * |
||
| 136 | * @param string $_file |
||
| 137 | * @param string $_item_name |
||
| 138 | * @param string $_version |
||
| 139 | * @param string $_author |
||
| 140 | * @param string $_optname |
||
| 141 | * @param string $_api_url |
||
| 142 | * @param string $_checkout_url |
||
| 143 | * @param string $_account_url |
||
| 144 | */ |
||
| 145 | public function __construct( $_file, $_item_name, $_version, $_author, $_optname = null, $_api_url = null, $_checkout_url = null, $_account_url = null ) { |
||
|
|
|||
| 146 | |||
| 147 | $give_options = give_get_settings(); |
||
| 148 | |||
| 149 | $this->file = $_file; |
||
| 150 | $this->item_name = $_item_name; |
||
| 151 | $this->item_shortname = 'give_' . preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->item_name ) ) ); |
||
| 152 | $this->version = $_version; |
||
| 153 | $this->license = isset( $give_options[ $this->item_shortname . '_license_key' ] ) ? trim( $give_options[ $this->item_shortname . '_license_key' ] ) : ''; |
||
| 154 | $this->license_data = get_option( $this->item_shortname . '_license_active' ); |
||
| 155 | $this->author = $_author; |
||
| 156 | $this->api_url = is_null( $_api_url ) ? $this->api_url : $_api_url; |
||
| 157 | $this->checkout_url = is_null( $_checkout_url ) ? $this->checkout_url : $_checkout_url; |
||
| 158 | $this->account_url = is_null( $_account_url ) ? $this->account_url : $_account_url; |
||
| 159 | $this->auto_updater_obj = null; |
||
| 160 | |||
| 161 | // Setup hooks |
||
| 162 | $this->includes(); |
||
| 163 | $this->hooks(); |
||
| 164 | $this->auto_updater(); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Includes |
||
| 169 | * |
||
| 170 | * Include the updater class. |
||
| 171 | * |
||
| 172 | * @access private |
||
| 173 | * @since 1.0 |
||
| 174 | * |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | private function includes() { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Hooks |
||
| 186 | * |
||
| 187 | * Setup license hooks. |
||
| 188 | * |
||
| 189 | * @access private |
||
| 190 | * @since 1.0 |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | private function hooks() { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Auto Updater |
||
| 227 | * |
||
| 228 | * @access private |
||
| 229 | * @since 1.0 |
||
| 230 | * |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | public function auto_updater() { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * License Settings |
||
| 250 | * |
||
| 251 | * Add license field to settings. |
||
| 252 | * |
||
| 253 | * @access public |
||
| 254 | * @since 1.0 |
||
| 255 | * |
||
| 256 | * @param array $settings License settings. |
||
| 257 | * |
||
| 258 | * @return array License settings. |
||
| 259 | */ |
||
| 260 | public function settings( $settings ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * License Settings Content |
||
| 285 | * |
||
| 286 | * Add Some Content to the Licensing Settings. |
||
| 287 | * |
||
| 288 | * @access public |
||
| 289 | * @since 1.0 |
||
| 290 | * |
||
| 291 | * @param array $settings License settings content. |
||
| 292 | * |
||
| 293 | * @return array License settings content. |
||
| 294 | */ |
||
| 295 | public function license_settings_content( $settings ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Activate License |
||
| 311 | * |
||
| 312 | * Activate the license key. |
||
| 313 | * |
||
| 314 | * @access public |
||
| 315 | * @since 1.0 |
||
| 316 | * |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function activate_license() { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Deactivate License |
||
| 378 | * |
||
| 379 | * Deactivate the license key. |
||
| 380 | * |
||
| 381 | * @access public |
||
| 382 | * @since 1.0 |
||
| 383 | * |
||
| 384 | * @return void |
||
| 385 | */ |
||
| 386 | public function deactivate_license() { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Check if license key is valid once per week. |
||
| 425 | * |
||
| 426 | * @access public |
||
| 427 | * @since 1.7 |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | public function weekly_license_check() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Check subscription validation once per week |
||
| 459 | * |
||
| 460 | * @access public |
||
| 461 | * @since 1.7 |
||
| 462 | * |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | public function weekly_subscription_check() { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Check if license key is part of subscription or not |
||
| 492 | * |
||
| 493 | * @access private |
||
| 494 | * @since 1.7 |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | private function __single_subscription_check() { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Admin notices for errors |
||
| 535 | * |
||
| 536 | * @access public |
||
| 537 | * @since 1.0 |
||
| 538 | * |
||
| 539 | * @return void |
||
| 540 | */ |
||
| 541 | public function notices() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Check if license is valid or not. |
||
| 671 | * |
||
| 672 | * @access public |
||
| 673 | * @since 1.7 |
||
| 674 | * |
||
| 675 | * @return bool |
||
| 676 | */ |
||
| 677 | public function is_valid_license() { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Check if license is valid or not. |
||
| 687 | * |
||
| 688 | * @access private |
||
| 689 | * @since 1.7 |
||
| 690 | * |
||
| 691 | * @return bool |
||
| 692 | */ |
||
| 693 | private function __is_third_party_addon() { |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Remove license key from subscription. |
||
| 699 | * |
||
| 700 | * This function mainly uses when admin user deactivate license key, |
||
| 701 | * then we do not need subscription information for that license key. |
||
| 702 | * |
||
| 703 | * @access private |
||
| 704 | * @since 1.7 |
||
| 705 | * |
||
| 706 | * @return bool |
||
| 707 | */ |
||
| 708 | private function __remove_license_key_from_subscriptions() { |
||
| 733 | |||
| 734 | /** |
||
| 735 | * @param $plugin_file |
||
| 736 | * @param $plugin_data |
||
| 737 | * @param $status |
||
| 738 | * |
||
| 739 | * @return bool |
||
| 740 | */ |
||
| 741 | public function plugin_page_notices( $plugin_file, $plugin_data, $status ) { |
||
| 754 | |||
| 755 | |||
| 756 | /** |
||
| 757 | * Get message related to license state. |
||
| 758 | * |
||
| 759 | * @since 1.8.7 |
||
| 760 | * @access public |
||
| 761 | * @return array |
||
| 762 | */ |
||
| 763 | public function license_state_message() { |
||
| 777 | |||
| 778 | |||
| 779 | /** |
||
| 780 | * Check if admin can edit license or not, |
||
| 781 | * |
||
| 782 | * @since 1.8.9 |
||
| 783 | * @access private |
||
| 784 | */ |
||
| 785 | private function __is_user_can_edit_license(){ |
||
| 801 | |||
| 802 | |||
| 803 | /** |
||
| 804 | * Get license information. |
||
| 805 | * |
||
| 806 | * @since 1.8.9 |
||
| 807 | * @access public |
||
| 808 | * |
||
| 809 | * @param string $edd_action |
||
| 810 | * @param bool $response_in_array |
||
| 811 | * |
||
| 812 | * @return mixed |
||
| 813 | */ |
||
| 814 | public function get_license_info( $edd_action = '', $response_in_array = false ) { |
||
| 844 | } |
||
| 845 | |||
| 847 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.