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() { |
||
402 | |||
403 | /** |
||
404 | * Kicks off a full sync. |
||
405 | * |
||
406 | * @access public |
||
407 | * @static |
||
408 | * |
||
409 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
410 | * @return bool True if full sync was successfully started. |
||
411 | */ |
||
412 | public static function do_full_sync( $modules = null ) { |
||
429 | |||
430 | /** |
||
431 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
432 | * |
||
433 | * @access public |
||
434 | * @static |
||
435 | * |
||
436 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
437 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
438 | */ |
||
439 | public static function jetpack_cron_schedule( $schedules ) { |
||
453 | |||
454 | /** |
||
455 | * Starts an incremental sync via cron. |
||
456 | * |
||
457 | * @access public |
||
458 | * @static |
||
459 | */ |
||
460 | public static function do_cron_sync() { |
||
463 | |||
464 | /** |
||
465 | * Starts a full sync via cron. |
||
466 | * |
||
467 | * @access public |
||
468 | * @static |
||
469 | */ |
||
470 | public static function do_cron_full_sync() { |
||
473 | |||
474 | /** |
||
475 | * Try to send actions until we run out of things to send, |
||
476 | * or have to wait more than 15s before sending again, |
||
477 | * or we hit a lock or some other sending issue |
||
478 | * |
||
479 | * @access public |
||
480 | * @static |
||
481 | * |
||
482 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
483 | */ |
||
484 | public static function do_cron_sync_by_type( $type ) { |
||
522 | |||
523 | /** |
||
524 | * Initialize the sync listener. |
||
525 | * |
||
526 | * @access public |
||
527 | * @static |
||
528 | */ |
||
529 | public static function initialize_listener() { |
||
532 | |||
533 | /** |
||
534 | * Initializes the sync sender. |
||
535 | * |
||
536 | * @access public |
||
537 | * @static |
||
538 | */ |
||
539 | public static function initialize_sender() { |
||
543 | |||
544 | /** |
||
545 | * Initializes sync for WooCommerce. |
||
546 | * |
||
547 | * @access public |
||
548 | * @static |
||
549 | */ |
||
550 | public static function initialize_woocommerce() { |
||
556 | |||
557 | /** |
||
558 | * Adds Woo's sync modules to existing modules for sending. |
||
559 | * |
||
560 | * @access public |
||
561 | * @static |
||
562 | * |
||
563 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
564 | * @return array A list of sync modules that now includes Woo's modules. |
||
565 | */ |
||
566 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
570 | |||
571 | /** |
||
572 | * Initializes sync for WP Super Cache. |
||
573 | * |
||
574 | * @access public |
||
575 | * @static |
||
576 | */ |
||
577 | public static function initialize_wp_super_cache() { |
||
583 | |||
584 | /** |
||
585 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
586 | * |
||
587 | * @access public |
||
588 | * @static |
||
589 | * |
||
590 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
591 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
592 | */ |
||
593 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
597 | |||
598 | /** |
||
599 | * Sanitizes the name of sync's cron schedule. |
||
600 | * |
||
601 | * @access public |
||
602 | * @static |
||
603 | * |
||
604 | * @param string $schedule The name of a WordPress cron schedule. |
||
605 | * @return string The sanitized name of sync's cron schedule. |
||
606 | */ |
||
607 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
618 | |||
619 | /** |
||
620 | * Allows offsetting of start times for sync cron jobs. |
||
621 | * |
||
622 | * @access public |
||
623 | * @static |
||
624 | * |
||
625 | * @param string $schedule The name of a cron schedule. |
||
626 | * @param string $hook The hook that this method is responding to. |
||
627 | * @return int The offset for the sync cron schedule. |
||
628 | */ |
||
629 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
653 | |||
654 | /** |
||
655 | * Decides if a sync cron should be scheduled. |
||
656 | * |
||
657 | * @access public |
||
658 | * @static |
||
659 | * |
||
660 | * @param string $schedule The name of a cron schedule. |
||
661 | * @param string $hook The hook that this method is responding to. |
||
662 | */ |
||
663 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
679 | |||
680 | /** |
||
681 | * Clears Jetpack sync cron jobs. |
||
682 | * |
||
683 | * @access public |
||
684 | * @static |
||
685 | */ |
||
686 | public static function clear_sync_cron_jobs() { |
||
690 | |||
691 | /** |
||
692 | * Initializes Jetpack sync cron jobs. |
||
693 | * |
||
694 | * @access public |
||
695 | * @static |
||
696 | */ |
||
697 | public static function init_sync_cron_jobs() { |
||
723 | |||
724 | /** |
||
725 | * Perform maintenance when a plugin upgrade occurs. |
||
726 | * |
||
727 | * @access public |
||
728 | * @static |
||
729 | * |
||
730 | * @param string $new_version New version of the plugin. |
||
731 | * @param string $old_version Old version of the plugin. |
||
732 | */ |
||
733 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
750 | |||
751 | /** |
||
752 | * Get syncing status for the given fields. |
||
753 | * |
||
754 | * @access public |
||
755 | * @static |
||
756 | * |
||
757 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
758 | * @return array An associative array with the status report. |
||
759 | */ |
||
760 | public static function get_sync_status( $fields = null ) { |
||
814 | } |
||
815 |
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.