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 | * Sync module name. |
||
| 43 | * |
||
| 44 | * @access public |
||
| 45 | * |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function name() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Set module defaults. |
||
| 54 | * Define the callable whitelist based on whether this is a single site or a multisite installation. |
||
| 55 | * |
||
| 56 | * @access public |
||
| 57 | */ |
||
| 58 | public function set_defaults() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Initialize callables action listeners. |
||
| 68 | * |
||
| 69 | * @access public |
||
| 70 | * |
||
| 71 | * @param callable $callable Action handler callable. |
||
| 72 | */ |
||
| 73 | public function init_listeners( $callable ) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Initialize callables action listeners for full sync. |
||
| 103 | * |
||
| 104 | * @access public |
||
| 105 | * |
||
| 106 | * @param callable $callable Action handler callable. |
||
| 107 | */ |
||
| 108 | public function init_full_sync_listeners( $callable ) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Initialize the module in the sender. |
||
| 114 | * |
||
| 115 | * @access public |
||
| 116 | */ |
||
| 117 | public function init_before_send() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Perform module cleanup. |
||
| 126 | * Deletes any transients and options that this module uses. |
||
| 127 | * Usually triggered when uninstalling the plugin. |
||
| 128 | * |
||
| 129 | * @access public |
||
| 130 | */ |
||
| 131 | public function reset_data() { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set the callable whitelist. |
||
| 143 | * |
||
| 144 | * @access public |
||
| 145 | * |
||
| 146 | * @param array $callables The new callables whitelist. |
||
| 147 | */ |
||
| 148 | public function set_callable_whitelist( $callables ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the callable whitelist. |
||
| 154 | * |
||
| 155 | * @access public |
||
| 156 | * |
||
| 157 | * @return array The callables whitelist. |
||
| 158 | */ |
||
| 159 | public function get_callable_whitelist() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieve all callables as per the current callables whitelist. |
||
| 165 | * |
||
| 166 | * @access public |
||
| 167 | * |
||
| 168 | * @return array All callables. |
||
| 169 | */ |
||
| 170 | public function get_all_callables() { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Invoke a particular callable. |
||
| 184 | * Used as a wrapper to standartize invocation. |
||
| 185 | * |
||
| 186 | * @access private |
||
| 187 | * |
||
| 188 | * @param callable $callable Callable to invoke. |
||
| 189 | * @return mixed Return value of the callable. |
||
| 190 | */ |
||
| 191 | private function get_callable( $callable ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Enqueue the callable actions for full sync. |
||
| 197 | * |
||
| 198 | * @access public |
||
| 199 | * |
||
| 200 | * @param array $config Full sync configuration for this sync module. |
||
| 201 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 202 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 203 | * @return array Number of actions enqueued, and next module state. |
||
| 204 | */ |
||
| 205 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 221 | * |
||
| 222 | * @access public |
||
| 223 | * |
||
| 224 | * @param array $config Full sync configuration for this sync module. |
||
| 225 | * @return array Number of items yet to be enqueued. |
||
| 226 | */ |
||
| 227 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 233 | * |
||
| 234 | * @access public |
||
| 235 | * |
||
| 236 | * @return array Full sync actions of this module. |
||
| 237 | */ |
||
| 238 | public function get_full_sync_actions() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Unlock callables so they would be available for syncing again. |
||
| 244 | * |
||
| 245 | * @access public |
||
| 246 | */ |
||
| 247 | public function unlock_sync_callable() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Unlock callables and plugin action links. |
||
| 253 | * |
||
| 254 | * @access public |
||
| 255 | */ |
||
| 256 | public function unlock_plugin_action_link_and_callables() { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Parse and store the plugin action links if on the plugins page. |
||
| 264 | * |
||
| 265 | * @uses \DOMDocument |
||
| 266 | * @uses libxml_use_internal_errors |
||
| 267 | * @uses mb_convert_encoding |
||
| 268 | * |
||
| 269 | * @access public |
||
| 270 | */ |
||
| 271 | public function set_plugin_action_links() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Whether a certain callable should be sent. |
||
| 347 | * |
||
| 348 | * @access public |
||
| 349 | * |
||
| 350 | * @param array $callable_checksums Callable checksums. |
||
| 351 | * @param string $name Name of the callable. |
||
| 352 | * @param string $checksum A checksum of the callable. |
||
| 353 | * @return boolean Whether to send the callable. |
||
| 354 | */ |
||
| 355 | public function should_send_callable( $callable_checksums, $name, $checksum ) { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Sync the callables if we're supposed to. |
||
| 370 | * |
||
| 371 | * @access public |
||
| 372 | */ |
||
| 373 | public function maybe_sync_callables() { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Expand the callables within a hook before they are serialized and sent to the server. |
||
| 422 | * |
||
| 423 | * @access public |
||
| 424 | * |
||
| 425 | * @param array $args The hook parameters. |
||
| 426 | * @return array $args The hook parameters. |
||
| 427 | */ |
||
| 428 | public function expand_callables( $args ) { |
||
| 441 | } |
||
| 442 |
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.