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 | * Plugin directory name |
||
111 | * |
||
112 | * @access private |
||
113 | * @since 2.5.0 |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | private $plugin_dirname; |
||
118 | |||
119 | /** |
||
120 | * Website URL |
||
121 | * |
||
122 | * @access private |
||
123 | * @since 1.0 |
||
124 | * |
||
125 | * @var string |
||
126 | */ |
||
127 | private static $site_url = 'https://givewp.com/'; |
||
128 | |||
129 | /** |
||
130 | * API URL |
||
131 | * |
||
132 | * @access private |
||
133 | * @since 1.0 |
||
134 | * |
||
135 | * @var string |
||
136 | */ |
||
137 | private $api_url = 'https://givewp.com/edd-sl-api/'; |
||
138 | |||
139 | /** |
||
140 | * array of licensed addons |
||
141 | * |
||
142 | * @since 2.1.4 |
||
143 | * @access private |
||
144 | * |
||
145 | * @var array |
||
146 | */ |
||
147 | private static $licensed_addons = array(); |
||
148 | |||
149 | /** |
||
150 | * Account URL |
||
151 | * |
||
152 | * @access private |
||
153 | * @since 1.7 |
||
154 | * |
||
155 | * @var null|string |
||
156 | */ |
||
157 | private static $account_url = 'https://givewp.com/my-account/'; |
||
158 | |||
159 | /** |
||
160 | * Downloads URL |
||
161 | * |
||
162 | * @access private |
||
163 | * @since 2.5.0 |
||
164 | * |
||
165 | * @var null|string |
||
166 | */ |
||
167 | private static $downloads_url = 'https://givewp.com/my-downloads/'; |
||
168 | |||
169 | /** |
||
170 | * Checkout URL |
||
171 | * |
||
172 | * @access private |
||
173 | * @since 1.7 |
||
174 | * |
||
175 | * @var null|string |
||
176 | */ |
||
177 | private static $checkout_url = 'https://givewp.com/checkout/'; |
||
178 | |||
179 | /** |
||
180 | * Class Constructor |
||
181 | * |
||
182 | * Set up the Give License Class. |
||
183 | * |
||
184 | * @access public |
||
185 | * |
||
186 | * @param string $_file |
||
187 | * @param string $_item_name |
||
188 | * @param string $_version |
||
189 | * @param string $_author |
||
190 | * @param string $_optname |
||
191 | * @param string $_api_url |
||
192 | * @param string $_checkout_url |
||
193 | * @param string $_account_url |
||
194 | * @param int $_item_id |
||
195 | * |
||
196 | * @since 1.0 |
||
197 | */ |
||
198 | public function __construct( |
||
235 | |||
236 | |||
237 | /** |
||
238 | * Get plugin shortname |
||
239 | * |
||
240 | * @param $plugin_name |
||
241 | * |
||
242 | * @return string |
||
243 | * @since 2.1.0 |
||
244 | * @access public |
||
245 | */ |
||
246 | public static function get_short_name( $plugin_name ) { |
||
252 | |||
253 | /** |
||
254 | * Activate License |
||
255 | * |
||
256 | * Activate the license key. |
||
257 | * |
||
258 | * @access public |
||
259 | * @return void |
||
260 | * @since 1.0 |
||
261 | */ |
||
262 | public function activate_license() { |
||
264 | |||
265 | /** |
||
266 | * Deactivate License |
||
267 | * |
||
268 | * Deactivate the license key. |
||
269 | * |
||
270 | * @access public |
||
271 | * @return void |
||
272 | * @since 1.0 |
||
273 | */ |
||
274 | public function deactivate_license() { |
||
276 | |||
277 | |||
278 | /** |
||
279 | * Get license information. |
||
280 | * |
||
281 | * @param string $edd_action |
||
282 | * @param bool $response_in_array |
||
283 | * |
||
284 | * @return mixed |
||
285 | * @deprecated 2.5.0 Use self::request_license_api instead. |
||
286 | * |
||
287 | * @since 1.8.9 |
||
288 | * @access public |
||
289 | */ |
||
290 | public function get_license_info( $edd_action = '', $response_in_array = false ) { |
||
307 | |||
308 | /** |
||
309 | * Return licensed addons info |
||
310 | * |
||
311 | * Note: note only for internal logic |
||
312 | * |
||
313 | * @return array |
||
314 | * @since 2.1.4 |
||
315 | */ |
||
316 | static function get_licensed_addons() { |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Check if license key attached to subscription |
||
323 | * |
||
324 | * @param string $license_key |
||
325 | * |
||
326 | * @return array |
||
327 | * @since 2.5.0 |
||
328 | */ |
||
329 | static function is_subscription( $license_key = '' ) { |
||
345 | |||
346 | /** |
||
347 | * Get license information. |
||
348 | * |
||
349 | * @param array $api_params |
||
350 | * @param bool $response_in_array |
||
351 | * |
||
352 | * @return array|WP_Error |
||
353 | * @since 1.8.9 |
||
354 | * @access public |
||
355 | */ |
||
356 | public static function request_license_api( $api_params = array(), $response_in_array = false ) { |
||
392 | |||
393 | /** |
||
394 | * Get license by plugin dirname |
||
395 | * |
||
396 | * @param string $plugin_dirname |
||
397 | * |
||
398 | * @return array |
||
399 | * |
||
400 | * @since 2.5.0 |
||
401 | * @access public |
||
402 | */ |
||
403 | public static function get_license_by_plugin_dirname( $plugin_dirname ) { |
||
429 | |||
430 | |||
431 | /** |
||
432 | * Get checkout url |
||
433 | * |
||
434 | * @return string|null |
||
435 | * @since 2.5.0 |
||
436 | */ |
||
437 | public static function get_checkout_url() { |
||
440 | |||
441 | /** |
||
442 | * Get account url |
||
443 | * |
||
444 | * @return string|null |
||
445 | * @since 2.5.0 |
||
446 | */ |
||
447 | public static function get_account_url() { |
||
450 | |||
451 | /** |
||
452 | * Get downloads url |
||
453 | * |
||
454 | * @return string|null |
||
455 | * @since 2.5.0 |
||
456 | */ |
||
457 | public static function get_downloads_url() { |
||
460 | |||
461 | /** |
||
462 | * Get account url |
||
463 | * |
||
464 | * @return string|null |
||
465 | * @since 2.5.0 |
||
466 | */ |
||
467 | public static function get_website_url() { |
||
470 | |||
471 | |||
472 | /** |
||
473 | * Get plugin information by id. |
||
474 | * Note: only for internal use |
||
475 | * |
||
476 | * @param string $plugin_slug |
||
477 | * |
||
478 | * @return array |
||
479 | * @since 2.5.0 |
||
480 | */ |
||
481 | public static function get_plugin_by_slug( $plugin_slug ) { |
||
488 | |||
489 | /** |
||
490 | * Get plugin information by id. |
||
491 | * Note: only for internal use |
||
492 | * |
||
493 | * @param string $plugin_slug |
||
494 | * |
||
495 | * @return string |
||
496 | * @since 2.5.0 |
||
497 | */ |
||
498 | public static function build_plugin_name_from_slug( $plugin_slug ) { |
||
503 | |||
504 | /** |
||
505 | * Render license section |
||
506 | * |
||
507 | * @return string |
||
508 | * @since 2.5.0 |
||
509 | */ |
||
510 | public static function render_licenses_list() { |
||
577 | |||
578 | /** |
||
579 | * Get add-on item html |
||
580 | * Note: only for internal use |
||
581 | * |
||
582 | * @param $plugin |
||
583 | * |
||
584 | * @return string |
||
585 | * @since 2.5.0 |
||
586 | */ |
||
587 | public static function html_by_plugin( $plugin ) { |
||
631 | |||
632 | /** |
||
633 | * Get add-on item html |
||
634 | * Note: only for internal use |
||
635 | * |
||
636 | * @param array $license |
||
637 | * |
||
638 | * @return string |
||
639 | * @since 2.5.0 |
||
640 | */ |
||
641 | private static function html_by_license( $license ) { |
||
691 | |||
692 | |||
693 | /** |
||
694 | * license row html |
||
695 | * |
||
696 | * @param array $license |
||
697 | * @param array $plugin |
||
698 | * |
||
699 | * @return string |
||
700 | * @since 2.5.0 |
||
701 | */ |
||
702 | private static function html_license_row( $license, $plugin = array() ) { |
||
854 | |||
855 | |||
856 | /** |
||
857 | * Plugin row html |
||
858 | * |
||
859 | * @param array $plugin |
||
860 | * |
||
861 | * @return string |
||
862 | * @since 2.5.0 |
||
863 | */ |
||
864 | public static function html_plugin_row( $plugin ) { |
||
926 | |||
927 | |||
928 | /** |
||
929 | * Get refresh license status |
||
930 | * |
||
931 | * @return mixed|void |
||
932 | * @since 2.5.0 |
||
933 | */ |
||
934 | public static function refresh_license_status() { |
||
944 | } |
||
945 | |||
947 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.