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 |
||
27 | class Give_License { |
||
28 | |||
29 | /** |
||
30 | * File |
||
31 | * |
||
32 | * @access private |
||
33 | * @since 1.0 |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $file; |
||
38 | |||
39 | /** |
||
40 | * License |
||
41 | * |
||
42 | * @access private |
||
43 | * @since 1.0 |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $license; |
||
48 | |||
49 | /** |
||
50 | * Item name |
||
51 | * |
||
52 | * @access private |
||
53 | * @since 1.0 |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | private $item_name; |
||
58 | |||
59 | /** |
||
60 | * Item ID |
||
61 | * |
||
62 | * @access private |
||
63 | * @since 2.2.4 |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | private $item_id; |
||
68 | |||
69 | /** |
||
70 | * License Information object. |
||
71 | * |
||
72 | * @access private |
||
73 | * @since 1.7 |
||
74 | * |
||
75 | * @var object |
||
76 | */ |
||
77 | private $license_data; |
||
78 | |||
79 | /** |
||
80 | * Item shortname |
||
81 | * |
||
82 | * @access private |
||
83 | * @since 1.0 |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | private $item_shortname; |
||
88 | |||
89 | /** |
||
90 | * Version |
||
91 | * |
||
92 | * @access private |
||
93 | * @since 1.0 |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $version; |
||
98 | |||
99 | /** |
||
100 | * Author |
||
101 | * |
||
102 | * @access private |
||
103 | * @since 1.0 |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | private $author; |
||
108 | |||
109 | /** |
||
110 | * API URL |
||
111 | * |
||
112 | * @access private |
||
113 | * @since 1.0 |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | private $api_url = 'https://givewp.com/edd-sl-api/'; |
||
118 | |||
119 | /** |
||
120 | * array of licensed addons |
||
121 | * |
||
122 | * @since 2.1.4 |
||
123 | * @access private |
||
124 | * |
||
125 | * @var array |
||
126 | */ |
||
127 | private static $licensed_addons = array(); |
||
128 | |||
129 | /** |
||
130 | * Account URL |
||
131 | * |
||
132 | * @access private |
||
133 | * @since 1.7 |
||
134 | * |
||
135 | * @var null|string |
||
136 | */ |
||
137 | private $account_url = 'https://givewp.com/my-account/'; |
||
138 | |||
139 | /** |
||
140 | * Checkout URL |
||
141 | * |
||
142 | * @access private |
||
143 | * @since 1.7 |
||
144 | * |
||
145 | * @var null|string |
||
146 | */ |
||
147 | private $checkout_url = 'https://givewp.com/checkout/'; |
||
148 | |||
149 | /** |
||
150 | * Class Constructor |
||
151 | * |
||
152 | * Set up the Give License Class. |
||
153 | * |
||
154 | * @access public |
||
155 | * @since 1.0 |
||
156 | * |
||
157 | * @param string $_file |
||
158 | * @param string $_item_name |
||
159 | * @param string $_version |
||
160 | * @param string $_author |
||
161 | * @param string $_optname |
||
162 | * @param string $_api_url |
||
163 | * @param string $_checkout_url |
||
164 | * @param string $_account_url |
||
165 | * @param int $_item_id |
||
166 | */ |
||
167 | public function __construct( |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Get plugin shortname |
||
221 | * |
||
222 | * @since 2.1.0 |
||
223 | * @access public |
||
224 | * |
||
225 | * @param $plugin_name |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public static function get_short_name( $plugin_name ) { |
||
235 | |||
236 | /** |
||
237 | * Includes |
||
238 | * |
||
239 | * Include the updater class. |
||
240 | * |
||
241 | * @access private |
||
242 | * @since 1.0 |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | private function includes() { |
||
252 | |||
253 | /** |
||
254 | * Hooks |
||
255 | * |
||
256 | * Setup license hooks. |
||
257 | * |
||
258 | * @access private |
||
259 | * @since 1.0 |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | private function hooks() { |
||
290 | |||
291 | |||
292 | /** |
||
293 | * Auto Updater |
||
294 | * |
||
295 | * @access private |
||
296 | * @since 1.0 |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function auto_updater() { |
||
320 | |||
321 | /** |
||
322 | * License Settings |
||
323 | * |
||
324 | * Add license field to settings. |
||
325 | * |
||
326 | * @access public |
||
327 | * @since 1.0 |
||
328 | * |
||
329 | * @param array $settings License settings. |
||
330 | * |
||
331 | * @return array License settings. |
||
332 | */ |
||
333 | public function settings( $settings ) { |
||
355 | |||
356 | /** |
||
357 | * License Settings Content |
||
358 | * |
||
359 | * Add Some Content to the Licensing Settings. |
||
360 | * |
||
361 | * @access public |
||
362 | * @since 1.0 |
||
363 | * |
||
364 | * @param array $settings License settings content. |
||
365 | * |
||
366 | * @return array License settings content. |
||
367 | */ |
||
368 | public function license_settings_content( $settings ) { |
||
381 | |||
382 | /** |
||
383 | * Activate License |
||
384 | * |
||
385 | * Activate the license key. |
||
386 | * |
||
387 | * @access public |
||
388 | * @since 1.0 |
||
389 | * |
||
390 | * @return void |
||
391 | */ |
||
392 | public function activate_license() { |
||
454 | |||
455 | /** |
||
456 | * Deactivate License |
||
457 | * |
||
458 | * Deactivate the license key. |
||
459 | * |
||
460 | * @access public |
||
461 | * @since 1.0 |
||
462 | * |
||
463 | * @return void |
||
464 | */ |
||
465 | public function deactivate_license() { |
||
483 | |||
484 | /** |
||
485 | * Check if license key is valid once per week. |
||
486 | * |
||
487 | * @access public |
||
488 | * @since 1.7 |
||
489 | * |
||
490 | * @return void |
||
491 | */ |
||
492 | public function weekly_license_check() { |
||
522 | |||
523 | /** |
||
524 | * Check subscription validation once per week |
||
525 | * |
||
526 | * @access public |
||
527 | * @since 1.7 |
||
528 | * |
||
529 | * @return void |
||
530 | */ |
||
531 | public function weekly_subscription_check() { |
||
555 | |||
556 | /** |
||
557 | * Check if license key is part of subscription or not |
||
558 | * |
||
559 | * @access private |
||
560 | * @since 1.7 |
||
561 | * |
||
562 | * @return void |
||
563 | */ |
||
564 | private function __single_subscription_check() { |
||
598 | |||
599 | /** |
||
600 | * Admin notices for errors |
||
601 | * |
||
602 | * @access public |
||
603 | * @since 1.0 |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function notices() { |
||
752 | |||
753 | /** |
||
754 | * Check if license is valid or not. |
||
755 | * |
||
756 | * @since 1.7 |
||
757 | * @access public |
||
758 | * |
||
759 | * @param null|object $licence_data |
||
760 | * |
||
761 | * @return bool |
||
762 | */ |
||
763 | public function is_valid_license( $licence_data = null ) { |
||
772 | |||
773 | |||
774 | /** |
||
775 | * Check if license is license object of no. |
||
776 | * |
||
777 | * @since 1.7 |
||
778 | * @access public |
||
779 | * |
||
780 | * @param null|object $licence_data |
||
781 | * |
||
782 | * @return bool |
||
783 | */ |
||
784 | public function is_license( $licence_data = null ) { |
||
793 | |||
794 | /** |
||
795 | * Check if license is valid or not. |
||
796 | * |
||
797 | * @access private |
||
798 | * @since 1.7 |
||
799 | * |
||
800 | * @return bool |
||
801 | */ |
||
802 | private function __is_third_party_addon() { |
||
805 | |||
806 | /** |
||
807 | * Remove license key from subscription. |
||
808 | * |
||
809 | * This function mainly uses when admin user deactivate license key, |
||
810 | * then we do not need subscription information for that license key. |
||
811 | * |
||
812 | * @access private |
||
813 | * @since 1.7 |
||
814 | * |
||
815 | * @return bool |
||
816 | */ |
||
817 | private function __remove_license_key_from_subscriptions() { |
||
842 | |||
843 | /** |
||
844 | * Display plugin page licenses status notices. |
||
845 | * |
||
846 | * @param $plugin_file |
||
847 | * @param $plugin_data |
||
848 | * @param $status |
||
849 | * |
||
850 | * @return bool |
||
851 | */ |
||
852 | public function plugin_page_notices( $plugin_file, $plugin_data, $status ) { |
||
865 | |||
866 | |||
867 | /** |
||
868 | * Get message related to license state. |
||
869 | * |
||
870 | * @since 1.8.7 |
||
871 | * @access public |
||
872 | * @return array |
||
873 | */ |
||
874 | public function license_state_message() { |
||
888 | |||
889 | |||
890 | /** |
||
891 | * Check if admin can edit license or not. |
||
892 | * |
||
893 | * @since 1.8.9 |
||
894 | * @access private |
||
895 | */ |
||
896 | private function __is_user_can_edit_license() { |
||
917 | |||
918 | |||
919 | /** |
||
920 | * Get license information. |
||
921 | * |
||
922 | * @since 1.8.9 |
||
923 | * @access public |
||
924 | * |
||
925 | * @param string $edd_action |
||
926 | * @param bool $response_in_array |
||
927 | * |
||
928 | * @return mixed |
||
929 | */ |
||
930 | public function get_license_info( $edd_action = '', $response_in_array = false ) { |
||
961 | |||
962 | |||
963 | /** |
||
964 | * Unset license |
||
965 | * |
||
966 | * @since 1.8.14 |
||
967 | * @access private |
||
968 | */ |
||
969 | private function unset_license() { |
||
982 | |||
983 | |||
984 | /** |
||
985 | * Check if deactivating any license key or not. |
||
986 | * |
||
987 | * @since 1.8.17 |
||
988 | * @access private |
||
989 | * |
||
990 | * @return bool |
||
991 | */ |
||
992 | private function is_deactivating_license() { |
||
1004 | |||
1005 | /** |
||
1006 | * Return licensed addons info |
||
1007 | * |
||
1008 | * Note: note only for internal logic |
||
1009 | * |
||
1010 | * @since 2.1.4 |
||
1011 | * |
||
1012 | * @return array |
||
1013 | */ |
||
1014 | static function get_licensed_addons() { |
||
1017 | |||
1018 | } |
||
1019 | |||
1021 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.