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 Jetpack_Sync_Functions 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 Jetpack_Sync_Functions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Jetpack_Sync_Functions { |
||
8 | |||
9 | public static function get_modules() { |
||
10 | require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' ); |
||
11 | |||
12 | return Jetpack_Admin::init()->get_modules(); |
||
13 | } |
||
14 | |||
15 | public static function get_taxonomies() { |
||
16 | global $wp_taxonomies; |
||
17 | |||
18 | return $wp_taxonomies; |
||
19 | } |
||
20 | |||
21 | public static function get_post_types() { |
||
26 | |||
27 | public static function get_post_type_features() { |
||
28 | global $_wp_post_type_features; |
||
32 | |||
33 | public static function get_hosting_provider() { |
||
34 | if ( defined( 'GD_SYSTEM_PLUGIN_DIR' ) || class_exists( '\\WPaaS\\Plugin' ) ) { |
||
35 | return 'gd-managed-wp'; |
||
36 | } |
||
37 | if ( defined( 'MM_BASE_DIR' ) ) { |
||
38 | return 'bh'; |
||
39 | } |
||
40 | if ( defined( 'IS_PRESSABLE' ) ) { |
||
41 | return 'pressable'; |
||
42 | } |
||
43 | if ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ) { |
||
44 | return 'wpe'; |
||
45 | } |
||
46 | return 'unknown'; |
||
47 | } |
||
48 | |||
49 | public static function rest_api_allowed_post_types() { |
||
53 | |||
54 | public static function rest_api_allowed_public_metadata() { |
||
58 | |||
59 | /** |
||
60 | * Finds out if a site is using a version control system. |
||
61 | * @return bool |
||
62 | **/ |
||
63 | public static function is_version_controlled() { |
||
72 | |||
73 | /** |
||
74 | * Returns true if the site has file write access false otherwise. |
||
75 | * @return bool |
||
76 | **/ |
||
77 | public static function file_system_write_access() { |
||
98 | |||
99 | public static function home_url() { |
||
102 | |||
103 | public static function site_url() { |
||
106 | |||
107 | public static function main_network_site_url() { |
||
110 | |||
111 | public static function preserve_scheme( $option, $url_function, $normalize_www = false ) { |
||
162 | |||
163 | public static function get_plugins() { |
||
171 | |||
172 | public static function wp_version() { |
||
177 | |||
178 | public static function site_icon_url() { |
||
185 | } |
||
186 |