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 |
||
21 | class Actions { |
||
22 | /** |
||
23 | * A variable to hold a sync sender object. |
||
24 | * |
||
25 | * @access public |
||
26 | * @static |
||
27 | * |
||
28 | * @var Automattic\Jetpack\Sync\Sender |
||
29 | */ |
||
30 | public static $sender = null; |
||
31 | |||
32 | /** |
||
33 | * A variable to hold a sync listener object. |
||
34 | * |
||
35 | * @access public |
||
36 | * @static |
||
37 | * |
||
38 | * @var Automattic\Jetpack\Sync\Listener |
||
39 | */ |
||
40 | public static $listener = null; |
||
41 | |||
42 | /** |
||
43 | * Name of the sync cron schedule. |
||
44 | * |
||
45 | * @access public |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
50 | |||
51 | /** |
||
52 | * Interval between the last and the next sync cron action. |
||
53 | * |
||
54 | * @access public |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
59 | |||
60 | /** |
||
61 | * Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
||
62 | * and set up a shut-down action for sending actions to WordPress.com |
||
63 | * |
||
64 | * @access public |
||
65 | * @static |
||
66 | */ |
||
67 | public static function init() { |
||
105 | |||
106 | /** |
||
107 | * Prepares sync to send actions on shutdown for the current request. |
||
108 | * |
||
109 | * @access public |
||
110 | * @static |
||
111 | */ |
||
112 | public static function add_sender_shutdown() { |
||
134 | |||
135 | /** |
||
136 | * Decides if the sender should run on shutdown for this request. |
||
137 | * |
||
138 | * @access public |
||
139 | * @static |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public static function should_initialize_sender() { |
||
170 | |||
171 | /** |
||
172 | * Decides if sync should run at all during this request. |
||
173 | * |
||
174 | * @access public |
||
175 | * @static |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | public static function sync_allowed() { |
||
206 | |||
207 | /** |
||
208 | * Determines if syncing during a cron job is allowed. |
||
209 | * |
||
210 | * @access public |
||
211 | * @static |
||
212 | * |
||
213 | * @return bool|int |
||
214 | */ |
||
215 | public static function sync_via_cron_allowed() { |
||
218 | |||
219 | /** |
||
220 | * Decides if the given post should be Publicized based on its type. |
||
221 | * |
||
222 | * @access public |
||
223 | * @static |
||
224 | * |
||
225 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
226 | * @param \WP_Post $post The post to test for Publicizability. |
||
227 | * @return bool |
||
228 | */ |
||
229 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
236 | |||
237 | /** |
||
238 | * Set an importing flag to `true` in sync settings. |
||
239 | * |
||
240 | * @access public |
||
241 | * @static |
||
242 | */ |
||
243 | public static function set_is_importing_true() { |
||
246 | |||
247 | /** |
||
248 | * Sends data to WordPress.com via an XMLRPC request. |
||
249 | * |
||
250 | * @access public |
||
251 | * @static |
||
252 | * |
||
253 | * @param object $data Data relating to a sync action. |
||
254 | * @param string $codec_name The name of the codec that encodes the data. |
||
255 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
256 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
257 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
258 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
259 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
260 | */ |
||
261 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration ) { |
||
344 | |||
345 | /** |
||
346 | * Kicks off the initial sync. |
||
347 | * |
||
348 | * @access public |
||
349 | * @static |
||
350 | * |
||
351 | * @return bool|null False if sync is not allowed. |
||
352 | */ |
||
353 | public static function do_initial_sync() { |
||
378 | |||
379 | /** |
||
380 | * Kicks off a full sync. |
||
381 | * |
||
382 | * @access public |
||
383 | * @static |
||
384 | * |
||
385 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
386 | * @return bool True if full sync was successfully started. |
||
387 | */ |
||
388 | public static function do_full_sync( $modules = null ) { |
||
405 | |||
406 | /** |
||
407 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
408 | * |
||
409 | * @access public |
||
410 | * @static |
||
411 | * |
||
412 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
413 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
414 | */ |
||
415 | public static function jetpack_cron_schedule( $schedules ) { |
||
429 | |||
430 | /** |
||
431 | * Starts an incremental sync via cron. |
||
432 | * |
||
433 | * @access public |
||
434 | * @static |
||
435 | */ |
||
436 | public static function do_cron_sync() { |
||
439 | |||
440 | /** |
||
441 | * Starts a full sync via cron. |
||
442 | * |
||
443 | * @access public |
||
444 | * @static |
||
445 | */ |
||
446 | public static function do_cron_full_sync() { |
||
449 | |||
450 | /** |
||
451 | * Try to send actions until we run out of things to send, |
||
452 | * or have to wait more than 15s before sending again, |
||
453 | * or we hit a lock or some other sending issue |
||
454 | * |
||
455 | * @access public |
||
456 | * @static |
||
457 | * |
||
458 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
459 | */ |
||
460 | public static function do_cron_sync_by_type( $type ) { |
||
485 | |||
486 | /** |
||
487 | * Initialize the sync listener. |
||
488 | * |
||
489 | * @access public |
||
490 | * @static |
||
491 | */ |
||
492 | public static function initialize_listener() { |
||
495 | |||
496 | /** |
||
497 | * Initializes the sync sender. |
||
498 | * |
||
499 | * @access public |
||
500 | * @static |
||
501 | */ |
||
502 | public static function initialize_sender() { |
||
506 | |||
507 | /** |
||
508 | * Initializes sync for WooCommerce. |
||
509 | * |
||
510 | * @access public |
||
511 | * @static |
||
512 | */ |
||
513 | public static function initialize_woocommerce() { |
||
519 | |||
520 | /** |
||
521 | * Adds Woo's sync modules to existing modules for sending. |
||
522 | * |
||
523 | * @access public |
||
524 | * @static |
||
525 | * |
||
526 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
527 | * @return array A list of sync modules that now includes Woo's modules. |
||
528 | */ |
||
529 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
533 | |||
534 | /** |
||
535 | * Initializes sync for WP Super Cache. |
||
536 | * |
||
537 | * @access public |
||
538 | * @static |
||
539 | */ |
||
540 | public static function initialize_wp_super_cache() { |
||
546 | |||
547 | /** |
||
548 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
549 | * |
||
550 | * @access public |
||
551 | * @static |
||
552 | * |
||
553 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
554 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
555 | */ |
||
556 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
560 | |||
561 | /** |
||
562 | * Sanitizes the name of sync's cron schedule. |
||
563 | * |
||
564 | * @access public |
||
565 | * @static |
||
566 | * |
||
567 | * @param string $schedule The name of a WordPress cron schedule. |
||
568 | * @return string The sanitized name of sync's cron schedule. |
||
569 | */ |
||
570 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
581 | |||
582 | /** |
||
583 | * Allows offsetting of start times for sync cron jobs. |
||
584 | * |
||
585 | * @access public |
||
586 | * @static |
||
587 | * |
||
588 | * @param string $schedule The name of a cron schedule. |
||
589 | * @param string $hook The hook that this method is responding to. |
||
590 | * @return int The offset for the sync cron schedule. |
||
591 | */ |
||
592 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
616 | |||
617 | /** |
||
618 | * Decides if a sync cron should be scheduled. |
||
619 | * |
||
620 | * @access public |
||
621 | * @static |
||
622 | * |
||
623 | * @param string $schedule The name of a cron schedule. |
||
624 | * @param string $hook The hook that this method is responding to. |
||
625 | */ |
||
626 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
642 | |||
643 | /** |
||
644 | * Clears Jetpack sync cron jobs. |
||
645 | * |
||
646 | * @access public |
||
647 | * @static |
||
648 | */ |
||
649 | public static function clear_sync_cron_jobs() { |
||
653 | |||
654 | /** |
||
655 | * Initializes Jetpack sync cron jobs. |
||
656 | * |
||
657 | * @access public |
||
658 | * @static |
||
659 | */ |
||
660 | public static function init_sync_cron_jobs() { |
||
686 | |||
687 | /** |
||
688 | * Perform maintenance when a plugin upgrade occurs. |
||
689 | * |
||
690 | * @access public |
||
691 | * @static |
||
692 | * |
||
693 | * @param string $new_version New version of the plugin. |
||
694 | * @param string $old_version Old version of the plugin. |
||
695 | */ |
||
696 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
711 | |||
712 | /** |
||
713 | * Get syncing status for the given fields. |
||
714 | * |
||
715 | * @access public |
||
716 | * @static |
||
717 | * |
||
718 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
719 | * @return array An associative array with the status report. |
||
720 | */ |
||
721 | public static function get_sync_status( $fields = null ) { |
||
767 | } |
||
768 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.