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() { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Check if license key is part of subscription or not |
||
| 495 | * |
||
| 496 | * @access private |
||
| 497 | * @since 1.7 |
||
| 498 | * |
||
| 499 | * @return void |
||
| 500 | */ |
||
| 501 | private function __single_subscription_check() { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Admin notices for errors |
||
| 534 | * |
||
| 535 | * @access public |
||
| 536 | * @since 1.0 |
||
| 537 | * |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | public function notices() { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Check if license is valid or not. |
||
| 651 | * |
||
| 652 | * @access public |
||
| 653 | * @since 1.7 |
||
| 654 | * |
||
| 655 | * @return bool |
||
| 656 | */ |
||
| 657 | public function is_valid_license() { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Check if license is valid or not. |
||
| 667 | * |
||
| 668 | * @access private |
||
| 669 | * @since 1.7 |
||
| 670 | * |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | private function __is_third_party_addon() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Remove license key from subscription. |
||
| 679 | * |
||
| 680 | * This function mainly uses when admin user deactivate license key, |
||
| 681 | * then we do not need subscription information for that license key. |
||
| 682 | * |
||
| 683 | * @access private |
||
| 684 | * @since 1.7 |
||
| 685 | * |
||
| 686 | * @return bool |
||
| 687 | */ |
||
| 688 | private function __remove_license_key_from_subscriptions() { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Remove license notices show blocker. |
||
| 716 | * |
||
| 717 | * @access private |
||
| 718 | * @since 1.7 |
||
| 719 | * |
||
| 720 | * @return void |
||
| 721 | */ |
||
| 722 | private function __remove_license_notices_show_blocker() { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Check if notice dismissed by admin user or not. |
||
| 752 | * |
||
| 753 | * @access private |
||
| 754 | * @since 1.7 |
||
| 755 | * |
||
| 756 | * @param int $notice_id Notice ID. |
||
| 757 | * |
||
| 758 | * @return bool |
||
| 759 | */ |
||
| 760 | private function __is_notice_dismissed( $notice_id ) { |
||
| 778 | |||
| 779 | |||
| 780 | /** |
||
| 781 | * @param $plugin_file |
||
| 782 | * @param $plugin_data |
||
| 783 | * @param $status |
||
| 784 | * |
||
| 785 | * @return bool |
||
| 786 | */ |
||
| 787 | public function plugin_page_notices( $plugin_file, $plugin_data, $status ) { |
||
| 800 | |||
| 801 | |||
| 802 | /** |
||
| 803 | * Get message related to license state. |
||
| 804 | * |
||
| 805 | * @since 1.8.7 |
||
| 806 | * @access public |
||
| 807 | * @return array |
||
| 808 | */ |
||
| 809 | public function license_state_message() { |
||
| 823 | |||
| 824 | |||
| 825 | /** |
||
| 826 | * Check if admin can edit license or not, |
||
| 827 | * |
||
| 828 | * @since 1.8.9 |
||
| 829 | * @access private |
||
| 830 | */ |
||
| 831 | private function __is_user_can_edit_license(){ |
||
| 847 | |||
| 848 | |||
| 849 | /** |
||
| 850 | * Get license information. |
||
| 851 | * |
||
| 852 | * @since 1.8.9 |
||
| 853 | * @access public |
||
| 854 | * |
||
| 855 | * @param string $edd_action |
||
| 856 | * |
||
| 857 | * @return mixed |
||
| 858 | */ |
||
| 859 | public function get_license_info( $edd_action = '' ) { |
||
| 889 | } |
||
| 890 | |||
| 892 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.