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 Utils 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 Utils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Utils { |
||
14 | |||
15 | /** |
||
16 | * WP Options string: jetpack_beta_active |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected static $option = 'jetpack_beta_active'; |
||
21 | |||
22 | /** |
||
23 | * WP Options string: jetpack_beta_dev_currently_installed |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected static $option_dev_installed = 'jetpack_beta_dev_currently_installed'; |
||
28 | |||
29 | /** |
||
30 | * WP Options string: jp_beta_autoupdate |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected static $option_autoupdate = 'jp_beta_autoupdate'; |
||
35 | |||
36 | /** |
||
37 | * WP Options string: jp_beta_email_notifications |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected static $option_email_notif = 'jp_beta_email_notifications'; |
||
42 | |||
43 | /** |
||
44 | * Checks if passed plugin matches JP or JP Dev paths. |
||
45 | * |
||
46 | * @param string $plugin - A plugin path. |
||
47 | */ |
||
48 | public static function is_jetpack_plugin( $plugin ) { |
||
51 | |||
52 | /** |
||
53 | * Returns active Jetpack plugin file partial path string (jetpack/jetpack.php|jetpack-dev/jetpack.php). |
||
54 | */ |
||
55 | public static function get_plugin_file() { |
||
58 | |||
59 | /** |
||
60 | * Returns active plugin slug string (jetpack|jetpack-dev). |
||
61 | */ |
||
62 | View Code Duplication | public static function get_plugin_slug() { |
|
69 | |||
70 | /** |
||
71 | * Builds URL to the admin area for the current site and specified query param. |
||
72 | * |
||
73 | * @param string $query - Path relative to the admin URL. |
||
74 | */ |
||
75 | public static function admin_url( $query = '?page=jetpack-beta' ) { |
||
80 | |||
81 | /** |
||
82 | * Determine if JP dev version should be updated. |
||
83 | */ |
||
84 | public static function should_update_dev_version() { |
||
87 | |||
88 | /** |
||
89 | * Build plugin update data response for dev plugin. |
||
90 | */ |
||
91 | public static function get_jepack_dev_update_response() { |
||
101 | |||
102 | /** |
||
103 | * Build plugin update data response for JP dev master. |
||
104 | */ |
||
105 | public static function get_jepack_dev_master_update_response() { |
||
114 | |||
115 | /** |
||
116 | * Get the active JP or JP Dev plugin version. |
||
117 | * |
||
118 | * @param bool $is_dev_version - If dev plugin version is being queried. |
||
119 | * |
||
120 | * @return string|0 Plugin version. |
||
|
|||
121 | */ |
||
122 | public static function get_jetpack_plugin_version( $is_dev_version = false ) { |
||
131 | |||
132 | /** |
||
133 | * Get WP Option: jetpack_beta_active |
||
134 | */ |
||
135 | public static function get_option() { |
||
138 | |||
139 | /** |
||
140 | * Get WP Option: jetpack_beta_dev_currently_installed |
||
141 | */ |
||
142 | public static function get_dev_installed() { |
||
145 | |||
146 | /** |
||
147 | * Get active Jetpack branch/section. |
||
148 | */ |
||
149 | public static function get_branch_and_section() { |
||
164 | |||
165 | /** |
||
166 | * Check if Jetpack version is 'stable' version. |
||
167 | */ |
||
168 | View Code Duplication | public static function is_on_stable() { |
|
175 | |||
176 | /** |
||
177 | * Check if Jetpack active version is a tag version. |
||
178 | */ |
||
179 | public static function is_on_tag() { |
||
186 | |||
187 | /** |
||
188 | * Get active Jetpack Dev branch/section. |
||
189 | */ |
||
190 | public static function get_branch_and_section_dev() { |
||
200 | |||
201 | /** |
||
202 | * Massage JP plugin version string. |
||
203 | * |
||
204 | * @param bool $is_dev_version - If JP Dev version is being queried. |
||
205 | */ |
||
206 | public static function get_jetpack_plugin_pretty_version( $is_dev_version = false ) { |
||
244 | |||
245 | /** |
||
246 | * Fetch latest Jetpack version. |
||
247 | * |
||
248 | * @param bool $is_dev_version - If JP Dev version is being queried. |
||
249 | */ |
||
250 | public static function get_new_jetpack_version( $is_dev_version = false ) { |
||
274 | |||
275 | /** |
||
276 | * Get JP Dev plugin repo URL. |
||
277 | */ |
||
278 | public static function get_url_dev() { |
||
282 | |||
283 | /** |
||
284 | * Get JP plugin repo URL. |
||
285 | * |
||
286 | * @param string $branch - Branch. |
||
287 | * @param string $section - Section. |
||
288 | */ |
||
289 | public static function get_url( $branch = null, $section = null ) { |
||
310 | |||
311 | /** |
||
312 | * Get install URL for JP dev. |
||
313 | */ |
||
314 | public static function get_install_url_dev() { |
||
318 | |||
319 | /** |
||
320 | * Get install URL for JP. |
||
321 | * |
||
322 | * @param string $branch - Branch. |
||
323 | * @param string $section - Section. |
||
324 | */ |
||
325 | public static function get_install_url( $branch = null, $section = null ) { |
||
361 | |||
362 | /** |
||
363 | * Get stable JP version plugin data. |
||
364 | */ |
||
365 | public static function get_jetpack_plugin_info_stable() { |
||
368 | |||
369 | /** |
||
370 | * Get dev JP version plugin data. |
||
371 | */ |
||
372 | public static function get_jetpack_plugin_info_dev() { |
||
375 | |||
376 | /** |
||
377 | * Get JP plugin data. |
||
378 | * |
||
379 | * @param mixed $plugin_file - JP or JP Dev plugin path. |
||
380 | */ |
||
381 | public static function get_jetpack_plugin_info( $plugin_file = null ) { |
||
398 | |||
399 | /** |
||
400 | * Fetch the Jetpack beta manifest. |
||
401 | * |
||
402 | * @param bool $force_refresh - Whether to bypass cached response. |
||
403 | */ |
||
404 | public static function get_beta_manifest( $force_refresh = false ) { |
||
407 | |||
408 | /** |
||
409 | * Fetch WordPress.org Jetpack plugin info. |
||
410 | */ |
||
411 | public static function get_org_data() { |
||
414 | |||
415 | /** |
||
416 | * Helper function used to fetch remote data from WordPress.org, GitHub, and betadownload.jetpack.me |
||
417 | * |
||
418 | * @param string $url - Url being fetched. |
||
419 | * @param string $transient - Transient name (manifest|org_data|github_commits_). |
||
420 | * @param bool $bypass - Whether to bypass cached response. |
||
421 | */ |
||
422 | public static function get_remote_data( $url, $transient, $bypass = false ) { |
||
440 | |||
441 | /** |
||
442 | * Delete set transients when plugin is deactivated. |
||
443 | */ |
||
444 | public static function delete_all_transiants() { |
||
453 | |||
454 | /** |
||
455 | * Install & activate JP for the given branch/section. |
||
456 | * |
||
457 | * @param string $branch - Branch. |
||
458 | * @param string $section - Section. |
||
459 | */ |
||
460 | public static function install_and_activate( $branch, $section ) { |
||
505 | |||
506 | /** |
||
507 | * Update to the latest version. |
||
508 | * |
||
509 | * @param string $branch - Branch. |
||
510 | * @param string $section - Section. |
||
511 | */ |
||
512 | public static function update_plugin( $branch, $section ) { |
||
523 | |||
524 | /** |
||
525 | * Helper function to update installed version option. |
||
526 | * |
||
527 | * @param string $branch - Branch. |
||
528 | * @param string $section - Section. |
||
529 | */ |
||
530 | public static function update_option( $branch, $section ) { |
||
536 | |||
537 | /** |
||
538 | * Return manifest info for specififed branch/section. |
||
539 | * |
||
540 | * @param string $branch - Branch. |
||
541 | * @param string $section - Section. |
||
542 | */ |
||
543 | public static function get_manifest_data( $branch, $section ) { |
||
567 | |||
568 | /** |
||
569 | * Install specified plugin version. |
||
570 | * |
||
571 | * @param string $url - Url for plugin version. |
||
572 | * @param string $plugin_folder - Path JP or JP Dev plugin folder. |
||
573 | * @param string $section - Section. |
||
574 | */ |
||
575 | public static function proceed_to_install_and_activate( $url, $plugin_folder, $section ) { |
||
584 | |||
585 | /** |
||
586 | * Download plugin files. |
||
587 | * |
||
588 | * @param string $url - Url for plugin version. |
||
589 | * @param string $plugin_folder - Path JP or JP Dev plugin folder. |
||
590 | * @param string $section - Section. |
||
591 | */ |
||
592 | public static function proceed_to_install( $url, $plugin_folder, $section ) { |
||
621 | |||
622 | /** |
||
623 | * Check if plugin is network activated. |
||
624 | */ |
||
625 | public static function is_network_active() { |
||
640 | |||
641 | /** |
||
642 | * Swap plugin files. |
||
643 | * |
||
644 | * @param string $current_plugin - Current plugin path. |
||
645 | * @param string $replace_with_plugin - Plugin path to replace with. |
||
646 | * @param bool $force_activate - Whether to force activate plguin. |
||
647 | */ |
||
648 | public static function replace_active_plugin( $current_plugin, $replace_with_plugin = null, $force_activate = false ) { |
||
679 | |||
680 | /** |
||
681 | * Check if `stable` should be updated. |
||
682 | * |
||
683 | * @return bool |
||
684 | */ |
||
685 | public static function should_update_stable_version() { |
||
717 | |||
718 | /** |
||
719 | * Here we are checking if the DEV branch that we are currenly on is not something that is available in the manifest |
||
720 | * Meaning that the DEV branch was merged into master and so we need to update it. |
||
721 | * |
||
722 | * @return bool |
||
723 | */ |
||
724 | public static function should_update_dev_to_master() { |
||
733 | |||
734 | /** |
||
735 | * Get WP Option: jp_beta_autoupdate |
||
736 | */ |
||
737 | public static function is_set_to_autoupdate() { |
||
740 | |||
741 | /** |
||
742 | * Get WP Option: jp_beta_email_notifications |
||
743 | */ |
||
744 | public static function is_set_to_email_notifications() { |
||
747 | |||
748 | /** |
||
749 | * Get "What changed" info for display. |
||
750 | * |
||
751 | * @return string|false |
||
752 | */ |
||
753 | public static function what_changed() { |
||
789 | |||
790 | /** |
||
791 | * Get version commit if available. |
||
792 | * |
||
793 | * @return string|false |
||
794 | */ |
||
795 | public static function get_version_commit() { |
||
802 | |||
803 | /** |
||
804 | * Fetch commit data from GitHub. |
||
805 | * |
||
806 | * @param string $commit - The commit to fetch. |
||
807 | */ |
||
808 | public static function get_commit_data_from_github( $commit ) { |
||
811 | |||
812 | /** |
||
813 | * Checks if a dir is empty. |
||
814 | * |
||
815 | * @param [type] $dir The absolute directory path to check. |
||
816 | * @return boolean |
||
817 | */ |
||
818 | public static function is_dir_empty( $dir ) { |
||
821 | |||
822 | /** |
||
823 | * Clears the autoloader transient. |
||
824 | */ |
||
825 | public static function clear_autoloader_plugin_cache() { |
||
828 | } |
||
829 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.