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 | * @return bool |
||
| 195 | */ |
||
| 196 | public static function should_initialize_sender_enqueue() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Decides if sync should run at all during this request. |
||
| 206 | * |
||
| 207 | * @access public |
||
| 208 | * @static |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public static function sync_allowed() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Helper function to get details as to why sync is not allowed, if it is not allowed. |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public static function get_debug_details() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Determines if syncing during a cron job is allowed. |
||
| 281 | * |
||
| 282 | * @access public |
||
| 283 | * @static |
||
| 284 | * |
||
| 285 | * @return bool|int |
||
| 286 | */ |
||
| 287 | public static function sync_via_cron_allowed() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Decides if the given post should be Publicized based on its type. |
||
| 293 | * |
||
| 294 | * @access public |
||
| 295 | * @static |
||
| 296 | * |
||
| 297 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
| 298 | * @param \WP_Post $post The post to test for Publicizability. |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Set an importing flag to `true` in sync settings. |
||
| 311 | * |
||
| 312 | * @access public |
||
| 313 | * @static |
||
| 314 | */ |
||
| 315 | public static function set_is_importing_true() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Sends data to WordPress.com via an XMLRPC request. |
||
| 321 | * |
||
| 322 | * @access public |
||
| 323 | * @static |
||
| 324 | * |
||
| 325 | * @param object $data Data relating to a sync action. |
||
| 326 | * @param string $codec_name The name of the codec that encodes the data. |
||
| 327 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
| 328 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
| 329 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
| 330 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
| 331 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
| 332 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
| 333 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
| 334 | */ |
||
| 335 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Kicks off the initial sync. |
||
| 424 | * |
||
| 425 | * @access public |
||
| 426 | * @static |
||
| 427 | * |
||
| 428 | * @return bool|null False if sync is not allowed. |
||
| 429 | */ |
||
| 430 | public static function do_initial_sync() { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Kicks off a full sync. |
||
| 455 | * |
||
| 456 | * @access public |
||
| 457 | * @static |
||
| 458 | * |
||
| 459 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
| 460 | * @return bool True if full sync was successfully started. |
||
| 461 | */ |
||
| 462 | public static function do_full_sync( $modules = null ) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
| 482 | * |
||
| 483 | * @access public |
||
| 484 | * @static |
||
| 485 | * |
||
| 486 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
| 487 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
| 488 | */ |
||
| 489 | public static function jetpack_cron_schedule( $schedules ) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Starts an incremental sync via cron. |
||
| 506 | * |
||
| 507 | * @access public |
||
| 508 | * @static |
||
| 509 | */ |
||
| 510 | public static function do_cron_sync() { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Starts a full sync via cron. |
||
| 516 | * |
||
| 517 | * @access public |
||
| 518 | * @static |
||
| 519 | */ |
||
| 520 | public static function do_cron_full_sync() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Try to send actions until we run out of things to send, |
||
| 526 | * or have to wait more than 15s before sending again, |
||
| 527 | * or we hit a lock or some other sending issue |
||
| 528 | * |
||
| 529 | * @access public |
||
| 530 | * @static |
||
| 531 | * |
||
| 532 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
| 533 | */ |
||
| 534 | public static function do_cron_sync_by_type( $type ) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Initialize the sync listener. |
||
| 575 | * |
||
| 576 | * @access public |
||
| 577 | * @static |
||
| 578 | */ |
||
| 579 | public static function initialize_listener() { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Initializes the sync sender. |
||
| 585 | * |
||
| 586 | * @access public |
||
| 587 | * @static |
||
| 588 | */ |
||
| 589 | public static function initialize_sender() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Initializes sync for WooCommerce. |
||
| 596 | * |
||
| 597 | * @access public |
||
| 598 | * @static |
||
| 599 | */ |
||
| 600 | public static function initialize_woocommerce() { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Adds Woo's sync modules to existing modules for sending. |
||
| 609 | * |
||
| 610 | * @access public |
||
| 611 | * @static |
||
| 612 | * |
||
| 613 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
| 614 | * @return array A list of sync modules that now includes Woo's modules. |
||
| 615 | */ |
||
| 616 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Initializes sync for WP Super Cache. |
||
| 623 | * |
||
| 624 | * @access public |
||
| 625 | * @static |
||
| 626 | */ |
||
| 627 | public static function initialize_wp_super_cache() { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
| 636 | * |
||
| 637 | * @access public |
||
| 638 | * @static |
||
| 639 | * |
||
| 640 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
| 641 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
| 642 | */ |
||
| 643 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Sanitizes the name of sync's cron schedule. |
||
| 650 | * |
||
| 651 | * @access public |
||
| 652 | * @static |
||
| 653 | * |
||
| 654 | * @param string $schedule The name of a WordPress cron schedule. |
||
| 655 | * @return string The sanitized name of sync's cron schedule. |
||
| 656 | */ |
||
| 657 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Allows offsetting of start times for sync cron jobs. |
||
| 671 | * |
||
| 672 | * @access public |
||
| 673 | * @static |
||
| 674 | * |
||
| 675 | * @param string $schedule The name of a cron schedule. |
||
| 676 | * @param string $hook The hook that this method is responding to. |
||
| 677 | * @return int The offset for the sync cron schedule. |
||
| 678 | */ |
||
| 679 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Decides if a sync cron should be scheduled. |
||
| 706 | * |
||
| 707 | * @access public |
||
| 708 | * @static |
||
| 709 | * |
||
| 710 | * @param string $schedule The name of a cron schedule. |
||
| 711 | * @param string $hook The hook that this method is responding to. |
||
| 712 | */ |
||
| 713 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Clears Jetpack sync cron jobs. |
||
| 732 | * |
||
| 733 | * @access public |
||
| 734 | * @static |
||
| 735 | */ |
||
| 736 | public static function clear_sync_cron_jobs() { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Initializes Jetpack sync cron jobs. |
||
| 743 | * |
||
| 744 | * @access public |
||
| 745 | * @static |
||
| 746 | */ |
||
| 747 | public static function init_sync_cron_jobs() { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Perform maintenance when a plugin upgrade occurs. |
||
| 776 | * |
||
| 777 | * @access public |
||
| 778 | * @static |
||
| 779 | * |
||
| 780 | * @param string $new_version New version of the plugin. |
||
| 781 | * @param string $old_version Old version of the plugin. |
||
| 782 | */ |
||
| 783 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Get syncing status for the given fields. |
||
| 803 | * |
||
| 804 | * @access public |
||
| 805 | * @static |
||
| 806 | * |
||
| 807 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
| 808 | * @return array An associative array with the status report. |
||
| 809 | */ |
||
| 810 | public static function get_sync_status( $fields = null ) { |
||
| 870 | } |
||
| 871 |
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: