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 | * Helper function to get details as to why sync is not allowed, if it is not allowed. |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public static function get_debug_details() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Determines if syncing during a cron job is allowed. |
||
| 265 | * |
||
| 266 | * @access public |
||
| 267 | * @static |
||
| 268 | * |
||
| 269 | * @return bool|int |
||
| 270 | */ |
||
| 271 | public static function sync_via_cron_allowed() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Decides if the given post should be Publicized based on its type. |
||
| 277 | * |
||
| 278 | * @access public |
||
| 279 | * @static |
||
| 280 | * |
||
| 281 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
| 282 | * @param \WP_Post $post The post to test for Publicizability. |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Set an importing flag to `true` in sync settings. |
||
| 295 | * |
||
| 296 | * @access public |
||
| 297 | * @static |
||
| 298 | */ |
||
| 299 | public static function set_is_importing_true() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Sends data to WordPress.com via an XMLRPC request. |
||
| 305 | * |
||
| 306 | * @access public |
||
| 307 | * @static |
||
| 308 | * |
||
| 309 | * @param object $data Data relating to a sync action. |
||
| 310 | * @param string $codec_name The name of the codec that encodes the data. |
||
| 311 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
| 312 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
| 313 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
| 314 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
| 315 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
|
|
|||
| 316 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
| 317 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
| 318 | */ |
||
| 319 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Kicks off the initial sync. |
||
| 408 | * |
||
| 409 | * @access public |
||
| 410 | * @static |
||
| 411 | * |
||
| 412 | * @return bool|null False if sync is not allowed. |
||
| 413 | */ |
||
| 414 | public static function do_initial_sync() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Kicks off a full sync. |
||
| 439 | * |
||
| 440 | * @access public |
||
| 441 | * @static |
||
| 442 | * |
||
| 443 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
| 444 | * @return bool True if full sync was successfully started. |
||
| 445 | */ |
||
| 446 | public static function do_full_sync( $modules = null ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
| 466 | * |
||
| 467 | * @access public |
||
| 468 | * @static |
||
| 469 | * |
||
| 470 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
| 471 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
| 472 | */ |
||
| 473 | public static function jetpack_cron_schedule( $schedules ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Starts an incremental sync via cron. |
||
| 490 | * |
||
| 491 | * @access public |
||
| 492 | * @static |
||
| 493 | */ |
||
| 494 | public static function do_cron_sync() { |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Starts a full sync via cron. |
||
| 500 | * |
||
| 501 | * @access public |
||
| 502 | * @static |
||
| 503 | */ |
||
| 504 | public static function do_cron_full_sync() { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Try to send actions until we run out of things to send, |
||
| 510 | * or have to wait more than 15s before sending again, |
||
| 511 | * or we hit a lock or some other sending issue |
||
| 512 | * |
||
| 513 | * @access public |
||
| 514 | * @static |
||
| 515 | * |
||
| 516 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
| 517 | */ |
||
| 518 | public static function do_cron_sync_by_type( $type ) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Initialize the sync listener. |
||
| 559 | * |
||
| 560 | * @access public |
||
| 561 | * @static |
||
| 562 | */ |
||
| 563 | public static function initialize_listener() { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Initializes the sync sender. |
||
| 569 | * |
||
| 570 | * @access public |
||
| 571 | * @static |
||
| 572 | */ |
||
| 573 | public static function initialize_sender() { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Initializes sync for WooCommerce. |
||
| 580 | * |
||
| 581 | * @access public |
||
| 582 | * @static |
||
| 583 | */ |
||
| 584 | public static function initialize_woocommerce() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Adds Woo's sync modules to existing modules for sending. |
||
| 593 | * |
||
| 594 | * @access public |
||
| 595 | * @static |
||
| 596 | * |
||
| 597 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
| 598 | * @return array A list of sync modules that now includes Woo's modules. |
||
| 599 | */ |
||
| 600 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Initializes sync for WP Super Cache. |
||
| 607 | * |
||
| 608 | * @access public |
||
| 609 | * @static |
||
| 610 | */ |
||
| 611 | public static function initialize_wp_super_cache() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
| 620 | * |
||
| 621 | * @access public |
||
| 622 | * @static |
||
| 623 | * |
||
| 624 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
| 625 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
| 626 | */ |
||
| 627 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Sanitizes the name of sync's cron schedule. |
||
| 634 | * |
||
| 635 | * @access public |
||
| 636 | * @static |
||
| 637 | * |
||
| 638 | * @param string $schedule The name of a WordPress cron schedule. |
||
| 639 | * @return string The sanitized name of sync's cron schedule. |
||
| 640 | */ |
||
| 641 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Allows offsetting of start times for sync cron jobs. |
||
| 655 | * |
||
| 656 | * @access public |
||
| 657 | * @static |
||
| 658 | * |
||
| 659 | * @param string $schedule The name of a cron schedule. |
||
| 660 | * @param string $hook The hook that this method is responding to. |
||
| 661 | * @return int The offset for the sync cron schedule. |
||
| 662 | */ |
||
| 663 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Decides if a sync cron should be scheduled. |
||
| 690 | * |
||
| 691 | * @access public |
||
| 692 | * @static |
||
| 693 | * |
||
| 694 | * @param string $schedule The name of a cron schedule. |
||
| 695 | * @param string $hook The hook that this method is responding to. |
||
| 696 | */ |
||
| 697 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Clears Jetpack sync cron jobs. |
||
| 716 | * |
||
| 717 | * @access public |
||
| 718 | * @static |
||
| 719 | */ |
||
| 720 | public static function clear_sync_cron_jobs() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Initializes Jetpack sync cron jobs. |
||
| 727 | * |
||
| 728 | * @access public |
||
| 729 | * @static |
||
| 730 | */ |
||
| 731 | public static function init_sync_cron_jobs() { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Perform maintenance when a plugin upgrade occurs. |
||
| 760 | * |
||
| 761 | * @access public |
||
| 762 | * @static |
||
| 763 | * |
||
| 764 | * @param string $new_version New version of the plugin. |
||
| 765 | * @param string $old_version Old version of the plugin. |
||
| 766 | */ |
||
| 767 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Get syncing status for the given fields. |
||
| 787 | * |
||
| 788 | * @access public |
||
| 789 | * @static |
||
| 790 | * |
||
| 791 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
| 792 | * @return array An associative array with the status report. |
||
| 793 | */ |
||
| 794 | public static function get_sync_status( $fields = null ) { |
||
| 854 | } |
||
| 855 |
This check looks for
@paramannotations 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.