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 Full_Sync_Immediately 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 Full_Sync_Immediately, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 20 | class Full_Sync_Immediately extends Module { | 
            ||
| 21 | /**  | 
            ||
| 22 | * Prefix of the full sync status option name.  | 
            ||
| 23 | *  | 
            ||
| 24 | * @var string  | 
            ||
| 25 | */  | 
            ||
| 26 | const STATUS_OPTION = 'jetpack_sync_full_status';  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Sync Lock name.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @var string  | 
            ||
| 32 | */  | 
            ||
| 33 | const LOCK_NAME = 'full_sync';  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * Sync module name.  | 
            ||
| 37 | *  | 
            ||
| 38 | * @access public  | 
            ||
| 39 | *  | 
            ||
| 40 | * @return string  | 
            ||
| 41 | */  | 
            ||
| 42 | 	public function name() { | 
            ||
| 43 | return 'full-sync';  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * Initialize action listeners for full sync.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @access public  | 
            ||
| 50 | *  | 
            ||
| 51 | * @param callable $callable Action handler callable.  | 
            ||
| 52 | */  | 
            ||
| 53 | 	public function init_full_sync_listeners( $callable ) { | 
            ||
| 54 | }  | 
            ||
| 55 | |||
| 56 | /**  | 
            ||
| 57 | * Start a full sync.  | 
            ||
| 58 | *  | 
            ||
| 59 | * @access public  | 
            ||
| 60 | *  | 
            ||
| 61 | * @param array $full_sync_config Full sync configuration.  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 62 | *  | 
            ||
| 63 | * @return bool Always returns true at success.  | 
            ||
| 64 | */  | 
            ||
| 65 | 	public function start( $full_sync_config = null ) { | 
            ||
| 66 | // There was a full sync in progress.  | 
            ||
| 67 | 		if ( $this->is_started() && ! $this->is_finished() ) { | 
            ||
| 68 | /**  | 
            ||
| 69 | * Fires when a full sync is cancelled.  | 
            ||
| 70 | *  | 
            ||
| 71 | * @since 4.2.0  | 
            ||
| 72 | */  | 
            ||
| 73 | do_action( 'jetpack_full_sync_cancelled' );  | 
            ||
| 74 | $this->send_action( 'jetpack_full_sync_cancelled' );  | 
            ||
| 75 | }  | 
            ||
| 76 | |||
| 77 | // Remove all evidence of previous full sync items and status.  | 
            ||
| 78 | $this->reset_data();  | 
            ||
| 79 | |||
| 80 | 		if ( ! is_array( $full_sync_config ) ) { | 
            ||
| 81 | $full_sync_config = Defaults::$default_full_sync_config;  | 
            ||
| 82 | 			if ( is_multisite() ) { | 
            ||
| 83 | $full_sync_config['network_options'] = 1;  | 
            ||
| 84 | }  | 
            ||
| 85 | }  | 
            ||
| 86 | |||
| 87 | 		if ( isset( $full_sync_config['users'] ) && 'initial' === $full_sync_config['users'] ) { | 
            ||
| 88 | $full_sync_config['users'] = Modules::get_module( 'users' )->get_initial_sync_user_config();  | 
            ||
| 89 | }  | 
            ||
| 90 | |||
| 91 | $this->update_status(  | 
            ||
| 92 | array(  | 
            ||
| 93 | 'started' => time(),  | 
            ||
| 94 | 'config' => $full_sync_config,  | 
            ||
| 95 | 'progress' => $this->get_initial_progress( $full_sync_config ),  | 
            ||
| 96 | )  | 
            ||
| 97 | );  | 
            ||
| 98 | |||
| 99 | $range = $this->get_content_range( $full_sync_config );  | 
            ||
| 100 | /**  | 
            ||
| 101 | * Fires when a full sync begins. This action is serialized  | 
            ||
| 102 | * and sent to the server so that it knows a full sync is coming.  | 
            ||
| 103 | *  | 
            ||
| 104 | * @param array $full_sync_config Sync configuration for all sync modules.  | 
            ||
| 105 | * @param array $range Range of the sync items, containing min and max IDs for some item types.  | 
            ||
| 106 | * @param array $empty The modules with no items to sync during a full sync.  | 
            ||
| 107 | *  | 
            ||
| 108 | * @since 4.2.0  | 
            ||
| 109 | * @since 7.3.0 Added $range arg.  | 
            ||
| 110 | * @since 7.4.0 Added $empty arg.  | 
            ||
| 111 | */  | 
            ||
| 112 | do_action( 'jetpack_full_sync_start', $full_sync_config, $range );  | 
            ||
| 113 | $this->send_action( 'jetpack_full_sync_start', array( $full_sync_config, $range ) );  | 
            ||
| 114 | |||
| 115 | return true;  | 
            ||
| 116 | }  | 
            ||
| 117 | |||
| 118 | /**  | 
            ||
| 119 | * Whether full sync has started.  | 
            ||
| 120 | *  | 
            ||
| 121 | * @access public  | 
            ||
| 122 | *  | 
            ||
| 123 | * @return boolean  | 
            ||
| 124 | */  | 
            ||
| 125 | 	public function is_started() { | 
            ||
| 126 | return ! ! $this->get_status()['started'];  | 
            ||
| 127 | }  | 
            ||
| 128 | |||
| 129 | /**  | 
            ||
| 130 | * Retrieve the status of the current full sync.  | 
            ||
| 131 | *  | 
            ||
| 132 | * @access public  | 
            ||
| 133 | *  | 
            ||
| 134 | * @return array Full sync status.  | 
            ||
| 135 | */  | 
            ||
| 136 | 	public function get_status() { | 
            ||
| 137 | $default = array(  | 
            ||
| 138 | 'started' => false,  | 
            ||
| 139 | 'finished' => false,  | 
            ||
| 140 | 'progress' => array(),  | 
            ||
| 141 | 'config' => array(),  | 
            ||
| 142 | );  | 
            ||
| 143 | |||
| 144 | return wp_parse_args( \Jetpack_Options::get_raw_option( self::STATUS_OPTION ), $default );  | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * Whether full sync has finished.  | 
            ||
| 149 | *  | 
            ||
| 150 | * @access public  | 
            ||
| 151 | *  | 
            ||
| 152 | * @return boolean  | 
            ||
| 153 | */  | 
            ||
| 154 | 	public function is_finished() { | 
            ||
| 155 | return ! ! $this->get_status()['finished'];  | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * Clear all the full sync data.  | 
            ||
| 160 | *  | 
            ||
| 161 | * @access public  | 
            ||
| 162 | */  | 
            ||
| 163 | 	public function reset_data() { | 
            ||
| 164 | $this->clear_status();  | 
            ||
| 165 | ( new Lock() )->remove( self::LOCK_NAME );  | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 | /**  | 
            ||
| 169 | * Clear all the full sync status options.  | 
            ||
| 170 | *  | 
            ||
| 171 | * @access public  | 
            ||
| 172 | */  | 
            ||
| 173 | 	public function clear_status() { | 
            ||
| 174 | \Jetpack_Options::delete_raw_option( self::STATUS_OPTION );  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * Updates the status of the current full sync.  | 
            ||
| 179 | *  | 
            ||
| 180 | * @access public  | 
            ||
| 181 | *  | 
            ||
| 182 | * @param array $values New values to set.  | 
            ||
| 183 | *  | 
            ||
| 184 | * @return bool True if success.  | 
            ||
| 185 | */  | 
            ||
| 186 | 	public function update_status( $values ) { | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Retrieve the status of the current full sync.  | 
            ||
| 192 | *  | 
            ||
| 193 | * @param array $values New values to set.  | 
            ||
| 194 | *  | 
            ||
| 195 | * @access public  | 
            ||
| 196 | *  | 
            ||
| 197 | * @return boolean Full sync status.  | 
            ||
| 198 | */  | 
            ||
| 199 | 	public function set_status( $values ) { | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Given an initial Full Sync configuration get the initial status.  | 
            ||
| 205 | *  | 
            ||
| 206 | * @param array $full_sync_config Full sync configuration.  | 
            ||
| 207 | *  | 
            ||
| 208 | * @return array Initial Sent status.  | 
            ||
| 209 | */  | 
            ||
| 210 | 	public function get_initial_progress( $full_sync_config ) { | 
            ||
| 224 | |||
| 225 | /**  | 
            ||
| 226 | * Get the range for content (posts and comments) to sync.  | 
            ||
| 227 | *  | 
            ||
| 228 | * @access private  | 
            ||
| 229 | *  | 
            ||
| 230 | * @return array Array of range (min ID, max ID, total items) for all content types.  | 
            ||
| 231 | */  | 
            ||
| 232 | View Code Duplication | 	private function get_content_range() { | 
            |
| 246 | |||
| 247 | /**  | 
            ||
| 248 | * Get the range (min ID, max ID and total items) of items to sync.  | 
            ||
| 249 | *  | 
            ||
| 250 | * @access public  | 
            ||
| 251 | *  | 
            ||
| 252 | * @param string $type Type of sync item to get the range for.  | 
            ||
| 253 | *  | 
            ||
| 254 | * @return array Array of min ID, max ID and total items in the range.  | 
            ||
| 255 | */  | 
            ||
| 256 | View Code Duplication | 	public function get_range( $type ) { | 
            |
| 285 | |||
| 286 | /**  | 
            ||
| 287 | * Continue sending instead of enqueueing.  | 
            ||
| 288 | *  | 
            ||
| 289 | * @access public  | 
            ||
| 290 | */  | 
            ||
| 291 | 	public function continue_enqueuing() { | 
            ||
| 294 | |||
| 295 | /**  | 
            ||
| 296 | * Continue sending.  | 
            ||
| 297 | *  | 
            ||
| 298 | * @access public  | 
            ||
| 299 | */  | 
            ||
| 300 | View Code Duplication | 	public function continue_sending() { | 
            |
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * Immediately send the next items to full sync.  | 
            ||
| 312 | *  | 
            ||
| 313 | * @access public  | 
            ||
| 314 | */  | 
            ||
| 315 | 	public function send() { | 
            ||
| 335 | |||
| 336 | /**  | 
            ||
| 337 | * Get Modules that are configured to Full Sync and haven't finished sending  | 
            ||
| 338 | *  | 
            ||
| 339 | * @return array  | 
            ||
| 340 | */  | 
            ||
| 341 | 	public function get_remaining_modules_to_send() { | 
            ||
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * Send 'jetpack_full_sync_end' and update 'finished' status.  | 
            ||
| 373 | *  | 
            ||
| 374 | * @access public  | 
            ||
| 375 | */  | 
            ||
| 376 | 	public function send_full_sync_end() { | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * Empty Function as we don't close buffers on Immediate Full Sync.  | 
            ||
| 398 | *  | 
            ||
| 399 | * @param Array $actions an array of actions, ignored for queueless sync.  | 
            ||
| 400 | */  | 
            ||
| 401 | 	public function update_sent_progress_action( $actions ) { | 
            ||
| 404 | }  | 
            ||
| 405 | 
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.