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_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 | public function name() { |
||
| 24 | |||
| 25 | function init_full_sync_listeners( $callable ) { |
||
| 26 | // synthetic actions for full sync |
||
| 27 | add_action( 'jetpack_full_sync_start', $callable ); |
||
| 28 | add_action( 'jetpack_full_sync_end', $callable ); |
||
| 29 | add_action( 'jetpack_full_sync_cancelled', $callable ); |
||
| 30 | } |
||
| 31 | |||
| 32 | function init_before_send() { |
||
| 36 | |||
| 37 | function start( $module_configs = null ) { |
||
| 38 | $was_already_running = $this->is_started() && ! $this->is_finished(); |
||
| 39 | |||
| 40 | // remove all evidence of previous full sync items and status |
||
| 41 | $this->reset_data(); |
||
| 42 | |||
| 43 | if ( $was_already_running ) { |
||
| 44 | /** |
||
| 45 | * Fires when a full sync is cancelled. |
||
| 46 | * |
||
| 47 | * @since 4.2.0 |
||
| 48 | */ |
||
| 49 | do_action( 'jetpack_full_sync_cancelled' ); |
||
| 50 | } |
||
| 51 | |||
| 52 | $this->update_status_option( 'started', time() ); |
||
| 53 | $this->update_status_option( 'params', $module_configs ); |
||
| 54 | |||
| 55 | $enqueue_status = array(); |
||
| 56 | $full_sync_config = array(); |
||
| 57 | |||
| 58 | // default value is full sync |
||
| 59 | if ( ! is_array( $module_configs ) ) { |
||
| 60 | $module_configs = array(); |
||
| 61 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 62 | $module_configs[ $module->name() ] = true; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // set default configuration, calculate totals, and save configuration if totals > 0 |
||
| 67 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 68 | $module_name = $module->name(); |
||
| 69 | $module_config = isset( $module_configs[ $module_name ] ) ? $module_configs[ $module_name ] : false; |
||
| 70 | |||
| 71 | if ( ! $module_config ) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ( 'users' === $module_name && 'initial' === $module_config ) { |
||
| 76 | $module_config = $module->get_initial_sync_user_config(); |
||
| 77 | } |
||
| 78 | |||
| 79 | $enqueue_status[ $module_name ] = false; |
||
| 80 | |||
| 81 | $total_items = $module->estimate_full_sync_actions( $module_config ); |
||
| 82 | |||
| 83 | // if there's information to process, configure this module |
||
| 84 | if ( ! is_null( $total_items ) && $total_items > 0 ) { |
||
| 85 | $full_sync_config[ $module_name ] = $module_config; |
||
| 86 | $enqueue_status[ $module_name ] = array( |
||
| 87 | $total_items, // total |
||
| 88 | 0, // queued |
||
| 89 | false, // current state |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->set_config( $full_sync_config ); |
||
| 95 | $this->set_enqueue_status( $enqueue_status ); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Fires when a full sync begins. This action is serialized |
||
| 99 | * and sent to the server so that it knows a full sync is coming. |
||
| 100 | * |
||
| 101 | * @since 4.2.0 |
||
| 102 | */ |
||
| 103 | do_action( 'jetpack_full_sync_start', $full_sync_config ); |
||
| 104 | |||
| 105 | $this->continue_enqueuing( $full_sync_config, $enqueue_status ); |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | function continue_enqueuing( $configs = null, $enqueue_status = null ) { |
||
| 111 | if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) { |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | |||
| 115 | // if full sync queue is full, don't enqueue more items |
||
| 116 | $max_queue_size_full_sync = Jetpack_Sync_Settings::get_setting( 'max_queue_size_full_sync' ); |
||
| 117 | $full_sync_queue = new Jetpack_Sync_Queue( 'full_sync' ); |
||
| 118 | |||
| 119 | $available_queue_slots = $max_queue_size_full_sync - $full_sync_queue->size(); |
||
| 120 | |||
| 121 | if ( $available_queue_slots <= 0 ) { |
||
| 122 | return; |
||
| 123 | } else { |
||
| 124 | $remaining_items_to_enqueue = min( Jetpack_Sync_Settings::get_setting( 'max_enqueue_full_sync' ), $available_queue_slots ); |
||
| 125 | } |
||
| 126 | |||
| 127 | if ( ! $configs ) { |
||
| 128 | $configs = $this->get_config(); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( ! $enqueue_status ) { |
||
| 132 | $enqueue_status = $this->get_enqueue_status(); |
||
| 133 | } |
||
| 134 | |||
| 135 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 136 | $module_name = $module->name(); |
||
| 137 | |||
| 138 | // skip module if not configured for this sync or module is done |
||
| 139 | if ( ! isset( $configs[ $module_name ] ) |
||
| 140 | || // no module config |
||
| 141 | ! $configs[ $module_name ] |
||
| 142 | || // no enqueue status |
||
| 143 | ! $enqueue_status[ $module_name ] |
||
| 144 | || // finished enqueuing this module |
||
| 145 | true === $enqueue_status[ $module_name ][ 2 ] ) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $configs[ $module_name ], $remaining_items_to_enqueue, $enqueue_status[ $module_name ][ 2 ] ); |
||
| 150 | |||
| 151 | $enqueue_status[ $module_name ][ 2 ] = $next_enqueue_state; |
||
| 152 | |||
| 153 | // if items were processed, subtract them from the limit |
||
| 154 | if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) { |
||
| 155 | $enqueue_status[ $module_name ][ 1 ] += $items_enqueued; |
||
| 156 | $remaining_items_to_enqueue -= $items_enqueued; |
||
| 157 | } |
||
| 158 | |||
| 159 | // stop processing if we've reached our limit of items to enqueue |
||
| 160 | if ( 0 >= $remaining_items_to_enqueue ) { |
||
| 161 | $this->set_enqueue_status( $enqueue_status ); |
||
| 162 | return; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $this->set_enqueue_status( $enqueue_status ); |
||
| 167 | |||
| 168 | // setting autoload to true means that it's faster to check whether we should continue enqueuing |
||
| 169 | $this->update_status_option( 'queue_finished', time(), true ); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Fires when a full sync ends. This action is serialized |
||
| 173 | * and sent to the server. |
||
| 174 | * |
||
| 175 | * @since 4.2.0 |
||
| 176 | */ |
||
| 177 | do_action( 'jetpack_full_sync_end', '' ); |
||
| 178 | } |
||
| 179 | |||
| 180 | function update_sent_progress_action( $actions ) { |
||
| 181 | |||
| 182 | // quick way to map to first items with an array of arrays |
||
| 183 | $actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) ); |
||
| 184 | |||
| 185 | if ( ! $this->is_started() || $this->is_finished() ) { |
||
| 186 | return; |
||
| 187 | } |
||
| 188 | |||
| 189 | if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) { |
||
| 190 | $this->update_status_option( 'send_started', time() ); |
||
| 191 | } |
||
| 192 | |||
| 193 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 194 | $module_actions = $module->get_full_sync_actions(); |
||
| 195 | $status_option_name = "{$module->name()}_sent"; |
||
| 196 | $items_sent = $this->get_status_option( $status_option_name, 0 ); |
||
| 197 | |||
| 198 | foreach ( $module_actions as $module_action ) { |
||
| 199 | if ( isset( $actions_with_counts[ $module_action ] ) ) { |
||
| 200 | $items_sent += $actions_with_counts[ $module_action ]; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | if ( $items_sent > 0 ) { |
||
| 205 | $this->update_status_option( $status_option_name, $items_sent ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) { |
||
| 210 | $this->update_status_option( 'finished', time() ); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | public function get_action_name( $queue_item ) { |
||
| 215 | if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) { |
||
| 216 | return $queue_item[0]; |
||
| 217 | } |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | public function is_started() { |
||
| 222 | return !! $this->get_status_option( 'started' ); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function is_finished() { |
||
| 226 | return !! $this->get_status_option( 'finished' ); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function get_status() { |
||
| 230 | $status = array( |
||
| 231 | 'started' => $this->get_status_option( 'started' ), |
||
| 232 | 'queue_finished' => $this->get_status_option( 'queue_finished' ), |
||
| 233 | 'send_started' => $this->get_status_option( 'send_started' ), |
||
| 234 | 'finished' => $this->get_status_option( 'finished' ), |
||
| 235 | 'sent' => array(), |
||
| 236 | 'queue' => array(), |
||
| 237 | 'config' => $this->get_status_option( 'params' ), |
||
| 238 | 'total' => array(), |
||
| 239 | ); |
||
| 240 | |||
| 241 | $enqueue_status = $this->get_enqueue_status(); |
||
| 242 | $module_config = $this->get_config(); |
||
|
|
|||
| 243 | |||
| 244 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 245 | $name = $module->name(); |
||
| 246 | |||
| 247 | if ( ! isset( $enqueue_status[ $name ] ) ) { |
||
| 248 | continue; |
||
| 249 | } |
||
| 250 | |||
| 251 | list( $total, $queued, $state ) = $enqueue_status[ $name ]; |
||
| 252 | |||
| 253 | if ( $total ) { |
||
| 254 | $status[ 'total' ][ $name ] = $total; |
||
| 255 | } |
||
| 256 | |||
| 257 | if ( $queued ) { |
||
| 258 | $status[ 'queue' ][ $name ] = $queued; |
||
| 259 | } |
||
| 260 | |||
| 261 | if ( $sent = $this->get_status_option( "{$name}_sent" ) ) { |
||
| 262 | $status[ 'sent' ][ $name ] = $sent; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return $status; |
||
| 267 | } |
||
| 268 | |||
| 269 | public function clear_status() { |
||
| 270 | $prefix = self::STATUS_OPTION_PREFIX; |
||
| 271 | Jetpack_Sync_Options::delete_option( "{$prefix}_started" ); |
||
| 272 | Jetpack_Sync_Options::delete_option( "{$prefix}_params" ); |
||
| 273 | Jetpack_Sync_Options::delete_option( "{$prefix}_queue_finished" ); |
||
| 274 | Jetpack_Sync_Options::delete_option( "{$prefix}_send_started" ); |
||
| 275 | Jetpack_Sync_Options::delete_option( "{$prefix}_finished" ); |
||
| 276 | |||
| 277 | foreach ( Jetpack_Sync_Modules::get_modules() as $module ) { |
||
| 278 | Jetpack_Sync_Options::delete_option( "{$prefix}_{$module->name()}_sent" ); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | public function reset_data() { |
||
| 283 | $this->clear_status(); |
||
| 284 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
||
| 285 | $listener = Jetpack_Sync_Listener::get_instance(); |
||
| 286 | $listener->get_full_sync_queue()->reset(); |
||
| 287 | } |
||
| 288 | |||
| 289 | private function get_status_option( $name, $default = null ) { |
||
| 290 | $value = Jetpack_Sync_Options::get_option( self::STATUS_OPTION_PREFIX . "_$name", $default ); |
||
| 291 | |||
| 292 | return is_numeric( $value ) ? intval( $value ) : $value; |
||
| 293 | } |
||
| 294 | |||
| 295 | private function update_status_option( $name, $value, $autoload = false ) { |
||
| 296 | Jetpack_Sync_Options::update_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload ); |
||
| 297 | } |
||
| 298 | |||
| 299 | private function set_enqueue_status( $new_status ) { |
||
| 302 | |||
| 303 | private function get_enqueue_status() { |
||
| 306 | |||
| 307 | private function set_config( $config ) { |
||
| 310 | |||
| 311 | private function get_config() { |
||
| 314 | |||
| 315 | private function write_option( $name, $value ) { |
||
| 341 | |||
| 342 | View Code Duplication | private function read_option( $name, $default = null ) { |
|
| 343 | global $wpdb; |
||
| 344 | $value = $wpdb->get_var( |
||
| 345 | $wpdb->prepare( |
||
| 346 | "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
||
| 347 | $name |
||
| 348 | ) |
||
| 349 | ); |
||
| 350 | $value = maybe_unserialize( $value ); |
||
| 351 | |||
| 352 | if ( $value === null && $default !== null ) { |
||
| 358 | } |
||
| 359 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.