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 Jetpack_Sync_Module_Callables 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 Jetpack_Sync_Module_Callables, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Jetpack_Sync_Module_Callables extends Jetpack_Sync_Module { |
||
| 6 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
||
| 7 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
||
| 8 | |||
| 9 | private $callable_whitelist; |
||
| 10 | |||
| 11 | public function name() { |
||
| 14 | |||
| 15 | public function set_defaults() { |
||
| 22 | |||
| 23 | public function init_listeners( $callable ) { |
||
| 24 | add_action( 'jetpack_sync_callable', $callable, 10, 2 ); |
||
| 25 | add_action( 'admin_init', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late |
||
| 26 | |||
| 27 | // For some options, we should always send the change right away! |
||
| 28 | $always_send_updates_to_these_options = array( |
||
| 29 | 'jetpack_active_modules', |
||
| 30 | 'home', |
||
| 31 | 'siteurl', |
||
| 32 | 'jetpack_sync_error_idc', |
||
| 33 | ); |
||
| 34 | foreach( $always_send_updates_to_these_options as $option ) { |
||
| 35 | add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) ); |
||
| 36 | } |
||
| 37 | |||
| 38 | add_action( 'jetpack_pre_plugin_upgrade', array( $this, 'unlock_sync_callable' ) ); |
||
| 39 | |||
| 40 | // Provide a hook so that hosts can send changes to certain callables right away. |
||
| 41 | // Especially useful when a host uses constants to change home and siteurl. |
||
| 42 | add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) ); |
||
| 43 | |||
| 44 | // get_plugins and wp_version |
||
| 45 | // gets fired when new code gets installed, updates etc. |
||
| 46 | add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
||
| 47 | add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) ); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function init_full_sync_listeners( $callable ) { |
||
| 51 | add_action( 'jetpack_full_sync_callables', $callable ); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function init_before_send() { |
||
| 55 | add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) ); |
||
| 56 | |||
| 57 | // full sync |
||
| 58 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) ); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function reset_data() { |
||
| 62 | delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME ); |
||
| 63 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
||
| 64 | |||
| 65 | $url_callables = array( 'home_url', 'site_url', 'main_network_site_url' ); |
||
| 66 | foreach( $url_callables as $callable ) { |
||
| 67 | delete_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable ); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | function set_callable_whitelist( $callables ) { |
||
| 72 | $this->callable_whitelist = $callables; |
||
| 73 | } |
||
| 74 | |||
| 75 | function get_callable_whitelist() { |
||
| 76 | return $this->callable_whitelist; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function get_all_callables() { |
||
| 80 | // get_all_callables should run as the master user always. |
||
| 81 | $current_user_id = get_current_user_id(); |
||
| 82 | wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) ); |
||
| 83 | $callables = array_combine( |
||
| 84 | array_keys( $this->get_callable_whitelist() ), |
||
| 85 | array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) ) |
||
| 86 | ); |
||
| 87 | wp_set_current_user( $current_user_id ); |
||
| 88 | return $callables; |
||
| 89 | } |
||
| 90 | |||
| 91 | private function get_callable( $callable ) { |
||
| 92 | return call_user_func( $callable ); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 96 | /** |
||
| 97 | * Tells the client to sync all callables to the server |
||
| 98 | * |
||
| 99 | * @since 4.2.0 |
||
| 100 | * |
||
| 101 | * @param boolean Whether to expand callables (should always be true) |
||
| 102 | */ |
||
| 103 | do_action( 'jetpack_full_sync_callables', true ); |
||
| 104 | |||
| 105 | // The number of actions enqueued, and next module state (true == done) |
||
| 106 | return array( 1, true ); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function estimate_full_sync_actions( $config ) { |
||
| 110 | return 1; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function get_full_sync_actions() { |
||
| 114 | return array( 'jetpack_full_sync_callables' ); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function unlock_sync_callable() { |
||
| 118 | add_filter( 'jetpack_check_and_send_callables', '__return_true' ); |
||
| 119 | error_log('unlock_sync_callable ' . getmypid() ); |
||
| 120 | delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ); |
||
| 121 | } |
||
| 122 | |||
| 123 | public function unlock_plugin_action_link_and_callables() { |
||
| 127 | |||
| 128 | public function set_plugin_action_links() { |
||
| 129 | if ( |
||
| 130 | ! class_exists( 'DOMDocument' ) || |
||
| 131 | ! function_exists ( 'libxml_use_internal_errors' ) || |
||
| 132 | ! function_exists ( 'mb_convert_encoding' ) |
||
| 133 | ) { |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | $plugins_action_links = array(); |
||
| 138 | // Is the transient lock in place? |
||
| 139 | $plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh', false ); |
||
| 197 | |||
| 198 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
||
| 210 | |||
| 211 | public function maybe_sync_callables() { |
||
| 253 | |||
| 254 | public function expand_callables( $args ) { |
||
| 267 | } |
||
| 268 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.