Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Updates 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 Updates, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 15 | class Updates extends Module { | ||
| 16 | /** | ||
| 17 | * Name of the updates checksum option. | ||
| 18 | * | ||
| 19 | * @var string | ||
| 20 | */ | ||
| 21 | const UPDATES_CHECKSUM_OPTION_NAME = 'jetpack_updates_sync_checksum'; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * WordPress Version. | ||
| 25 | * | ||
| 26 | * @access private | ||
| 27 | * | ||
| 28 | * @var string | ||
| 29 | */ | ||
| 30 | private $old_wp_version = null; | ||
| 31 | |||
| 32 | /** | ||
| 33 | * The current updates. | ||
| 34 | * | ||
| 35 | * @access private | ||
| 36 | * | ||
| 37 | * @var array | ||
| 38 | */ | ||
| 39 | private $updates = array(); | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Set module defaults. | ||
| 43 | * | ||
| 44 | * @access public | ||
| 45 | */ | ||
| 46 | 	public function set_defaults() { | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Sync module name. | ||
| 52 | * | ||
| 53 | * @access public | ||
| 54 | * | ||
| 55 | * @return string | ||
| 56 | */ | ||
| 57 | 	public function name() { | ||
| 60 | |||
| 61 | /** | ||
| 62 | * Initialize updates action listeners. | ||
| 63 | * | ||
| 64 | * @access public | ||
| 65 | * | ||
| 66 | * @param callable $callable Action handler callable. | ||
| 67 | */ | ||
| 68 | 	public function init_listeners( $callable ) { | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Initialize updates action listeners for full sync. | ||
| 115 | * | ||
| 116 | * @access public | ||
| 117 | * | ||
| 118 | * @param callable $callable Action handler callable. | ||
| 119 | */ | ||
| 120 | 	public function init_full_sync_listeners( $callable ) { | ||
| 123 | |||
| 124 | /** | ||
| 125 | * Initialize the module in the sender. | ||
| 126 | * | ||
| 127 | * @access public | ||
| 128 | */ | ||
| 129 | 	public function init_before_send() { | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Handle a core network update. | ||
| 136 | * | ||
| 137 | * @access public | ||
| 138 | * | ||
| 139 | * @param int $wp_db_version Current version of the WordPress database. | ||
| 140 | * @param int $old_wp_db_version Old version of the WordPress database. | ||
| 141 | * @return int Current version of the WordPress database. | ||
| 142 | */ | ||
| 143 | 	public function update_core_network_event( $wp_db_version, $old_wp_db_version ) { | ||
| 157 | |||
| 158 | /** | ||
| 159 | * Handle a core update. | ||
| 160 | * | ||
| 161 | * @access public | ||
| 162 | * | ||
| 163 | 	 * @todo Implement nonce or refactor to use `admin_post_{$action}` hooks instead. | ||
| 164 | * | ||
| 165 | * @param string $new_wp_version The new WP core version. | ||
| 166 | */ | ||
| 167 | 	public function update_core( $new_wp_version ) { | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Retrieve the checksum for an update. | ||
| 212 | * | ||
| 213 | * @access public | ||
| 214 | * | ||
| 215 | * @param object $update The update object. | ||
| 216 | * @param string $transient The transient we're retrieving a checksum for. | ||
| 217 | * @return int The checksum. | ||
| 218 | */ | ||
| 219 | 	public function get_update_checksum( $update, $transient ) { | ||
| 281 | |||
| 282 | /** | ||
| 283 | * Validate a change coming from an update before sending for sync. | ||
| 284 | * | ||
| 285 | * @access public | ||
| 286 | * | ||
| 287 | * @param mixed $value Site transient value. | ||
| 288 | * @param int $expiration Time until transient expiration in seconds. | ||
| 289 | * @param string $transient Transient name. | ||
| 290 | */ | ||
| 291 | 	public function validate_update_change( $value, $expiration, $transient ) { | ||
| 327 | |||
| 328 | /** | ||
| 329 | * Sync the last update only. | ||
| 330 | * | ||
| 331 | * @access public | ||
| 332 | */ | ||
| 333 | 	public function sync_last_event() { | ||
| 351 | |||
| 352 | /** | ||
| 353 | * Enqueue the updates actions for full sync. | ||
| 354 | * | ||
| 355 | * @access public | ||
| 356 | * | ||
| 357 | * @param array $config Full sync configuration for this sync module. | ||
| 358 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. | ||
| 359 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. | ||
| 360 | * @return array Number of actions enqueued, and next module state. | ||
| 361 | */ | ||
| 362 | 	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
| 375 | |||
| 376 | /** | ||
| 377 | * Send the updates actions for full sync. | ||
| 378 | * | ||
| 379 | * @access public | ||
| 380 | * | ||
| 381 | * @param array $config Full sync configuration for this sync module. | ||
| 382 | * @param int $send_until The timestamp until the current request can send. | ||
| 383 | * @param array $state This module Full Sync status. | ||
| 384 | * | ||
| 385 | * @return array This module Full Sync status. | ||
| 386 | */ | ||
| 387 | 	public function send_full_sync_actions( $config, $send_until, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
| 394 | |||
| 395 | /** | ||
| 396 | * Retrieve an estimated number of actions that will be enqueued. | ||
| 397 | * | ||
| 398 | * @access public | ||
| 399 | * | ||
| 400 | * @param array $config Full sync configuration for this sync module. | ||
| 401 | * @return array Number of items yet to be enqueued. | ||
| 402 | */ | ||
| 403 | 	public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
| 406 | |||
| 407 | /** | ||
| 408 | * Retrieve the actions that will be sent for this module during a full sync. | ||
| 409 | * | ||
| 410 | * @access public | ||
| 411 | * | ||
| 412 | * @return array Full sync actions of this module. | ||
| 413 | */ | ||
| 414 | 	public function get_full_sync_actions() { | ||
| 417 | |||
| 418 | /** | ||
| 419 | * Retrieve all updates that we're interested in. | ||
| 420 | * | ||
| 421 | * @access public | ||
| 422 | * | ||
| 423 | * @return array All updates. | ||
| 424 | */ | ||
| 425 | 	public function get_all_updates() { | ||
| 432 | |||
| 433 | /** | ||
| 434 | * Remove unnecessary keys from synced updates data. | ||
| 435 | * | ||
| 436 | * @access public | ||
| 437 | * | ||
| 438 | * @param array $args Hook arguments. | ||
| 439 | * @return array $args Hook arguments. | ||
| 440 | */ | ||
| 441 | 	public function filter_update_keys( $args ) { | ||
| 450 | |||
| 451 | /** | ||
| 452 | * Filter out upgrader object from the completed upgrader action args. | ||
| 453 | * | ||
| 454 | * @access public | ||
| 455 | * | ||
| 456 | * @param array $args Hook arguments. | ||
| 457 | * @return array $args Filtered hook arguments. | ||
| 458 | */ | ||
| 459 | 	public function filter_upgrader_process_complete( $args ) { | ||
| 464 | |||
| 465 | /** | ||
| 466 | * Expand the updates within a hook before they are serialized and sent to the server. | ||
| 467 | * | ||
| 468 | * @access public | ||
| 469 | * | ||
| 470 | * @param array $args The hook parameters. | ||
| 471 | * @return array $args The hook parameters. | ||
| 472 | */ | ||
| 473 | 	public function expand_updates( $args ) { | ||
| 480 | |||
| 481 | /** | ||
| 482 | * Expand the themes within a hook before they are serialized and sent to the server. | ||
| 483 | * | ||
| 484 | * @access public | ||
| 485 | * | ||
| 486 | * @param array $args The hook parameters. | ||
| 487 | * @return array $args The hook parameters. | ||
| 488 | */ | ||
| 489 | 	public function expand_themes( $args ) { | ||
| 504 | |||
| 505 | /** | ||
| 506 | * Perform module cleanup. | ||
| 507 | * Deletes any transients and options that this module uses. | ||
| 508 | * Usually triggered when uninstalling the plugin. | ||
| 509 | * | ||
| 510 | * @access public | ||
| 511 | */ | ||
| 512 | 	public function reset_data() { | ||
| 515 | |||
| 516 | /** | ||
| 517 | * Return Total number of objects. | ||
| 518 | * | ||
| 519 | * @param array $config Full Sync config. | ||
| 520 | * | ||
| 521 | * @return int total | ||
| 522 | */ | ||
| 523 | 	public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
| 526 | |||
| 527 | /** | ||
| 528 | * Retrieve a set of updates by their IDs. | ||
| 529 | * | ||
| 530 | * @access public | ||
| 531 | * | ||
| 532 | * @param string $object_type Object type. | ||
| 533 | * @param array $ids Object IDs. | ||
| 534 | * @return array Array of objects. | ||
| 535 | */ | ||
| 536 | View Code Duplication | 	public function get_objects_by_id( $object_type, $ids ) { | |
| 537 | 		if ( empty( $ids ) || empty( $object_type ) || 'update' !== $object_type ) { | ||
| 538 | return array(); | ||
| 539 | } | ||
| 540 | |||
| 541 | $objects = array(); | ||
| 542 | 		foreach ( (array) $ids as $id ) { | ||
| 543 | $object = $this->get_object_by_id( $object_type, $id ); | ||
| 544 | |||
| 545 | 			if ( 'all' === $id ) { | ||
| 546 | // If all was requested it contains all updates and can simply be returned. | ||
| 547 | return $object; | ||
| 548 | } | ||
| 549 | $objects[ $id ] = $object; | ||
| 550 | } | ||
| 551 | |||
| 552 | return $objects; | ||
| 553 | } | ||
| 554 | |||
| 555 | /** | ||
| 556 | * Retrieve a update by its id. | ||
| 557 | * | ||
| 558 | * @access public | ||
| 559 | * | ||
| 560 | * @param string $object_type Type of the sync object. | ||
| 561 | * @param string $id ID of the sync object. | ||
| 562 | * @return mixed Value of Update. | ||
| 563 | */ | ||
| 564 | 	public function get_object_by_id( $object_type, $id ) { | ||
| 577 | |||
| 578 | } | ||
| 579 |