Complex classes like Actions 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 Actions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Actions { |
||
23 | /** |
||
24 | * A variable to hold a sync sender object. |
||
25 | * |
||
26 | * @access public |
||
27 | * @static |
||
28 | * |
||
29 | * @var Automattic\Jetpack\Sync\Sender |
||
30 | */ |
||
31 | public static $sender = null; |
||
32 | |||
33 | /** |
||
34 | * A variable to hold a sync listener object. |
||
35 | * |
||
36 | * @access public |
||
37 | * @static |
||
38 | * |
||
39 | * @var Automattic\Jetpack\Sync\Listener |
||
40 | */ |
||
41 | public static $listener = null; |
||
42 | |||
43 | /** |
||
44 | * Name of the sync cron schedule. |
||
45 | * |
||
46 | * @access public |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
51 | |||
52 | /** |
||
53 | * Interval between the last and the next sync cron action. |
||
54 | * |
||
55 | * @access public |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
60 | |||
61 | /** |
||
62 | * Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
||
63 | * and set up a shut-down action for sending actions to WordPress.com |
||
64 | * |
||
65 | * @access public |
||
66 | * @static |
||
67 | */ |
||
68 | public static function init() { |
||
106 | |||
107 | /** |
||
108 | * Prepares sync to send actions on shutdown for the current request. |
||
109 | * |
||
110 | * @access public |
||
111 | * @static |
||
112 | */ |
||
113 | public static function add_sender_shutdown() { |
||
135 | |||
136 | /** |
||
137 | * Decides if the sender should run on shutdown for this request. |
||
138 | * |
||
139 | * @access public |
||
140 | * @static |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public static function should_initialize_sender() { |
||
171 | |||
172 | /** |
||
173 | * Decides if the sender should run on shutdown when actions are queued. |
||
174 | * |
||
175 | * @access public |
||
176 | * @static |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public static function should_initialize_sender_enqueue() { |
||
187 | |||
188 | /** |
||
189 | * Decides if sync should run at all during this request. |
||
190 | * |
||
191 | * @access public |
||
192 | * @static |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public static function sync_allowed() { |
||
226 | |||
227 | /** |
||
228 | * Determines if syncing during a cron job is allowed. |
||
229 | * |
||
230 | * @access public |
||
231 | * @static |
||
232 | * |
||
233 | * @return bool|int |
||
234 | */ |
||
235 | public static function sync_via_cron_allowed() { |
||
238 | |||
239 | /** |
||
240 | * Decides if the given post should be Publicized based on its type. |
||
241 | * |
||
242 | * @access public |
||
243 | * @static |
||
244 | * |
||
245 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
246 | * @param \WP_Post $post The post to test for Publicizability. |
||
247 | * @return bool |
||
248 | */ |
||
249 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
256 | |||
257 | /** |
||
258 | * Set an importing flag to `true` in sync settings. |
||
259 | * |
||
260 | * @access public |
||
261 | * @static |
||
262 | */ |
||
263 | public static function set_is_importing_true() { |
||
266 | |||
267 | /** |
||
268 | * Sends data to WordPress.com via an XMLRPC request. |
||
269 | * |
||
270 | * @access public |
||
271 | * @static |
||
272 | * |
||
273 | * @param object $data Data relating to a sync action. |
||
274 | * @param string $codec_name The name of the codec that encodes the data. |
||
275 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
276 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
277 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
278 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
279 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
|
|||
280 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
281 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
282 | */ |
||
283 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
368 | |||
369 | /** |
||
370 | * Kicks off the initial sync. |
||
371 | * |
||
372 | * @access public |
||
373 | * @static |
||
374 | * |
||
375 | * @return bool|null False if sync is not allowed. |
||
376 | */ |
||
377 | public static function do_initial_sync() { |
||
399 | |||
400 | /** |
||
401 | * Kicks off a full sync. |
||
402 | * |
||
403 | * @access public |
||
404 | * @static |
||
405 | * |
||
406 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
407 | * @return bool True if full sync was successfully started. |
||
408 | */ |
||
409 | public static function do_full_sync( $modules = null ) { |
||
426 | |||
427 | /** |
||
428 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
429 | * |
||
430 | * @access public |
||
431 | * @static |
||
432 | * |
||
433 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
434 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
435 | */ |
||
436 | public static function jetpack_cron_schedule( $schedules ) { |
||
450 | |||
451 | /** |
||
452 | * Starts an incremental sync via cron. |
||
453 | * |
||
454 | * @access public |
||
455 | * @static |
||
456 | */ |
||
457 | public static function do_cron_sync() { |
||
460 | |||
461 | /** |
||
462 | * Starts a full sync via cron. |
||
463 | * |
||
464 | * @access public |
||
465 | * @static |
||
466 | */ |
||
467 | public static function do_cron_full_sync() { |
||
470 | |||
471 | /** |
||
472 | * Try to send actions until we run out of things to send, |
||
473 | * or have to wait more than 15s before sending again, |
||
474 | * or we hit a lock or some other sending issue |
||
475 | * |
||
476 | * @access public |
||
477 | * @static |
||
478 | * |
||
479 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
480 | */ |
||
481 | public static function do_cron_sync_by_type( $type ) { |
||
516 | |||
517 | /** |
||
518 | * Initialize the sync listener. |
||
519 | * |
||
520 | * @access public |
||
521 | * @static |
||
522 | */ |
||
523 | public static function initialize_listener() { |
||
526 | |||
527 | /** |
||
528 | * Initializes the sync sender. |
||
529 | * |
||
530 | * @access public |
||
531 | * @static |
||
532 | */ |
||
533 | public static function initialize_sender() { |
||
537 | |||
538 | /** |
||
539 | * Initializes sync for WooCommerce. |
||
540 | * |
||
541 | * @access public |
||
542 | * @static |
||
543 | */ |
||
544 | public static function initialize_woocommerce() { |
||
550 | |||
551 | /** |
||
552 | * Adds Woo's sync modules to existing modules for sending. |
||
553 | * |
||
554 | * @access public |
||
555 | * @static |
||
556 | * |
||
557 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
558 | * @return array A list of sync modules that now includes Woo's modules. |
||
559 | */ |
||
560 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
564 | |||
565 | /** |
||
566 | * Initializes sync for WP Super Cache. |
||
567 | * |
||
568 | * @access public |
||
569 | * @static |
||
570 | */ |
||
571 | public static function initialize_wp_super_cache() { |
||
577 | |||
578 | /** |
||
579 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
580 | * |
||
581 | * @access public |
||
582 | * @static |
||
583 | * |
||
584 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
585 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
586 | */ |
||
587 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
591 | |||
592 | /** |
||
593 | * Sanitizes the name of sync's cron schedule. |
||
594 | * |
||
595 | * @access public |
||
596 | * @static |
||
597 | * |
||
598 | * @param string $schedule The name of a WordPress cron schedule. |
||
599 | * @return string The sanitized name of sync's cron schedule. |
||
600 | */ |
||
601 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
612 | |||
613 | /** |
||
614 | * Allows offsetting of start times for sync cron jobs. |
||
615 | * |
||
616 | * @access public |
||
617 | * @static |
||
618 | * |
||
619 | * @param string $schedule The name of a cron schedule. |
||
620 | * @param string $hook The hook that this method is responding to. |
||
621 | * @return int The offset for the sync cron schedule. |
||
622 | */ |
||
623 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
647 | |||
648 | /** |
||
649 | * Decides if a sync cron should be scheduled. |
||
650 | * |
||
651 | * @access public |
||
652 | * @static |
||
653 | * |
||
654 | * @param string $schedule The name of a cron schedule. |
||
655 | * @param string $hook The hook that this method is responding to. |
||
656 | */ |
||
657 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
673 | |||
674 | /** |
||
675 | * Clears Jetpack sync cron jobs. |
||
676 | * |
||
677 | * @access public |
||
678 | * @static |
||
679 | */ |
||
680 | public static function clear_sync_cron_jobs() { |
||
684 | |||
685 | /** |
||
686 | * Initializes Jetpack sync cron jobs. |
||
687 | * |
||
688 | * @access public |
||
689 | * @static |
||
690 | */ |
||
691 | public static function init_sync_cron_jobs() { |
||
717 | |||
718 | /** |
||
719 | * Perform maintenance when a plugin upgrade occurs. |
||
720 | * |
||
721 | * @access public |
||
722 | * @static |
||
723 | * |
||
724 | * @param string $new_version New version of the plugin. |
||
725 | * @param string $old_version Old version of the plugin. |
||
726 | */ |
||
727 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
744 | |||
745 | /** |
||
746 | * Get syncing status for the given fields. |
||
747 | * |
||
748 | * @access public |
||
749 | * @static |
||
750 | * |
||
751 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
752 | * @return array An associative array with the status report. |
||
753 | */ |
||
754 | public static function get_sync_status( $fields = null ) { |
||
808 | } |
||
809 |
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.