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_Client 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_Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Jetpack_Sync_Client { |
||
| 8 | static $default_options_whitelist = array( |
||
|
|
|||
| 9 | 'stylesheet', |
||
| 10 | '/^theme_mods_.*$/', |
||
| 11 | 'blogname', |
||
| 12 | 'home', |
||
| 13 | 'siteurl', |
||
| 14 | 'blogdescription', |
||
| 15 | 'blog_charset', |
||
| 16 | 'permalink_structure', |
||
| 17 | 'category_base', |
||
| 18 | 'tag_base', |
||
| 19 | 'comment_moderation', |
||
| 20 | 'default_comment_status', |
||
| 21 | 'thread_comments', |
||
| 22 | 'thread_comments_depth', |
||
| 23 | 'jetpack_site_icon_url', |
||
| 24 | 'social_notifications_like', |
||
| 25 | 'page_on_front', |
||
| 26 | 'rss_use_excerpt', |
||
| 27 | 'subscription_options', |
||
| 28 | 'stb_enabled', |
||
| 29 | 'stc_enabled', |
||
| 30 | 'comment_registration', |
||
| 31 | 'require_name_email', |
||
| 32 | 'show_avatars', |
||
| 33 | 'avatar_default', |
||
| 34 | 'avatar_rating', |
||
| 35 | 'highlander_comment_form_prompt', |
||
| 36 | 'jetpack_comment_form_color_scheme', |
||
| 37 | 'stats_options', |
||
| 38 | 'gmt_offset', |
||
| 39 | 'timezone_string', |
||
| 40 | 'jetpack_sync_non_public_post_stati', |
||
| 41 | 'jetpack_options', |
||
| 42 | 'site_icon', // (int) - ID of core's Site Icon attachment ID |
||
| 43 | 'default_post_format', |
||
| 44 | 'default_category', |
||
| 45 | 'large_size_w', |
||
| 46 | 'large_size_h', |
||
| 47 | 'thumbnail_size_w', |
||
| 48 | 'thumbnail_size_h', |
||
| 49 | 'medium_size_w', |
||
| 50 | 'medium_size_h', |
||
| 51 | 'thumbnail_crop', |
||
| 52 | 'image_default_link_type', |
||
| 53 | 'site_logo', |
||
| 54 | 'sharing-options', |
||
| 55 | 'sharing-services', |
||
| 56 | 'post_count', |
||
| 57 | 'default_ping_status', |
||
| 58 | 'sticky_posts', |
||
| 59 | 'disabled_likes', |
||
| 60 | 'blog_public', |
||
| 61 | 'default_pingback_flag', |
||
| 62 | 'require_name_email', |
||
| 63 | 'close_comments_for_old_posts', |
||
| 64 | 'close_comments_days_old', |
||
| 65 | 'thread_comments', |
||
| 66 | 'thread_comments_depth', |
||
| 67 | 'page_comments', |
||
| 68 | 'comments_per_page', |
||
| 69 | 'default_comments_page', |
||
| 70 | 'comment_order', |
||
| 71 | 'comments_notify', |
||
| 72 | 'moderation_notify', |
||
| 73 | 'social_notifications_like', |
||
| 74 | 'social_notifications_reblog', |
||
| 75 | 'social_notifications_subscribe', |
||
| 76 | 'comment_whitelist', |
||
| 77 | 'comment_max_links', |
||
| 78 | 'moderation_keys', |
||
| 79 | 'blacklist_keys', |
||
| 80 | 'lang_id', |
||
| 81 | 'wga', |
||
| 82 | 'disabled_likes', |
||
| 83 | 'disabled_reblogs', |
||
| 84 | 'jetpack_comment_likes_enabled', |
||
| 85 | 'twitter_via', |
||
| 86 | 'twitter-cards-site-tag' ); |
||
| 87 | |||
| 88 | static $default_constants_whitelist = array( |
||
| 89 | 'EMPTY_TRASH_DAYS', |
||
| 90 | 'WP_POST_REVISIONS', |
||
| 91 | 'AUTOMATIC_UPDATER_DISABLED', |
||
| 92 | 'ABSPATH', |
||
| 93 | 'WP_CONTENT_DIR', |
||
| 94 | 'FS_METHOD', |
||
| 95 | 'DISALLOW_FILE_EDIT', |
||
| 96 | 'DISALLOW_FILE_MODS', |
||
| 97 | 'WP_AUTO_UPDATE_CORE', |
||
| 98 | 'WP_HTTP_BLOCK_EXTERNAL', |
||
| 99 | 'WP_ACCESSIBLE_HOSTS', |
||
| 100 | 'JETPACK__VERSION' |
||
| 101 | ); |
||
| 102 | |||
| 103 | static $default_callable_whitelist = array( |
||
| 104 | 'wp_max_upload_size' => 'wp_max_upload_size', |
||
| 105 | 'is_main_network' => array( 'Jetpack', 'is_multi_network' ), |
||
| 106 | 'is_multi_site' => 'is_multisite', |
||
| 107 | 'main_network_site' => 'network_site_url', |
||
| 108 | 'single_user_site' => array( 'Jetpack', 'is_single_user_site' ), |
||
| 109 | 'has_file_system_write_access' => array( 'Jetpack_Sync_Functions', 'file_system_write_access' ), |
||
| 110 | 'is_version_controlled' => array( 'Jetpack_Sync_Functions', 'is_version_controlled' ), |
||
| 111 | 'modules' => array( 'Jetpack_Sync_Functions', 'get_modules' ) |
||
| 112 | ); |
||
| 113 | |||
| 114 | static $default_multisite_callable_whitelist = array( |
||
| 115 | 'network_name' => array( 'Jetpack', 'network_name' ), |
||
| 116 | 'network_allow_new_registrations' => array( 'Jetpack', 'network_allow_new_registrations' ), |
||
| 117 | 'network_add_new_users' => array( 'Jetpack', 'network_add_new_users' ), |
||
| 118 | 'network_site_upload_space' => array( 'Jetpack', 'network_site_upload_space' ), |
||
| 119 | 'network_upload_file_types' => array( 'Jetpack', 'network_upload_file_types' ), |
||
| 120 | 'network_enable_administration_menus' => array( 'Jetpack', 'network_enable_administration_menus' ), |
||
| 121 | ); |
||
| 122 | |||
| 123 | // TODO: move this to server? - these are theme support values |
||
| 124 | // that should be synced as jetpack_current_theme_supports_foo option values |
||
| 125 | static $default_theme_support_whitelist = array( |
||
| 126 | 'post-thumbnails', |
||
| 127 | 'post-formats', |
||
| 128 | 'custom-header', |
||
| 129 | 'custom-background', |
||
| 130 | 'custom-logo', |
||
| 131 | 'menus', |
||
| 132 | 'automatic-feed-links', |
||
| 133 | 'editor-style', |
||
| 134 | 'widgets', |
||
| 135 | 'html5', |
||
| 136 | 'title-tag', |
||
| 137 | 'jetpack-social-menu', |
||
| 138 | 'jetpack-responsive-videos', |
||
| 139 | 'infinite-scroll', |
||
| 140 | 'site-logo', |
||
| 141 | ); |
||
| 142 | |||
| 143 | static $default_network_options_whitelist = array( 'site_name' ); |
||
| 144 | static $constants_checksum_option_name = 'jetpack_constants_sync_checksum'; |
||
| 145 | static $functions_checksum_option_name = 'jetpack_functions_sync_checksum'; |
||
| 146 | static $default_send_buffer_size = 20; |
||
| 147 | static $default_taxonomy_whitelist = array(); |
||
| 148 | |||
| 149 | private $sync_queue; |
||
| 150 | private $full_sync_client; |
||
| 151 | private $codec; |
||
| 152 | private $options_whitelist; |
||
| 153 | private $constants_whitelist; |
||
| 154 | private $meta_types = array( 'post' ); |
||
| 155 | private $callable_whitelist; |
||
| 156 | private $network_options_whitelist; |
||
| 157 | private $taxonomy_whitelist; |
||
| 158 | |||
| 159 | // singleton functions |
||
| 160 | private static $instance; |
||
| 161 | |||
| 162 | public static function getInstance() { |
||
| 169 | |||
| 170 | // this is necessary because you can't use "new" when you declare instance properties >:( |
||
| 171 | protected function __construct() { |
||
| 175 | |||
| 176 | private function init() { |
||
| 275 | |||
| 276 | // TODO: Refactor to use one set whitelist function, with one is_whitelisted. |
||
| 277 | function set_options_whitelist( $options ) { |
||
| 280 | |||
| 281 | function set_constants_whitelist( $constants ) { |
||
| 284 | |||
| 285 | function get_callable_whitelist( $functions ) { |
||
| 288 | |||
| 289 | function set_callable_whitelist( $functions ) { |
||
| 292 | |||
| 293 | function set_network_options_whitelist( $options ) { |
||
| 296 | |||
| 297 | function set_send_buffer_size( $size ) { |
||
| 300 | |||
| 301 | function set_taxonomy_whitelist( $taxonomies ) { |
||
| 304 | |||
| 305 | function is_whitelisted_option( $option ) { |
||
| 316 | |||
| 317 | function is_whitelisted_network_option( $option ) { |
||
| 320 | |||
| 321 | function set_codec( iJetpack_Sync_Codec $codec ) { |
||
| 324 | |||
| 325 | function set_full_sync_client( $full_sync_client ) { |
||
| 337 | |||
| 338 | function action_handler() { |
||
| 371 | |||
| 372 | function send_theme_info() { |
||
| 376 | |||
| 377 | function send_wp_version( $update, $meta_data ) { |
||
| 383 | |||
| 384 | function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
| 388 | |||
| 389 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 400 | |||
| 401 | function post_ids_to_posts( $args ) { |
||
| 428 | |||
| 429 | function group_metadata_by_post_id( $meta ) { |
||
| 432 | |||
| 433 | function get_post_metadata( $post_ids ) { |
||
| 437 | |||
| 438 | function comment_ids_to_comments( $args ) { |
||
| 446 | |||
| 447 | function do_sync() { |
||
| 496 | |||
| 497 | |||
| 498 | private function schedule_sync( $when ) { |
||
| 501 | |||
| 502 | function force_sync_constants() { |
||
| 506 | |||
| 507 | View Code Duplication | private function maybe_sync_constants() { |
|
| 518 | |||
| 519 | private function get_all_constants() { |
||
| 525 | |||
| 526 | private function get_constant( $constant ) { |
||
| 533 | |||
| 534 | public function force_sync_callables() { |
||
| 538 | |||
| 539 | View Code Duplication | private function maybe_sync_callables() { |
|
| 550 | |||
| 551 | private function get_all_callables() { |
||
| 557 | |||
| 558 | private function get_callable( $callable ) { |
||
| 561 | |||
| 562 | private function get_check_sum( $values ) { |
||
| 565 | |||
| 566 | function jetpack_sync_core_icon() { |
||
| 583 | |||
| 584 | function get_sync_queue() { |
||
| 587 | |||
| 588 | function reset_sync_queue() { |
||
| 591 | |||
| 592 | function set_defaults() { |
||
| 608 | |||
| 609 | function reset_data() { |
||
| 613 | } |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.