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() { |
||
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 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
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 ) { |
||
117 | |||
118 | /** |
||
119 | * Whether full sync has started. |
||
120 | * |
||
121 | * @access public |
||
122 | * |
||
123 | * @return boolean |
||
124 | */ |
||
125 | public function is_started() { |
||
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() { |
||
146 | |||
147 | /** |
||
148 | * Returns the progress percentage of a full sync. |
||
149 | * |
||
150 | * @access public |
||
151 | * |
||
152 | * @return int|null |
||
153 | */ |
||
154 | public function get_sync_progress_percentage() { |
||
178 | |||
179 | /** |
||
180 | * Whether full sync has finished. |
||
181 | * |
||
182 | * @access public |
||
183 | * |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function is_finished() { |
||
189 | |||
190 | /** |
||
191 | * Clear all the full sync data. |
||
192 | * |
||
193 | * @access public |
||
194 | */ |
||
195 | public function reset_data() { |
||
199 | |||
200 | /** |
||
201 | * Clear all the full sync status options. |
||
202 | * |
||
203 | * @access public |
||
204 | */ |
||
205 | public function clear_status() { |
||
208 | |||
209 | /** |
||
210 | * Updates the status of the current full sync. |
||
211 | * |
||
212 | * @access public |
||
213 | * |
||
214 | * @param array $values New values to set. |
||
215 | * |
||
216 | * @return bool True if success. |
||
217 | */ |
||
218 | public function update_status( $values ) { |
||
221 | |||
222 | /** |
||
223 | * Retrieve the status of the current full sync. |
||
224 | * |
||
225 | * @param array $values New values to set. |
||
226 | * |
||
227 | * @access public |
||
228 | * |
||
229 | * @return boolean Full sync status. |
||
230 | */ |
||
231 | public function set_status( $values ) { |
||
234 | |||
235 | /** |
||
236 | * Given an initial Full Sync configuration get the initial status. |
||
237 | * |
||
238 | * @param array $full_sync_config Full sync configuration. |
||
239 | * |
||
240 | * @return array Initial Sent status. |
||
241 | */ |
||
242 | public function get_initial_progress( $full_sync_config ) { |
||
256 | |||
257 | /** |
||
258 | * Get the range for content (posts and comments) to sync. |
||
259 | * |
||
260 | * @access private |
||
261 | * |
||
262 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
263 | */ |
||
264 | View Code Duplication | private function get_content_range() { |
|
278 | |||
279 | /** |
||
280 | * Get the range (min ID, max ID and total items) of items to sync. |
||
281 | * |
||
282 | * @access public |
||
283 | * |
||
284 | * @param string $type Type of sync item to get the range for. |
||
285 | * |
||
286 | * @return array Array of min ID, max ID and total items in the range. |
||
287 | */ |
||
288 | View Code Duplication | public function get_range( $type ) { |
|
317 | |||
318 | /** |
||
319 | * Continue sending instead of enqueueing. |
||
320 | * |
||
321 | * @access public |
||
322 | */ |
||
323 | public function continue_enqueuing() { |
||
326 | |||
327 | /** |
||
328 | * Continue sending. |
||
329 | * |
||
330 | * @access public |
||
331 | */ |
||
332 | public function continue_sending() { |
||
333 | // Return early if Full Sync is not running. |
||
334 | if ( ! $this->is_started() || $this->get_status()['finished'] ) { |
||
335 | return; |
||
336 | } |
||
337 | |||
338 | // Obtain send Lock. |
||
339 | $lock = new Lock(); |
||
340 | $lock_expiration = $lock->attempt( self::LOCK_NAME ); |
||
341 | |||
342 | // Return if unable to obtain lock. |
||
343 | if ( false === $lock_expiration ) { |
||
344 | return; |
||
345 | } |
||
346 | |||
347 | // Send Full Sync actions. |
||
348 | $this->send(); |
||
349 | |||
350 | // Remove lock. |
||
351 | $lock->remove( self::LOCK_NAME, $lock_expiration ); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Immediately send the next items to full sync. |
||
356 | * |
||
357 | * @access public |
||
358 | */ |
||
359 | public function send() { |
||
379 | |||
380 | /** |
||
381 | * Get Modules that are configured to Full Sync and haven't finished sending |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | public function get_remaining_modules_to_send() { |
||
414 | |||
415 | /** |
||
416 | * Send 'jetpack_full_sync_end' and update 'finished' status. |
||
417 | * |
||
418 | * @access public |
||
419 | */ |
||
420 | public function send_full_sync_end() { |
||
439 | |||
440 | /** |
||
441 | * Empty Function as we don't close buffers on Immediate Full Sync. |
||
442 | * |
||
443 | * @param array $actions an array of actions, ignored for queueless sync. |
||
444 | */ |
||
445 | public function update_sent_progress_action( $actions ) { } // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
446 | |||
447 | } |
||
448 |
This check looks for
@param
annotations 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.