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 | * Determines if syncing during a cron job is allowed. |
||
| 229 | * |
||
| 230 | * @access public |
||
| 231 | * @static |
||
| 232 | * |
||
| 233 | * @return bool|int |
||
| 234 | */ |
||
| 235 | public static function sync_via_cron_allowed() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Decides if the given post should be Publicized based on its type. |
||
| 241 | * |
||
| 242 | * @access public |
||
| 243 | * @static |
||
| 244 | * |
||
| 245 | * @param bool $should_publicize Publicize status prior to this filter running. |
||
| 246 | * @param \WP_Post $post The post to test for Publicizability. |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public static function prevent_publicize_blacklisted_posts( $should_publicize, $post ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set an importing flag to `true` in sync settings. |
||
| 259 | * |
||
| 260 | * @access public |
||
| 261 | * @static |
||
| 262 | */ |
||
| 263 | public static function set_is_importing_true() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Sends data to WordPress.com via an XMLRPC request. |
||
| 269 | * |
||
| 270 | * @access public |
||
| 271 | * @static |
||
| 272 | * |
||
| 273 | * @param object $data Data relating to a sync action. |
||
| 274 | * @param string $codec_name The name of the codec that encodes the data. |
||
| 275 | * @param float $sent_timestamp Current server time so we can compensate for clock differences. |
||
| 276 | * @param string $queue_id The queue the action belongs to, sync or full_sync. |
||
| 277 | * @param float $checkout_duration Time spent retrieving queue items from the DB. |
||
| 278 | * @param float $preprocess_duration Time spent converting queue items into data to send. |
||
| 279 | * @param int $queue_size The size of the sync queue at the time of processing. |
||
|
|
|||
| 280 | * @param string $buffer_id The ID of the Queue buffer checked out for processing. |
||
| 281 | * @return Jetpack_Error|mixed|WP_Error The result of the sending request. |
||
| 282 | */ |
||
| 283 | public static function send_data( $data, $codec_name, $sent_timestamp, $queue_id, $checkout_duration, $preprocess_duration, $queue_size = null, $buffer_id = null ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Kicks off the initial sync. |
||
| 371 | * |
||
| 372 | * @access public |
||
| 373 | * @static |
||
| 374 | * |
||
| 375 | * @return bool|null False if sync is not allowed. |
||
| 376 | */ |
||
| 377 | public static function do_initial_sync() { |
||
| 378 | // Lets not sync if we are not suppose to. |
||
| 379 | if ( ! self::sync_allowed() ) { |
||
| 380 | return false; |
||
| 381 | } |
||
| 382 | |||
| 383 | // Don't start new sync if a full sync is in process. |
||
| 384 | $full_sync_module = Modules::get_module( 'full-sync' ); |
||
| 385 | if ( $full_sync_module && $full_sync_module->is_started() && ! $full_sync_module->is_finished() ) { |
||
| 386 | return false; |
||
| 387 | } |
||
| 388 | |||
| 389 | $initial_sync_config = array( |
||
| 390 | 'options' => true, |
||
| 391 | 'functions' => true, |
||
| 392 | 'constants' => true, |
||
| 393 | 'users' => array( get_current_user_id() ), |
||
| 394 | 'network_options' => true, |
||
| 395 | ); |
||
| 396 | |||
| 397 | self::do_full_sync( $initial_sync_config ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Kicks off a full sync. |
||
| 402 | * |
||
| 403 | * @access public |
||
| 404 | * @static |
||
| 405 | * |
||
| 406 | * @param array $modules The sync modules should be included in this full sync. All will be included if null. |
||
| 407 | * @return bool True if full sync was successfully started. |
||
| 408 | */ |
||
| 409 | public static function do_full_sync( $modules = null ) { |
||
| 410 | if ( ! self::sync_allowed() ) { |
||
| 411 | return false; |
||
| 412 | } |
||
| 413 | |||
| 414 | $full_sync_module = Modules::get_module( 'full-sync' ); |
||
| 415 | |||
| 416 | if ( ! $full_sync_module ) { |
||
| 417 | return false; |
||
| 418 | } |
||
| 419 | |||
| 420 | self::initialize_listener(); |
||
| 421 | |||
| 422 | $full_sync_module->start( $modules ); |
||
| 423 | |||
| 424 | return true; |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Adds a cron schedule for regular syncing via cron, unless the schedule already exists. |
||
| 429 | * |
||
| 430 | * @access public |
||
| 431 | * @static |
||
| 432 | * |
||
| 433 | * @param array $schedules The list of WordPress cron schedules prior to this filter. |
||
| 434 | * @return array A list of WordPress cron schedules with the Jetpack sync interval added. |
||
| 435 | */ |
||
| 436 | public static function jetpack_cron_schedule( $schedules ) { |
||
| 437 | if ( ! isset( $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] ) ) { |
||
| 438 | $minutes = intval( self::DEFAULT_SYNC_CRON_INTERVAL_VALUE / 60 ); |
||
| 439 | $display = ( 1 === $minutes ) ? |
||
| 440 | __( 'Every minute', 'jetpack' ) : |
||
| 441 | /* translators: %d is an integer indicating the number of minutes. */ |
||
| 442 | sprintf( __( 'Every %d minutes', 'jetpack' ), $minutes ); |
||
| 443 | $schedules[ self::DEFAULT_SYNC_CRON_INTERVAL_NAME ] = array( |
||
| 444 | 'interval' => self::DEFAULT_SYNC_CRON_INTERVAL_VALUE, |
||
| 445 | 'display' => $display, |
||
| 446 | ); |
||
| 447 | } |
||
| 448 | return $schedules; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Starts an incremental sync via cron. |
||
| 453 | * |
||
| 454 | * @access public |
||
| 455 | * @static |
||
| 456 | */ |
||
| 457 | public static function do_cron_sync() { |
||
| 458 | self::do_cron_sync_by_type( 'sync' ); |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Starts a full sync via cron. |
||
| 463 | * |
||
| 464 | * @access public |
||
| 465 | * @static |
||
| 466 | */ |
||
| 467 | public static function do_cron_full_sync() { |
||
| 468 | self::do_cron_sync_by_type( 'full_sync' ); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Try to send actions until we run out of things to send, |
||
| 473 | * or have to wait more than 15s before sending again, |
||
| 474 | * or we hit a lock or some other sending issue |
||
| 475 | * |
||
| 476 | * @access public |
||
| 477 | * @static |
||
| 478 | * |
||
| 479 | * @param string $type Sync type. Can be `sync` or `full_sync`. |
||
| 480 | */ |
||
| 481 | public static function do_cron_sync_by_type( $type ) { |
||
| 482 | if ( ! self::sync_allowed() || ( 'sync' !== $type && 'full_sync' !== $type ) ) { |
||
| 483 | return; |
||
| 484 | } |
||
| 485 | |||
| 486 | self::initialize_sender(); |
||
| 487 | |||
| 488 | $time_limit = Settings::get_setting( 'cron_sync_time_limit' ); |
||
| 489 | $start_time = time(); |
||
| 490 | $executions = 0; |
||
| 491 | |||
| 492 | do { |
||
| 493 | $next_sync_time = self::$sender->get_next_sync_time( $type ); |
||
| 494 | |||
| 495 | if ( $next_sync_time ) { |
||
| 496 | $delay = $next_sync_time - time() + 1; |
||
| 497 | if ( $delay > 15 ) { |
||
| 498 | break; |
||
| 499 | } elseif ( $delay > 0 ) { |
||
| 500 | sleep( $delay ); |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | // Explicitly only allow 1 do_full_sync call until issue with Immediate Full Sync is resolved. |
||
| 505 | // For more context see p1HpG7-9pe-p2. |
||
| 506 | if ( 'full_sync' === $type && $executions >= 1 ) { |
||
| 507 | break; |
||
| 508 | } |
||
| 509 | |||
| 510 | $result = 'full_sync' === $type ? self::$sender->do_full_sync() : self::$sender->do_sync(); |
||
| 511 | |||
| 512 | // # of send actions performed. |
||
| 513 | $executions ++; |
||
| 514 | |||
| 515 | } while ( $result && ! is_wp_error( $result ) && ( $start_time + $time_limit ) > time() ); |
||
| 516 | |||
| 517 | return $executions; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Initialize the sync listener. |
||
| 522 | * |
||
| 523 | * @access public |
||
| 524 | * @static |
||
| 525 | */ |
||
| 526 | public static function initialize_listener() { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Initializes the sync sender. |
||
| 532 | * |
||
| 533 | * @access public |
||
| 534 | * @static |
||
| 535 | */ |
||
| 536 | public static function initialize_sender() { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Initializes sync for WooCommerce. |
||
| 543 | * |
||
| 544 | * @access public |
||
| 545 | * @static |
||
| 546 | */ |
||
| 547 | public static function initialize_woocommerce() { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Adds Woo's sync modules to existing modules for sending. |
||
| 556 | * |
||
| 557 | * @access public |
||
| 558 | * @static |
||
| 559 | * |
||
| 560 | * @param array $sync_modules The list of sync modules declared prior to this filter. |
||
| 561 | * @return array A list of sync modules that now includes Woo's modules. |
||
| 562 | */ |
||
| 563 | public static function add_woocommerce_sync_module( $sync_modules ) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Initializes sync for WP Super Cache. |
||
| 570 | * |
||
| 571 | * @access public |
||
| 572 | * @static |
||
| 573 | */ |
||
| 574 | public static function initialize_wp_super_cache() { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Adds WP Super Cache's sync modules to existing modules for sending. |
||
| 583 | * |
||
| 584 | * @access public |
||
| 585 | * @static |
||
| 586 | * |
||
| 587 | * @param array $sync_modules The list of sync modules declared prior to this filer. |
||
| 588 | * @return array A list of sync modules that now includes WP Super Cache's modules. |
||
| 589 | */ |
||
| 590 | public static function add_wp_super_cache_sync_module( $sync_modules ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Sanitizes the name of sync's cron schedule. |
||
| 597 | * |
||
| 598 | * @access public |
||
| 599 | * @static |
||
| 600 | * |
||
| 601 | * @param string $schedule The name of a WordPress cron schedule. |
||
| 602 | * @return string The sanitized name of sync's cron schedule. |
||
| 603 | */ |
||
| 604 | public static function sanitize_filtered_sync_cron_schedule( $schedule ) { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Allows offsetting of start times for sync cron jobs. |
||
| 618 | * |
||
| 619 | * @access public |
||
| 620 | * @static |
||
| 621 | * |
||
| 622 | * @param string $schedule The name of a cron schedule. |
||
| 623 | * @param string $hook The hook that this method is responding to. |
||
| 624 | * @return int The offset for the sync cron schedule. |
||
| 625 | */ |
||
| 626 | public static function get_start_time_offset( $schedule = '', $hook = '' ) { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Decides if a sync cron should be scheduled. |
||
| 653 | * |
||
| 654 | * @access public |
||
| 655 | * @static |
||
| 656 | * |
||
| 657 | * @param string $schedule The name of a cron schedule. |
||
| 658 | * @param string $hook The hook that this method is responding to. |
||
| 659 | */ |
||
| 660 | public static function maybe_schedule_sync_cron( $schedule, $hook ) { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Clears Jetpack sync cron jobs. |
||
| 679 | * |
||
| 680 | * @access public |
||
| 681 | * @static |
||
| 682 | */ |
||
| 683 | public static function clear_sync_cron_jobs() { |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Initializes Jetpack sync cron jobs. |
||
| 690 | * |
||
| 691 | * @access public |
||
| 692 | * @static |
||
| 693 | */ |
||
| 694 | public static function init_sync_cron_jobs() { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Perform maintenance when a plugin upgrade occurs. |
||
| 723 | * |
||
| 724 | * @access public |
||
| 725 | * @static |
||
| 726 | * |
||
| 727 | * @param string $new_version New version of the plugin. |
||
| 728 | * @param string $old_version Old version of the plugin. |
||
| 729 | */ |
||
| 730 | public static function cleanup_on_upgrade( $new_version = null, $old_version = null ) { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Get syncing status for the given fields. |
||
| 750 | * |
||
| 751 | * @access public |
||
| 752 | * @static |
||
| 753 | * |
||
| 754 | * @param string|null $fields A comma-separated string of the fields to include in the array from the JSON response. |
||
| 755 | * @return array An associative array with the status report. |
||
| 756 | */ |
||
| 757 | public static function get_sync_status( $fields = null ) { |
||
| 811 | } |
||
| 812 |
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.