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 | /** |
||
| 23 | * Name of the retry-after option prefix |
||
| 24 | * |
||
| 25 | * @access public |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | const RETRY_AFTER_PREFIX = 'jp_sync_retry_after_'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * A variable to hold a sync sender object. |
||
| 33 | * |
||
| 34 | * @access public |
||
| 35 | * @static |
||
| 36 | * |
||
| 37 | * @var Automattic\Jetpack\Sync\Sender |
||
| 38 | */ |
||
| 39 | public static $sender = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A variable to hold a sync listener object. |
||
| 43 | * |
||
| 44 | * @access public |
||
| 45 | * @static |
||
| 46 | * |
||
| 47 | * @var Automattic\Jetpack\Sync\Listener |
||
| 48 | */ |
||
| 49 | public static $listener = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Name of the sync cron schedule. |
||
| 53 | * |
||
| 54 | * @access public |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const DEFAULT_SYNC_CRON_INTERVAL_NAME = 'jetpack_sync_interval'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Interval between the last and the next sync cron action. |
||
| 62 | * |
||
| 63 | * @access public |
||
| 64 | * |
||
| 65 | * @var int |
||
| 66 | */ |
||
| 67 | const DEFAULT_SYNC_CRON_INTERVAL_VALUE = 300; // 5 * MINUTE_IN_SECONDS; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initialize Sync for cron jobs, set up listeners for WordPress Actions, |
||
| 71 | * and set up a shut-down action for sending actions to WordPress.com |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * @static |
||
| 75 | */ |
||
| 76 | public static function init() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Prepares sync to send actions on shutdown for the current request. |
||
| 117 | * |
||
| 118 | * @access public |
||
| 119 | * @static |
||
| 120 | */ |
||
| 121 | public static function add_sender_shutdown() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Define JETPACK_SYNC_READ_ONLY constant if not defined. |
||
| 146 | * This notifies sync to not run in shutdown if it was initialized during init. |
||
| 147 | * |
||
| 148 | * @access public |
||
| 149 | * @static |
||
| 150 | */ |
||
| 151 | public static function mark_sync_read_only() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Decides if the sender should run on shutdown for this request. |
||
| 157 | * |
||
| 158 | * @access public |
||
| 159 | * @static |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public static function should_initialize_sender() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Decides if the sender should run on shutdown when actions are queued. |
||
| 200 | * |
||
| 201 | * @access public |
||
| 202 | * @static |
||
| 203 | * |
||
| 204 | * @param bool $enable Should we initilize sender. |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | public static function should_initialize_sender_enqueue( $enable ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Decides if sync should run at all during this request. |
||
| 223 | * |
||
| 224 | * @access public |
||
| 225 | * @static |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public static function sync_allowed() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Helper function to get details as to why sync is not allowed, if it is not allowed. |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public static function get_debug_details() { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Determines if syncing during a cron job is allowed. |
||
| 298 | * |
||
| 299 | * @access public |
||
| 300 | * @static |
||
| 301 | * |
||
| 302 | * @return bool|int |
||
| 303 | */ |
||
| 304 | public static function sync_via_cron_allowed() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Decides if the given post should be Publicized based on its type. |
||
| 310 | * |
||
| 311 | * @access public |
||
| 312 | * @static |
||
| 313 | * |
||
| 314 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
| 315 | * @param \WP_Post $post The post to test for Publicizability. |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set an importing flag to `true` in sync settings. |
||
| 328 | * |
||
| 329 | * @access public |
||
| 330 | * @static |
||
| 331 | */ |
||
| 332 | public static function set_is_importing_true() { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sends data to WordPress.com via an XMLRPC request. |
||
| 338 | * |
||
| 339 | * @access public |
||
| 340 | * @static |
||
| 341 | * |
||
| 342 | * @param object $data Data relating to a sync action. |
||
| 343 | * @param string $codec_name The name of the codec that encodes the data. |
||
| 344 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
| 345 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
| 346 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
| 347 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
| 348 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
| 349 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
| 350 | * @return mixed|WP_Error The result of the sending request. |
||
| 351 | */ |
||
| 352 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Kicks off the initial sync. |
||
| 455 | * |
||
| 456 | * @access public |
||
| 457 | * @static |
||
| 458 | * |
||
| 459 | * @return bool|null False if sync is not allowed. |
||
| 460 | */ |
||
| 461 | public static function do_initial_sync() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Kicks off a full sync. |
||
| 486 | * |
||
| 487 | * @access public |
||
| 488 | * @static |
||
| 489 | * |
||
| 490 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
| 491 | * @return bool True if full sync was successfully started. |
||
| 492 | */ |
||
| 493 | public static function do_full_sync( $modules = null ) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
| 513 | * |
||
| 514 | * @access public |
||
| 515 | * @static |
||
| 516 | * |
||
| 517 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
| 518 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
| 519 | */ |
||
| 520 | public static function jetpack_cron_schedule( $schedules ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Starts an incremental sync via cron. |
||
| 537 | * |
||
| 538 | * @access public |
||
| 539 | * @static |
||
| 540 | */ |
||
| 541 | public static function do_cron_sync() { |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Starts a full sync via cron. |
||
| 547 | * |
||
| 548 | * @access public |
||
| 549 | * @static |
||
| 550 | */ |
||
| 551 | public static function do_cron_full_sync() { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Try to send actions until we run out of things to send, |
||
| 557 | * or have to wait more than 15s before sending again, |
||
| 558 | * or we hit a lock or some other sending issue |
||
| 559 | * |
||
| 560 | * @access public |
||
| 561 | * @static |
||
| 562 | * |
||
| 563 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
| 564 | */ |
||
| 565 | public static function do_cron_sync_by_type( $type ) { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Initialize the sync listener. |
||
| 606 | * |
||
| 607 | * @access public |
||
| 608 | * @static |
||
| 609 | */ |
||
| 610 | public static function initialize_listener() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Initializes the sync sender. |
||
| 616 | * |
||
| 617 | * @access public |
||
| 618 | * @static |
||
| 619 | */ |
||
| 620 | public static function initialize_sender() { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Initializes sync for WooCommerce. |
||
| 627 | * |
||
| 628 | * @access public |
||
| 629 | * @static |
||
| 630 | */ |
||
| 631 | public static function initialize_woocommerce() { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Adds Woo's sync modules to existing modules for sending. |
||
| 640 | * |
||
| 641 | * @access public |
||
| 642 | * @static |
||
| 643 | * |
||
| 644 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
| 645 | * @return array A list of sync modules that now includes Woo's modules. |
||
| 646 | */ |
||
| 647 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Initializes sync for WP Super Cache. |
||
| 654 | * |
||
| 655 | * @access public |
||
| 656 | * @static |
||
| 657 | */ |
||
| 658 | public static function initialize_wp_super_cache() { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
| 667 | * |
||
| 668 | * @access public |
||
| 669 | * @static |
||
| 670 | * |
||
| 671 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
| 672 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
| 673 | */ |
||
| 674 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Sanitizes the name of sync's cron schedule. |
||
| 681 | * |
||
| 682 | * @access public |
||
| 683 | * @static |
||
| 684 | * |
||
| 685 | * @param string $schedule The name of a WordPress cron schedule. |
||
| 686 | * @return string The sanitized name of sync's cron schedule. |
||
| 687 | */ |
||
| 688 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Allows offsetting of start times for sync cron jobs. |
||
| 702 | * |
||
| 703 | * @access public |
||
| 704 | * @static |
||
| 705 | * |
||
| 706 | * @param string $schedule The name of a cron schedule. |
||
| 707 | * @param string $hook The hook that this method is responding to. |
||
| 708 | * @return int The offset for the sync cron schedule. |
||
| 709 | */ |
||
| 710 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Decides if a sync cron should be scheduled. |
||
| 735 | * |
||
| 736 | * @access public |
||
| 737 | * @static |
||
| 738 | * |
||
| 739 | * @param string $schedule The name of a cron schedule. |
||
| 740 | * @param string $hook The hook that this method is responding to. |
||
| 741 | */ |
||
| 742 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Clears Jetpack sync cron jobs. |
||
| 761 | * |
||
| 762 | * @access public |
||
| 763 | * @static |
||
| 764 | */ |
||
| 765 | public static function clear_sync_cron_jobs() { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Initializes Jetpack sync cron jobs. |
||
| 772 | * |
||
| 773 | * @access public |
||
| 774 | * @static |
||
| 775 | */ |
||
| 776 | public static function init_sync_cron_jobs() { |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Perform maintenance when a plugin upgrade occurs. |
||
| 805 | * |
||
| 806 | * @access public |
||
| 807 | * @static |
||
| 808 | * |
||
| 809 | * @param string $new_version New version of the plugin. |
||
| 810 | * @param string $old_version Old version of the plugin. |
||
| 811 | */ |
||
| 812 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Get syncing status for the given fields. |
||
| 832 | * |
||
| 833 | * @access public |
||
| 834 | * @static |
||
| 835 | * |
||
| 836 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
| 837 | * @return array An associative array with the status report. |
||
| 838 | */ |
||
| 839 | public static function get_sync_status( $fields = null ) { |
||
| 899 | } |
||
| 900 |
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: