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 |
||
20 | class Actions { |
||
21 | /** |
||
22 | * A variable to hold a sync sender object. |
||
23 | * |
||
24 | * @access public |
||
25 | * @static |
||
26 | * |
||
27 | * @var Automattic\Jetpack\Sync\Sender |
||
28 | */ |
||
29 | public static $sender = null; |
||
30 | |||
31 | /** |
||
32 | * A variable to hold a sync listener object. |
||
33 | * |
||
34 | * @access public |
||
35 | * @static |
||
36 | * |
||
37 | * @var Automattic\Jetpack\Sync\Listener |
||
38 | */ |
||
39 | public static $listener = null; |
||
40 | |||
41 | /** |
||
42 | * Name of the sync cron schedule. |
||
43 | * |
||
44 | * @access public |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
49 | |||
50 | /** |
||
51 | * Interval between the last and the next sync cron action. |
||
52 | * |
||
53 | * @access public |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
58 | |||
59 | /** |
||
60 | * Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
||
61 | * and set up a shut-down action for sending actions to WordPress.com |
||
62 | * |
||
63 | * @access public |
||
64 | * @static |
||
65 | */ |
||
66 | public static function init() { |
||
104 | |||
105 | /** |
||
106 | * Prepares sync to send actions on shutdown for the current request. |
||
107 | * |
||
108 | * @access public |
||
109 | * @static |
||
110 | */ |
||
111 | public static function add_sender_shutdown() { |
||
133 | |||
134 | /** |
||
135 | * Define JETPACK_SYNC_READ_ONLY constant if not defined. |
||
136 | * This notifies sync to not run in shutdown if it was initialized during init. |
||
137 | * |
||
138 | * @access public |
||
139 | * @static |
||
140 | */ |
||
141 | public static function mark_sync_read_only() { |
||
144 | |||
145 | /** |
||
146 | * Decides if the sender should run on shutdown for this request. |
||
147 | * |
||
148 | * @access public |
||
149 | * @static |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public static function should_initialize_sender() { |
||
187 | |||
188 | /** |
||
189 | * Decides if the sender should run on shutdown when actions are queued. |
||
190 | * |
||
191 | * @access public |
||
192 | * @static |
||
193 | * |
||
194 | * @param bool $enable Should we initilize sender. |
||
195 | * @return bool |
||
196 | */ |
||
197 | public static function should_initialize_sender_enqueue( $enable ) { |
||
210 | |||
211 | /** |
||
212 | * Decides if sync should run at all during this request. |
||
213 | * |
||
214 | * @access public |
||
215 | * @static |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public static function sync_allowed() { |
||
249 | |||
250 | /** |
||
251 | * Helper function to get details as to why sync is not allowed, if it is not allowed. |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | public static function get_debug_details() { |
||
285 | |||
286 | /** |
||
287 | * Determines if syncing during a cron job is allowed. |
||
288 | * |
||
289 | * @access public |
||
290 | * @static |
||
291 | * |
||
292 | * @return bool|int |
||
293 | */ |
||
294 | public static function sync_via_cron_allowed() { |
||
297 | |||
298 | /** |
||
299 | * Decides if the given post should be Publicized based on its type. |
||
300 | * |
||
301 | * @access public |
||
302 | * @static |
||
303 | * |
||
304 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
305 | * @param \WP_Post $post The post to test for Publicizability. |
||
306 | * @return bool |
||
307 | */ |
||
308 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
315 | |||
316 | /** |
||
317 | * Set an importing flag to `true` in sync settings. |
||
318 | * |
||
319 | * @access public |
||
320 | * @static |
||
321 | */ |
||
322 | public static function set_is_importing_true() { |
||
325 | |||
326 | /** |
||
327 | * Sends data to WordPress.com via an XMLRPC request. |
||
328 | * |
||
329 | * @access public |
||
330 | * @static |
||
331 | * |
||
332 | * @param object $data Data relating to a sync action. |
||
333 | * @param string $codec_name The name of the codec that encodes the data. |
||
334 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
335 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
336 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
337 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
338 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
339 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
340 | * @return mixed|WP_Error The result of the sending request. |
||
341 | */ |
||
342 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
428 | |||
429 | /** |
||
430 | * Kicks off the initial sync. |
||
431 | * |
||
432 | * @access public |
||
433 | * @static |
||
434 | * |
||
435 | * @return bool|null False if sync is not allowed. |
||
436 | */ |
||
437 | public static function do_initial_sync() { |
||
459 | |||
460 | /** |
||
461 | * Kicks off a full sync. |
||
462 | * |
||
463 | * @access public |
||
464 | * @static |
||
465 | * |
||
466 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
467 | * @return bool True if full sync was successfully started. |
||
468 | */ |
||
469 | public static function do_full_sync( $modules = null ) { |
||
486 | |||
487 | /** |
||
488 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
489 | * |
||
490 | * @access public |
||
491 | * @static |
||
492 | * |
||
493 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
494 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
495 | */ |
||
496 | public static function jetpack_cron_schedule( $schedules ) { |
||
510 | |||
511 | /** |
||
512 | * Starts an incremental sync via cron. |
||
513 | * |
||
514 | * @access public |
||
515 | * @static |
||
516 | */ |
||
517 | public static function do_cron_sync() { |
||
520 | |||
521 | /** |
||
522 | * Starts a full sync via cron. |
||
523 | * |
||
524 | * @access public |
||
525 | * @static |
||
526 | */ |
||
527 | public static function do_cron_full_sync() { |
||
530 | |||
531 | /** |
||
532 | * Try to send actions until we run out of things to send, |
||
533 | * or have to wait more than 15s before sending again, |
||
534 | * or we hit a lock or some other sending issue |
||
535 | * |
||
536 | * @access public |
||
537 | * @static |
||
538 | * |
||
539 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
540 | */ |
||
541 | public static function do_cron_sync_by_type( $type ) { |
||
579 | |||
580 | /** |
||
581 | * Initialize the sync listener. |
||
582 | * |
||
583 | * @access public |
||
584 | * @static |
||
585 | */ |
||
586 | public static function initialize_listener() { |
||
589 | |||
590 | /** |
||
591 | * Initializes the sync sender. |
||
592 | * |
||
593 | * @access public |
||
594 | * @static |
||
595 | */ |
||
596 | public static function initialize_sender() { |
||
600 | |||
601 | /** |
||
602 | * Initializes sync for WooCommerce. |
||
603 | * |
||
604 | * @access public |
||
605 | * @static |
||
606 | */ |
||
607 | public static function initialize_woocommerce() { |
||
613 | |||
614 | /** |
||
615 | * Adds Woo's sync modules to existing modules for sending. |
||
616 | * |
||
617 | * @access public |
||
618 | * @static |
||
619 | * |
||
620 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
621 | * @return array A list of sync modules that now includes Woo's modules. |
||
622 | */ |
||
623 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
627 | |||
628 | /** |
||
629 | * Initializes sync for WP Super Cache. |
||
630 | * |
||
631 | * @access public |
||
632 | * @static |
||
633 | */ |
||
634 | public static function initialize_wp_super_cache() { |
||
640 | |||
641 | /** |
||
642 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
643 | * |
||
644 | * @access public |
||
645 | * @static |
||
646 | * |
||
647 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
648 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
649 | */ |
||
650 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
654 | |||
655 | /** |
||
656 | * Sanitizes the name of sync's cron schedule. |
||
657 | * |
||
658 | * @access public |
||
659 | * @static |
||
660 | * |
||
661 | * @param string $schedule The name of a WordPress cron schedule. |
||
662 | * @return string The sanitized name of sync's cron schedule. |
||
663 | */ |
||
664 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
675 | |||
676 | /** |
||
677 | * Allows offsetting of start times for sync cron jobs. |
||
678 | * |
||
679 | * @access public |
||
680 | * @static |
||
681 | * |
||
682 | * @param string $schedule The name of a cron schedule. |
||
683 | * @param string $hook The hook that this method is responding to. |
||
684 | * @return int The offset for the sync cron schedule. |
||
685 | */ |
||
686 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
708 | |||
709 | /** |
||
710 | * Decides if a sync cron should be scheduled. |
||
711 | * |
||
712 | * @access public |
||
713 | * @static |
||
714 | * |
||
715 | * @param string $schedule The name of a cron schedule. |
||
716 | * @param string $hook The hook that this method is responding to. |
||
717 | */ |
||
718 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
734 | |||
735 | /** |
||
736 | * Clears Jetpack sync cron jobs. |
||
737 | * |
||
738 | * @access public |
||
739 | * @static |
||
740 | */ |
||
741 | public static function clear_sync_cron_jobs() { |
||
745 | |||
746 | /** |
||
747 | * Initializes Jetpack sync cron jobs. |
||
748 | * |
||
749 | * @access public |
||
750 | * @static |
||
751 | */ |
||
752 | public static function init_sync_cron_jobs() { |
||
778 | |||
779 | /** |
||
780 | * Perform maintenance when a plugin upgrade occurs. |
||
781 | * |
||
782 | * @access public |
||
783 | * @static |
||
784 | * |
||
785 | * @param string $new_version New version of the plugin. |
||
786 | * @param string $old_version Old version of the plugin. |
||
787 | */ |
||
788 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
805 | |||
806 | /** |
||
807 | * Get syncing status for the given fields. |
||
808 | * |
||
809 | * @access public |
||
810 | * @static |
||
811 | * |
||
812 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
813 | * @return array An associative array with the status report. |
||
814 | */ |
||
815 | public static function get_sync_status( $fields = null ) { |
||
875 | } |
||
876 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: