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 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 Callables, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Callables extends Module { |
||
| 18 | /** |
||
| 19 | * Name of the callables checksum option. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Name of the transient for locking callables. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Whitelist for callables we want to sync. |
||
| 34 | * |
||
| 35 | * @access private |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $callable_whitelist; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * For some options, we should always send the change right away! |
||
| 43 | * |
||
| 44 | * @access public |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | const ALWAYS_SEND_UPDATES_TO_THESE_OPTIONS = array( |
||
| 49 | 'jetpack_active_modules', |
||
| 50 | 'home', // option is home, callable is home_url. |
||
| 51 | 'siteurl', |
||
| 52 | 'jetpack_sync_error_idc', |
||
| 53 | 'paused_plugins', |
||
| 54 | 'paused_themes', |
||
| 55 | ); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * For some options, the callable key differs from the option name/key |
||
| 59 | * |
||
| 60 | * @access public |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | const OPTION_NAMES_TO_CALLABLE_NAMES = array( |
||
| 65 | // @TODO: Audit the other option names for differences between the option names and callable names. |
||
| 66 | 'home' => 'home_url', |
||
| 67 | ); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Sync module name. |
||
| 71 | * |
||
| 72 | * @access public |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | public function name() { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set module defaults. |
||
| 82 | * Define the callable whitelist based on whether this is a single site or a multisite installation. |
||
| 83 | * |
||
| 84 | * @access public |
||
| 85 | */ |
||
| 86 | public function set_defaults() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Initialize callables action listeners. |
||
| 96 | * |
||
| 97 | * @access public |
||
| 98 | * |
||
| 99 | * @param callable $callable Action handler callable. |
||
| 100 | */ |
||
| 101 | public function init_listeners( $callable ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Initialize callables action listeners for full sync. |
||
| 122 | * |
||
| 123 | * @access public |
||
| 124 | * |
||
| 125 | * @param callable $callable Action handler callable. |
||
| 126 | */ |
||
| 127 | public function init_full_sync_listeners( $callable ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Initialize the module in the sender. |
||
| 133 | * |
||
| 134 | * @access public |
||
| 135 | */ |
||
| 136 | public function init_before_send() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Perform module cleanup. |
||
| 145 | * Deletes any transients and options that this module uses. |
||
| 146 | * Usually triggered when uninstalling the plugin. |
||
| 147 | * |
||
| 148 | * @access public |
||
| 149 | */ |
||
| 150 | public function reset_data() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set the callable whitelist. |
||
| 162 | * |
||
| 163 | * @access public |
||
| 164 | * |
||
| 165 | * @param array $callables The new callables whitelist. |
||
| 166 | */ |
||
| 167 | public function set_callable_whitelist( $callables ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the callable whitelist. |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * |
||
| 176 | * @return array The callables whitelist. |
||
| 177 | */ |
||
| 178 | public function get_callable_whitelist() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Retrieve all callables as per the current callables whitelist. |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * |
||
| 187 | * @return array All callables. |
||
| 188 | */ |
||
| 189 | public function get_all_callables() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Invoke a particular callable. |
||
| 203 | * Used as a wrapper to standartize invocation. |
||
| 204 | * |
||
| 205 | * @access private |
||
| 206 | * |
||
| 207 | * @param callable $callable Callable to invoke. |
||
| 208 | * @return mixed Return value of the callable. |
||
| 209 | */ |
||
| 210 | private function get_callable( $callable ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Enqueue the callable actions for full sync. |
||
| 216 | * |
||
| 217 | * @access public |
||
| 218 | * |
||
| 219 | * @param array $config Full sync configuration for this sync module. |
||
| 220 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 221 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 222 | * @return array Number of actions enqueued, and next module state. |
||
| 223 | */ |
||
| 224 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | * |
||
| 243 | * @param array $config Full sync configuration for this sync module. |
||
| 244 | * @return array Number of items yet to be enqueued. |
||
| 245 | */ |
||
| 246 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 252 | * |
||
| 253 | * @access public |
||
| 254 | * |
||
| 255 | * @return array Full sync actions of this module. |
||
| 256 | */ |
||
| 257 | public function get_full_sync_actions() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Unlock callables so they would be available for syncing again. |
||
| 263 | * |
||
| 264 | * @access public |
||
| 265 | */ |
||
| 266 | public function unlock_sync_callable() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Unlock callables and plugin action links. |
||
| 272 | * |
||
| 273 | * @access public |
||
| 274 | */ |
||
| 275 | public function unlock_plugin_action_link_and_callables() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Parse and store the plugin action links if on the plugins page. |
||
| 283 | * |
||
| 284 | * @uses \DOMDocument |
||
| 285 | * @uses libxml_use_internal_errors |
||
| 286 | * @uses mb_convert_encoding |
||
| 287 | * |
||
| 288 | * @access public |
||
| 289 | */ |
||
| 290 | public function set_plugin_action_links() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Whether a certain callable should be sent. |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * |
||
| 369 | * @param array $callable_checksums Callable checksums. |
||
| 370 | * @param string $name Name of the callable. |
||
| 371 | * @param string $checksum A checksum of the callable. |
||
| 372 | * @return boolean Whether to send the callable. |
||
| 373 | */ |
||
| 374 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Sync the callables if we're supposed to. |
||
| 389 | * |
||
| 390 | * @access public |
||
| 391 | */ |
||
| 392 | public function maybe_sync_callables() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the callables that should always be sent, e.g. on cron. |
||
| 447 | * |
||
| 448 | * @return array Callables that should always be sent |
||
| 449 | */ |
||
| 450 | protected function get_always_sent_callables() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Expand the callables within a hook before they are serialized and sent to the server. |
||
| 472 | * |
||
| 473 | * @access public |
||
| 474 | * |
||
| 475 | * @param array $args The hook parameters. |
||
| 476 | * @return array $args The hook parameters. |
||
| 477 | */ |
||
| 478 | public function expand_callables( $args ) { |
||
| 491 | } |
||
| 492 |
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.