Complex classes like Options 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 Options, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Options extends Module { |
||
| 16 | /** |
||
| 17 | * Whitelist for options we want to sync. |
||
| 18 | * |
||
| 19 | * @access private |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $options_whitelist; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Contentless options we want to sync. |
||
| 27 | * |
||
| 28 | * @access private |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $options_contentless; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Sync module name. |
||
| 36 | * |
||
| 37 | * @access public |
||
| 38 | * |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | public function name() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Initialize options action listeners. |
||
| 47 | * |
||
| 48 | * @access public |
||
| 49 | * |
||
| 50 | * @param callable $callable Action handler callable. |
||
| 51 | */ |
||
| 52 | public function init_listeners( $callable ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Initialize options action listeners for full sync. |
||
| 74 | * |
||
| 75 | * @access public |
||
| 76 | * |
||
| 77 | * @param callable $callable Action handler callable. |
||
| 78 | */ |
||
| 79 | public function init_full_sync_listeners( $callable ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Initialize the module in the sender. |
||
| 85 | * |
||
| 86 | * @access public |
||
| 87 | */ |
||
| 88 | public function init_before_send() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set module defaults. |
||
| 95 | * Define the options whitelist and contentless options. |
||
| 96 | * |
||
| 97 | * @access public |
||
| 98 | */ |
||
| 99 | public function set_defaults() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Set module defaults at a later time. |
||
| 106 | * |
||
| 107 | * @access public |
||
| 108 | */ |
||
| 109 | public function set_late_default() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Add old deprecated options to the list of options to keep in sync. |
||
| 119 | * |
||
| 120 | * @since 8.8.0 |
||
| 121 | * |
||
| 122 | * @access public |
||
| 123 | * |
||
| 124 | * @param array $options The default list of site options. |
||
| 125 | */ |
||
| 126 | public function add_deprecated_options( $options ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Enqueue the options actions for full sync. |
||
| 145 | * |
||
| 146 | * @access public |
||
| 147 | * |
||
| 148 | * @param array $config Full sync configuration for this sync module. |
||
| 149 | * @param int $max_items_to_enqueue Maximum number of items to enqueue. |
||
| 150 | * @param boolean $state True if full sync has finished enqueueing this module, false otherwise. |
||
| 151 | * @return array Number of actions enqueued, and next module state. |
||
| 152 | */ |
||
| 153 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Send the options actions for full sync. |
||
| 169 | * |
||
| 170 | * @access public |
||
| 171 | * |
||
| 172 | * @param array $config Full sync configuration for this sync module. |
||
| 173 | * @param int $send_until The timestamp until the current request can send. |
||
| 174 | * @param array $state This module Full Sync status. |
||
| 175 | * |
||
| 176 | * @return array This module Full Sync status. |
||
| 177 | */ |
||
| 178 | public function send_full_sync_actions( $config, $send_until, $state ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Retrieve an estimated number of actions that will be enqueued. |
||
| 188 | * |
||
| 189 | * @access public |
||
| 190 | * |
||
| 191 | * @param array $config Full sync configuration for this sync module. |
||
| 192 | * @return int Number of items yet to be enqueued. |
||
| 193 | */ |
||
| 194 | public function estimate_full_sync_actions( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Retrieve the actions that will be sent for this module during a full sync. |
||
| 200 | * |
||
| 201 | * @access public |
||
| 202 | * |
||
| 203 | * @return array Full sync actions of this module. |
||
| 204 | */ |
||
| 205 | public function get_full_sync_actions() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Retrieve all options as per the current options whitelist. |
||
| 211 | * Public so that we don't have to store so much data all the options twice. |
||
| 212 | * |
||
| 213 | * @access public |
||
| 214 | * |
||
| 215 | * @return array All options. |
||
| 216 | */ |
||
| 217 | public function get_all_options() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Update the options whitelist to the default one. |
||
| 240 | * |
||
| 241 | * @access public |
||
| 242 | */ |
||
| 243 | public function update_options_whitelist() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set the options whitelist. |
||
| 249 | * |
||
| 250 | * @access public |
||
| 251 | * |
||
| 252 | * @param array $options The new options whitelist. |
||
| 253 | */ |
||
| 254 | public function set_options_whitelist( $options ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the options whitelist. |
||
| 260 | * |
||
| 261 | * @access public |
||
| 262 | * |
||
| 263 | * @return array The options whitelist. |
||
| 264 | */ |
||
| 265 | public function get_options_whitelist() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Update the contentless options to the defaults. |
||
| 271 | * |
||
| 272 | * @access public |
||
| 273 | */ |
||
| 274 | public function update_options_contentless() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the contentless options. |
||
| 280 | * |
||
| 281 | * @access public |
||
| 282 | * |
||
| 283 | * @return array Array of the contentless options. |
||
| 284 | */ |
||
| 285 | public function get_options_contentless() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Reject any options that aren't whitelisted or contentless. |
||
| 291 | * |
||
| 292 | * @access public |
||
| 293 | * |
||
| 294 | * @param array $args The hook parameters. |
||
| 295 | * @return array $args The hook parameters. |
||
| 296 | */ |
||
| 297 | public function whitelist_options( $args ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Whether a certain option is whitelisted for sync. |
||
| 324 | * |
||
| 325 | * @access public |
||
| 326 | * |
||
| 327 | * @param string $option Option name. |
||
| 328 | * @return boolean Whether the option is whitelisted. |
||
| 329 | */ |
||
| 330 | public function is_whitelisted_option( $option ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Whether a certain option is a contentless one. |
||
| 336 | * |
||
| 337 | * @access private |
||
| 338 | * |
||
| 339 | * @param string $option Option name. |
||
| 340 | * @return boolean Whether the option is contentless. |
||
| 341 | */ |
||
| 342 | private function is_contentless_option( $option ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Filters out falsy values from theme mod options. |
||
| 348 | * |
||
| 349 | * @access private |
||
| 350 | * |
||
| 351 | * @param array $value Option value. |
||
| 352 | */ |
||
| 353 | private function filter_theme_mods( &$value ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Handle changes in the core site icon and sync them. |
||
| 361 | * |
||
| 362 | * @access public |
||
| 363 | */ |
||
| 364 | public function jetpack_sync_core_icon() { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Expand all options within a hook before they are serialized and sent to the server. |
||
| 386 | * |
||
| 387 | * @access public |
||
| 388 | * |
||
| 389 | * @param array $args The hook parameters. |
||
| 390 | * @return array $args The hook parameters. |
||
| 391 | */ |
||
| 392 | public function expand_options( $args ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Return Total number of objects. |
||
| 402 | * |
||
| 403 | * @param array $config Full Sync config. |
||
| 404 | * |
||
| 405 | * @return int total |
||
| 406 | */ |
||
| 407 | public function total( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 410 | |||
| 411 | } |
||
| 412 |