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 |
||
21 | class Full_Sync_Immediately extends Module { |
||
22 | /** |
||
23 | * Prefix of the full sync status option name. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | const STATUS_OPTION = 'jetpack_sync_full_status'; |
||
28 | |||
29 | /** |
||
30 | * Sync Lock name. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | const LOCK_NAME = 'full_sync'; |
||
35 | |||
36 | /** |
||
37 | * Sync module name. |
||
38 | * |
||
39 | * @access public |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | public function name() { |
||
46 | |||
47 | /** |
||
48 | * Initialize action listeners for full sync. |
||
49 | * |
||
50 | * @access public |
||
51 | * |
||
52 | * @param callable $callable Action handler callable. |
||
53 | */ |
||
54 | public function init_full_sync_listeners( $callable ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
56 | |||
57 | /** |
||
58 | * Start a full sync. |
||
59 | * |
||
60 | * @access public |
||
61 | * |
||
62 | * @param array $full_sync_config Full sync configuration. |
||
|
|||
63 | * |
||
64 | * @return bool Always returns true at success. |
||
65 | */ |
||
66 | public function start( $full_sync_config = null ) { |
||
118 | |||
119 | /** |
||
120 | * Whether full sync has started. |
||
121 | * |
||
122 | * @access public |
||
123 | * |
||
124 | * @return boolean |
||
125 | */ |
||
126 | public function is_started() { |
||
129 | |||
130 | /** |
||
131 | * Retrieve the status of the current full sync. |
||
132 | * |
||
133 | * @access public |
||
134 | * |
||
135 | * @return array Full sync status. |
||
136 | */ |
||
137 | public function get_status() { |
||
147 | |||
148 | /** |
||
149 | * Returns the progress percentage of a full sync. |
||
150 | * |
||
151 | * @access public |
||
152 | * |
||
153 | * @return int|null |
||
154 | */ |
||
155 | public function get_sync_progress_percentage() { |
||
179 | |||
180 | /** |
||
181 | * Whether full sync has finished. |
||
182 | * |
||
183 | * @access public |
||
184 | * |
||
185 | * @return boolean |
||
186 | */ |
||
187 | public function is_finished() { |
||
190 | |||
191 | /** |
||
192 | * Clear all the full sync data. |
||
193 | * |
||
194 | * @access public |
||
195 | */ |
||
196 | public function reset_data() { |
||
200 | |||
201 | /** |
||
202 | * Clear all the full sync status options. |
||
203 | * |
||
204 | * @access public |
||
205 | */ |
||
206 | public function clear_status() { |
||
209 | |||
210 | /** |
||
211 | * Updates the status of the current full sync. |
||
212 | * |
||
213 | * @access public |
||
214 | * |
||
215 | * @param array $values New values to set. |
||
216 | * |
||
217 | * @return bool True if success. |
||
218 | */ |
||
219 | public function update_status( $values ) { |
||
222 | |||
223 | /** |
||
224 | * Retrieve the status of the current full sync. |
||
225 | * |
||
226 | * @param array $values New values to set. |
||
227 | * |
||
228 | * @access public |
||
229 | * |
||
230 | * @return boolean Full sync status. |
||
231 | */ |
||
232 | public function set_status( $values ) { |
||
235 | |||
236 | /** |
||
237 | * Given an initial Full Sync configuration get the initial status. |
||
238 | * |
||
239 | * @param array $full_sync_config Full sync configuration. |
||
240 | * |
||
241 | * @return array Initial Sent status. |
||
242 | */ |
||
243 | public function get_initial_progress( $full_sync_config ) { |
||
257 | |||
258 | /** |
||
259 | * Get the range for content (posts and comments) to sync. |
||
260 | * |
||
261 | * @access private |
||
262 | * |
||
263 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
264 | */ |
||
265 | View Code Duplication | private function get_content_range() { |
|
279 | |||
280 | /** |
||
281 | * Get the range (min ID, max ID and total items) of items to sync. |
||
282 | * |
||
283 | * @access public |
||
284 | * |
||
285 | * @param string $type Type of sync item to get the range for. |
||
286 | * |
||
287 | * @return array Array of min ID, max ID and total items in the range. |
||
288 | */ |
||
289 | View Code Duplication | public function get_range( $type ) { |
|
318 | |||
319 | /** |
||
320 | * Continue sending instead of enqueueing. |
||
321 | * |
||
322 | * @access public |
||
323 | */ |
||
324 | public function continue_enqueuing() { |
||
327 | |||
328 | /** |
||
329 | * Continue sending. |
||
330 | * |
||
331 | * @access public |
||
332 | */ |
||
333 | public function continue_sending() { |
||
366 | |||
367 | /** |
||
368 | * Immediately send the next items to full sync. |
||
369 | * |
||
370 | * @access public |
||
371 | */ |
||
372 | public function send() { |
||
396 | |||
397 | /** |
||
398 | * Get Modules that are configured to Full Sync and haven't finished sending |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | public function get_remaining_modules_to_send() { |
||
431 | |||
432 | /** |
||
433 | * Send 'jetpack_full_sync_end' and update 'finished' status. |
||
434 | * |
||
435 | * @access public |
||
436 | */ |
||
437 | public function send_full_sync_end() { |
||
456 | |||
457 | /** |
||
458 | * Empty Function as we don't close buffers on Immediate Full Sync. |
||
459 | * |
||
460 | * @param array $actions an array of actions, ignored for queueless sync. |
||
461 | */ |
||
462 | public function update_sent_progress_action( $actions ) { } // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
463 | |||
464 | } |
||
465 |
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.