Complex classes like 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 Full_Sync, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Full_Sync extends Module { |
||
27 | /** |
||
28 | * Prefix of the full sync status option name. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | const STATUS_OPTION_PREFIX = 'jetpack_sync_full_'; |
||
33 | |||
34 | /** |
||
35 | * Timeout between the previous and the next allowed full sync. |
||
36 | * |
||
37 | * @todo Remove this as it's no longer used since https://github.com/Automattic/jetpack/pull/4561 |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | const FULL_SYNC_TIMEOUT = 3600; |
||
42 | |||
43 | /** |
||
44 | * Sync module name. |
||
45 | * |
||
46 | * @access public |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | public function name() { |
||
53 | |||
54 | /** |
||
55 | * Initialize action listeners for full sync. |
||
56 | * |
||
57 | * @access public |
||
58 | * |
||
59 | * @param callable $callable Action handler callable. |
||
60 | */ |
||
61 | public function init_full_sync_listeners( $callable ) { |
||
67 | |||
68 | /** |
||
69 | * Initialize the module in the sender. |
||
70 | * |
||
71 | * @access public |
||
72 | */ |
||
73 | public function init_before_send() { |
||
77 | |||
78 | /** |
||
79 | * Start a full sync. |
||
80 | * |
||
81 | * @access public |
||
82 | * |
||
83 | * @param array $module_configs Full sync configuration for all sync modules. |
||
|
|||
84 | * @return bool Always returns true at success. |
||
85 | */ |
||
86 | public function start( $module_configs = null ) { |
||
170 | |||
171 | /** |
||
172 | * Enqueue the next items to sync. |
||
173 | * |
||
174 | * @access public |
||
175 | * |
||
176 | * @param array $configs Full sync configuration for all sync modules. |
||
177 | * @param array $enqueue_status Current status of the queue, indexed by sync modules. |
||
178 | */ |
||
179 | public function continue_enqueuing( $configs = null, $enqueue_status = null ) { |
||
263 | |||
264 | /** |
||
265 | * Get the range (min ID, max ID and total items) of items to sync. |
||
266 | * |
||
267 | * @access public |
||
268 | * |
||
269 | * @param string $type Type of sync item to get the range for. |
||
270 | * @return array Array of min ID, max ID and total items in the range. |
||
271 | */ |
||
272 | public function get_range( $type ) { |
||
301 | |||
302 | /** |
||
303 | * Get the range for content (posts and comments) to sync. |
||
304 | * |
||
305 | * @access private |
||
306 | * |
||
307 | * @param array $config Full sync configuration for this all sync modules. |
||
308 | * @return array Array of range (min ID, max ID, total items) for all content types. |
||
309 | */ |
||
310 | private function get_content_range( $config ) { |
||
311 | $range = array(); |
||
312 | // Only when we are sending the whole range do we want to send also the range. |
||
313 | if ( true === isset( $config['posts'] ) && $config['posts'] ) { |
||
314 | $range['posts'] = $this->get_range( 'posts' ); |
||
315 | } |
||
316 | |||
317 | if ( true === isset( $config['comments'] ) && $config['comments'] ) { |
||
318 | $range['comments'] = $this->get_range( 'comments' ); |
||
319 | } |
||
320 | return $range; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Update the progress after sync modules actions have been processed on the server. |
||
325 | * |
||
326 | * @access public |
||
327 | * |
||
328 | * @param array $actions Actions that have been processed on the server. |
||
329 | */ |
||
330 | public function update_sent_progress_action( $actions ) { |
||
375 | |||
376 | /** |
||
377 | * Get the name of the action for an item in the sync queue. |
||
378 | * |
||
379 | * @access public |
||
380 | * |
||
381 | * @param array $queue_item Item of the sync queue. |
||
382 | * @return string|boolean Name of the action, false if queue item is invalid. |
||
383 | */ |
||
384 | public function get_action_name( $queue_item ) { |
||
390 | |||
391 | /** |
||
392 | * Retrieve the total number of items we're syncing in a particular queue item (action). |
||
393 | * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]` |
||
394 | * represents the first (and only) chunk of items to sync in that action. |
||
395 | * |
||
396 | * @access public |
||
397 | * |
||
398 | * @param array $queue_item Item of the sync queue that corresponds to a particular action. |
||
399 | * @return int Total number of items in the action. |
||
400 | */ |
||
401 | public function get_action_totals( $queue_item ) { |
||
412 | |||
413 | /** |
||
414 | * Retrieve the total number of items for a set of actions, grouped by action name. |
||
415 | * |
||
416 | * @access public |
||
417 | * |
||
418 | * @param array $actions An array of actions. |
||
419 | * @return array An array, representing the total number of items, grouped per action. |
||
420 | */ |
||
421 | public function get_actions_totals( $actions ) { |
||
435 | |||
436 | /** |
||
437 | * Whether full sync has started. |
||
438 | * |
||
439 | * @access public |
||
440 | * |
||
441 | * @return boolean |
||
442 | */ |
||
443 | public function is_started() { |
||
446 | |||
447 | /** |
||
448 | * Whether full sync has finished. |
||
449 | * |
||
450 | * @access public |
||
451 | * |
||
452 | * @return boolean |
||
453 | */ |
||
454 | public function is_finished() { |
||
457 | |||
458 | /** |
||
459 | * Retrieve the status of the current full sync. |
||
460 | * |
||
461 | * @access public |
||
462 | * |
||
463 | * @return array Full sync status. |
||
464 | */ |
||
465 | public function get_status() { |
||
510 | |||
511 | /** |
||
512 | * Clear all the full sync status options. |
||
513 | * |
||
514 | * @access public |
||
515 | */ |
||
516 | public function clear_status() { |
||
531 | |||
532 | /** |
||
533 | * Clear all the full sync data. |
||
534 | * |
||
535 | * @access public |
||
536 | */ |
||
537 | public function reset_data() { |
||
544 | |||
545 | /** |
||
546 | * Get the value of a full sync status option. |
||
547 | * |
||
548 | * @access private |
||
549 | * |
||
550 | * @param string $name Name of the option. |
||
551 | * @param mixed $default Default value of the option. |
||
552 | * @return mixed Option value. |
||
553 | */ |
||
554 | private function get_status_option( $name, $default = null ) { |
||
559 | |||
560 | /** |
||
561 | * Update the value of a full sync status option. |
||
562 | * |
||
563 | * @access private |
||
564 | * |
||
565 | * @param string $name Name of the option. |
||
566 | * @param mixed $value Value of the option. |
||
567 | * @param boolean $autoload Whether the option should be autoloaded at the beginning of the request. |
||
568 | */ |
||
569 | private function update_status_option( $name, $value, $autoload = false ) { |
||
572 | |||
573 | /** |
||
574 | * Set the full sync enqueue status. |
||
575 | * |
||
576 | * @access private |
||
577 | * |
||
578 | * @param array $new_status The new full sync enqueue status. |
||
579 | */ |
||
580 | private function set_enqueue_status( $new_status ) { |
||
583 | |||
584 | /** |
||
585 | * Delete full sync enqueue status. |
||
586 | * |
||
587 | * @access private |
||
588 | * |
||
589 | * @return boolean Whether the status was deleted. |
||
590 | */ |
||
591 | private function delete_enqueue_status() { |
||
594 | |||
595 | /** |
||
596 | * Retrieve the current full sync enqueue status. |
||
597 | * |
||
598 | * @access private |
||
599 | * |
||
600 | * @return array Full sync enqueue status. |
||
601 | */ |
||
602 | public function get_enqueue_status() { |
||
605 | |||
606 | /** |
||
607 | * Set the full sync enqueue configuration. |
||
608 | * |
||
609 | * @access private |
||
610 | * |
||
611 | * @param array $config The new full sync enqueue configuration. |
||
612 | */ |
||
613 | private function set_config( $config ) { |
||
616 | |||
617 | /** |
||
618 | * Delete full sync configuration. |
||
619 | * |
||
620 | * @access private |
||
621 | * |
||
622 | * @return boolean Whether the configuration was deleted. |
||
623 | */ |
||
624 | private function delete_config() { |
||
627 | |||
628 | /** |
||
629 | * Retrieve the current full sync enqueue config. |
||
630 | * |
||
631 | * @access private |
||
632 | * |
||
633 | * @return array Full sync enqueue config. |
||
634 | */ |
||
635 | private function get_config() { |
||
638 | |||
639 | /** |
||
640 | * Update an option manually to bypass filters and caching. |
||
641 | * |
||
642 | * @access private |
||
643 | * |
||
644 | * @param string $name Option name. |
||
645 | * @param mixed $value Option value. |
||
646 | * @return int The number of updated rows in the database. |
||
647 | */ |
||
648 | private function write_option( $name, $value ) { |
||
677 | |||
678 | /** |
||
679 | * Update an option manually to bypass filters and caching. |
||
680 | * |
||
681 | * @access private |
||
682 | * |
||
683 | * @param string $name Option name. |
||
684 | * @param mixed $default Default option value. |
||
685 | * @return mixed Option value. |
||
686 | */ |
||
687 | private function read_option( $name, $default = null ) { |
||
703 | } |
||
704 |
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.