Complex classes like Jetpack_Sync_Module_Full_Sync 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_Module_Full_Sync, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module { |
||
18 | const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
||
19 | const FULL_SYNC_TIMEOUT = 3600; |
||
20 | |||
21 | private $items_added_since_last_pause; |
||
22 | private $last_pause_time; |
||
23 | private $queue_rate_limit; |
||
24 | |||
25 | public function name() { |
||
28 | |||
29 | function init_full_sync_listeners( $callable ) { |
||
35 | |||
36 | function init_before_send() { |
||
40 | |||
41 | function start( $modules = null ) { |
||
42 | $was_already_running = $this->is_started() && ! $this->is_finished(); |
||
43 | |||
44 | // remove all evidence of previous full sync items and status |
||
45 | $this->reset_data(); |
||
46 | |||
47 | $this->enable_queue_rate_limit(); |
||
48 | |||
49 | if ( $was_already_running ) { |
||
50 | /** |
||
51 | * Fires when a full sync is cancelled. |
||
52 | * |
||
53 | * @since 4.2.0 |
||
54 | */ |
||
55 | do_action( 'jetpack_full_sync_cancelled' ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Fires when a full sync begins. This action is serialized |
||
60 | * and sent to the server so that it knows a full sync is coming. |
||
61 | * |
||
62 | * @since 4.2.0 |
||
63 | */ |
||
64 | do_action( 'jetpack_full_sync_start', $modules ); |
||
65 | $this->update_status_option( 'started', time() ); |
||
66 | |||
67 | // configure modules |
||
68 | if ( ! is_array( $modules ) ) { |
||
69 | $modules = array(); |
||
70 | } |
||
71 | |||
72 | if ( isset( $modules['users'] ) && 'initial' === $modules['users'] ) { |
||
73 | $user_module = Jetpack_Sync_Modules::get_module( 'users' ); |
||
74 | $modules['users'] = $user_module->get_initial_sync_user_config(); |
||
75 | } |
||
76 | |||
77 | // by default, all modules are fully enabled |
||
78 | if ( count( $modules ) === 0 ) { |
||
79 | $default_module_config = true; |
||
80 | } else { |
||
81 | $default_module_config = false; |
||
82 | } |
||
83 | |||
84 | // set default configuration, calculate totals, and save configuration if totals > 0 |
||
85 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
86 | $module_name = $module->name(); |
||
87 | if ( ! isset( $modules[ $module_name ] ) ) { |
||
88 | $modules[ $module_name ] = $default_module_config; |
||
89 | } |
||
90 | |||
91 | // check if this module is enabled |
||
92 | if ( ! ( $module_config = $modules[ $module_name ] ) ) { |
||
93 | continue; |
||
94 | } |
||
95 | |||
96 | $total_items = $module->estimate_full_sync_actions( $module_config ); |
||
97 | |||
98 | if ( ! is_null( $total_items ) && $total_items > 0 ) { |
||
99 | $this->update_status_option( "{$module_name}_total", $total_items ); |
||
100 | $this->update_status_option( "{$module_name}_config", $module_config ); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
105 | $module_name = $module->name(); |
||
106 | $module_config = $modules[ $module_name ]; |
||
107 | |||
108 | // check if this module is enabled |
||
109 | if ( ! $module_config ) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $items_enqueued = $module->enqueue_full_sync_actions( $module_config ); |
||
114 | |||
115 | if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) { |
||
116 | $this->update_status_option( "{$module_name}_queued", $items_enqueued ); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | $this->update_status_option( 'queue_finished', time() ); |
||
121 | |||
122 | $store = new Jetpack_Sync_WP_Replicastore(); |
||
123 | |||
124 | /** |
||
125 | * Fires when a full sync ends. This action is serialized |
||
126 | * and sent to the server with checksums so that we can confirm the |
||
127 | * sync was successful. |
||
128 | * |
||
129 | * @since 4.2.0 |
||
130 | */ |
||
131 | do_action( 'jetpack_full_sync_end', $store->checksum_all() ); |
||
132 | |||
133 | $this->disable_queue_rate_limit(); |
||
134 | |||
135 | return true; |
||
136 | } |
||
137 | |||
138 | function update_sent_progress_action( $actions ) { |
||
171 | |||
172 | public function get_action_name( $queue_item ) { |
||
178 | |||
179 | public function is_started() { |
||
182 | |||
183 | public function is_finished() { |
||
186 | |||
187 | public function get_status() { |
||
221 | |||
222 | public function clear_status() { |
||
236 | |||
237 | public function reset_data() { |
||
243 | |||
244 | private function get_status_option( $option, $default = null ) { |
||
256 | |||
257 | private function update_status_option( $name, $value ) { |
||
261 | |||
262 | private function enable_queue_rate_limit() { |
||
270 | |||
271 | private function disable_queue_rate_limit() { |
||
275 | |||
276 | public function queue_item_added() { |
||
279 | |||
280 | public function queue_items_added( $item_count ) { |
||
296 | } |
||
297 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.