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 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
281 | */ |
||
282 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null ) { |
||
367 | |||
368 | /** |
||
369 | * Kicks off the initial sync. |
||
370 | * |
||
371 | * @access public |
||
372 | * @static |
||
373 | * |
||
374 | * @return bool|null False if sync is not allowed. |
||
375 | */ |
||
376 | public static function do_initial_sync() { |
||
401 | |||
402 | /** |
||
403 | * Kicks off a full sync. |
||
404 | * |
||
405 | * @access public |
||
406 | * @static |
||
407 | * |
||
408 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
409 | * @return bool True if full sync was successfully started. |
||
410 | */ |
||
411 | public static function do_full_sync( $modules = null ) { |
||
428 | |||
429 | /** |
||
430 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
431 | * |
||
432 | * @access public |
||
433 | * @static |
||
434 | * |
||
435 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
436 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
437 | */ |
||
438 | public static function jetpack_cron_schedule( $schedules ) { |
||
452 | |||
453 | /** |
||
454 | * Starts an incremental sync via cron. |
||
455 | * |
||
456 | * @access public |
||
457 | * @static |
||
458 | */ |
||
459 | public static function do_cron_sync() { |
||
462 | |||
463 | /** |
||
464 | * Starts a full sync via cron. |
||
465 | * |
||
466 | * @access public |
||
467 | * @static |
||
468 | */ |
||
469 | public static function do_cron_full_sync() { |
||
472 | |||
473 | /** |
||
474 | * Try to send actions until we run out of things to send, |
||
475 | * or have to wait more than 15s before sending again, |
||
476 | * or we hit a lock or some other sending issue |
||
477 | * |
||
478 | * @access public |
||
479 | * @static |
||
480 | * |
||
481 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
482 | */ |
||
483 | public static function do_cron_sync_by_type( $type ) { |
||
508 | |||
509 | /** |
||
510 | * Initialize the sync listener. |
||
511 | * |
||
512 | * @access public |
||
513 | * @static |
||
514 | */ |
||
515 | public static function initialize_listener() { |
||
518 | |||
519 | /** |
||
520 | * Initializes the sync sender. |
||
521 | * |
||
522 | * @access public |
||
523 | * @static |
||
524 | */ |
||
525 | public static function initialize_sender() { |
||
529 | |||
530 | /** |
||
531 | * Initializes sync for WooCommerce. |
||
532 | * |
||
533 | * @access public |
||
534 | * @static |
||
535 | */ |
||
536 | public static function initialize_woocommerce() { |
||
542 | |||
543 | /** |
||
544 | * Adds Woo's sync modules to existing modules for sending. |
||
545 | * |
||
546 | * @access public |
||
547 | * @static |
||
548 | * |
||
549 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
550 | * @return array A list of sync modules that now includes Woo's modules. |
||
551 | */ |
||
552 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
556 | |||
557 | /** |
||
558 | * Initializes sync for WP Super Cache. |
||
559 | * |
||
560 | * @access public |
||
561 | * @static |
||
562 | */ |
||
563 | public static function initialize_wp_super_cache() { |
||
569 | |||
570 | /** |
||
571 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
572 | * |
||
573 | * @access public |
||
574 | * @static |
||
575 | * |
||
576 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
577 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
578 | */ |
||
579 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
583 | |||
584 | /** |
||
585 | * Sanitizes the name of sync's cron schedule. |
||
586 | * |
||
587 | * @access public |
||
588 | * @static |
||
589 | * |
||
590 | * @param string $schedule The name of a WordPress cron schedule. |
||
591 | * @return string The sanitized name of sync's cron schedule. |
||
592 | */ |
||
593 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
604 | |||
605 | /** |
||
606 | * Allows offsetting of start times for sync cron jobs. |
||
607 | * |
||
608 | * @access public |
||
609 | * @static |
||
610 | * |
||
611 | * @param string $schedule The name of a cron schedule. |
||
612 | * @param string $hook The hook that this method is responding to. |
||
613 | * @return int The offset for the sync cron schedule. |
||
614 | */ |
||
615 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
639 | |||
640 | /** |
||
641 | * Decides if a sync cron should be scheduled. |
||
642 | * |
||
643 | * @access public |
||
644 | * @static |
||
645 | * |
||
646 | * @param string $schedule The name of a cron schedule. |
||
647 | * @param string $hook The hook that this method is responding to. |
||
648 | */ |
||
649 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
665 | |||
666 | /** |
||
667 | * Clears Jetpack sync cron jobs. |
||
668 | * |
||
669 | * @access public |
||
670 | * @static |
||
671 | */ |
||
672 | public static function clear_sync_cron_jobs() { |
||
676 | |||
677 | /** |
||
678 | * Initializes Jetpack sync cron jobs. |
||
679 | * |
||
680 | * @access public |
||
681 | * @static |
||
682 | */ |
||
683 | public static function init_sync_cron_jobs() { |
||
709 | |||
710 | /** |
||
711 | * Perform maintenance when a plugin upgrade occurs. |
||
712 | * |
||
713 | * @access public |
||
714 | * @static |
||
715 | * |
||
716 | * @param string $new_version New version of the plugin. |
||
717 | * @param string $old_version Old version of the plugin. |
||
718 | */ |
||
719 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
736 | |||
737 | /** |
||
738 | * Get syncing status for the given fields. |
||
739 | * |
||
740 | * @access public |
||
741 | * @static |
||
742 | * |
||
743 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
744 | * @return array An associative array with the status report. |
||
745 | */ |
||
746 | public static function get_sync_status( $fields = null ) { |
||
800 | } |
||
801 |
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.