Complex classes like Settings 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 Settings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Settings { |
||
| 14 | /** |
||
| 15 | * Prefix, used for the sync settings option names. |
||
| 16 | * |
||
| 17 | * @access public |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * A whitelist of valid settings. |
||
| 25 | * |
||
| 26 | * @access public |
||
| 27 | * @static |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public static $valid_settings = array( |
||
| 32 | 'dequeue_max_bytes' => true, |
||
| 33 | 'upload_max_bytes' => true, |
||
| 34 | 'upload_max_rows' => true, |
||
| 35 | 'sync_wait_time' => true, |
||
| 36 | 'sync_wait_threshold' => true, |
||
| 37 | 'enqueue_wait_time' => true, |
||
| 38 | 'max_queue_size' => true, |
||
| 39 | 'max_queue_lag' => true, |
||
| 40 | 'queue_max_writes_sec' => true, |
||
| 41 | 'post_types_blacklist' => true, |
||
| 42 | 'taxonomies_blacklist' => true, |
||
| 43 | 'disable' => true, |
||
| 44 | 'network_disable' => true, |
||
| 45 | 'render_filtered_content' => true, |
||
| 46 | 'post_meta_whitelist' => true, |
||
| 47 | 'comment_meta_whitelist' => true, |
||
| 48 | 'max_enqueue_full_sync' => true, |
||
| 49 | 'max_queue_size_full_sync' => true, |
||
| 50 | 'sync_via_cron' => true, |
||
| 51 | 'cron_sync_time_limit' => true, |
||
| 52 | 'known_importers' => true, |
||
| 53 | 'term_relationships_full_sync_item_size' => true, |
||
| 54 | 'sync_sender_enabled' => true, |
||
| 55 | 'full_sync_sender_enabled' => true, |
||
| 56 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Whether WordPress is currently running an import. |
||
| 60 | * |
||
| 61 | * @access public |
||
| 62 | * @static |
||
| 63 | * |
||
| 64 | * @var null|boolean |
||
| 65 | */ |
||
| 66 | public static $is_importing; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Whether WordPress is currently running a WP cron request. |
||
| 70 | * |
||
| 71 | * @access public |
||
| 72 | * @static |
||
| 73 | * |
||
| 74 | * @var null|boolean |
||
| 75 | */ |
||
| 76 | public static $is_doing_cron; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Whether we're currently syncing. |
||
| 80 | * |
||
| 81 | * @access public |
||
| 82 | * @static |
||
| 83 | * |
||
| 84 | * @var null|boolean |
||
| 85 | */ |
||
| 86 | public static $is_syncing; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Whether we're currently sending sync items. |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * @static |
||
| 93 | * |
||
| 94 | * @var null|boolean |
||
| 95 | */ |
||
| 96 | public static $is_sending; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Some settings can be expensive to compute - let's cache them. |
||
| 100 | * |
||
| 101 | * @access public |
||
| 102 | * @static |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | public static $settings_cache = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Retrieve all settings with their current values. |
||
| 110 | * |
||
| 111 | * @access public |
||
| 112 | * @static |
||
| 113 | * |
||
| 114 | * @return array All current settings. |
||
| 115 | */ |
||
| 116 | public static function get_settings() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
| 127 | * autoloaded on page load rather than re-queried every time. |
||
| 128 | * |
||
| 129 | * @access public |
||
| 130 | * @static |
||
| 131 | * |
||
| 132 | * @param string $setting The setting name. |
||
| 133 | * @return mixed The setting value. |
||
| 134 | */ |
||
| 135 | public static function get_setting( $setting ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Change multiple settings in the same time. |
||
| 204 | * |
||
| 205 | * @access public |
||
| 206 | * @static |
||
| 207 | * |
||
| 208 | * @param array $new_settings The new settings. |
||
| 209 | */ |
||
| 210 | public static function update_settings( $new_settings ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Whether the specified setting is a network setting. |
||
| 235 | * |
||
| 236 | * @access public |
||
| 237 | * @static |
||
| 238 | * |
||
| 239 | * @param string $setting Setting name. |
||
| 240 | * @return boolean Whether the setting is a network setting. |
||
| 241 | */ |
||
| 242 | public static function is_network_setting( $setting ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns escaped SQL for blacklisted post types. |
||
| 248 | * Can be injected directly into a WHERE clause. |
||
| 249 | * |
||
| 250 | * @access public |
||
| 251 | * @static |
||
| 252 | * |
||
| 253 | * @return string SQL WHERE clause. |
||
| 254 | */ |
||
| 255 | public static function get_blacklisted_post_types_sql() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns escaped SQL for blacklisted taxonomies. |
||
| 261 | * Can be injected directly into a WHERE clause. |
||
| 262 | * |
||
| 263 | * @access public |
||
| 264 | * @static |
||
| 265 | * |
||
| 266 | * @return string SQL WHERE clause. |
||
| 267 | */ |
||
| 268 | public static function get_blacklisted_taxonomies_sql() { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns escaped SQL for blacklisted post meta. |
||
| 274 | * Can be injected directly into a WHERE clause. |
||
| 275 | * |
||
| 276 | * @access public |
||
| 277 | * @static |
||
| 278 | * |
||
| 279 | * @return string SQL WHERE clause. |
||
| 280 | */ |
||
| 281 | public static function get_whitelisted_post_meta_sql() { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Returns escaped SQL for blacklisted comment meta. |
||
| 287 | * Can be injected directly into a WHERE clause. |
||
| 288 | * |
||
| 289 | * @access public |
||
| 290 | * @static |
||
| 291 | * |
||
| 292 | * @return string SQL WHERE clause. |
||
| 293 | */ |
||
| 294 | public static function get_whitelisted_comment_meta_sql() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns escaped SQL for comments, excluding any spam comments. |
||
| 300 | * Can be injected directly into a WHERE clause. |
||
| 301 | * |
||
| 302 | * @access public |
||
| 303 | * @static |
||
| 304 | * |
||
| 305 | * @return string SQL WHERE clause. |
||
| 306 | */ |
||
| 307 | public static function get_comments_filter_sql() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Delete any settings options and clean up the current settings state. |
||
| 313 | * |
||
| 314 | * @access public |
||
| 315 | * @static |
||
| 316 | */ |
||
| 317 | public static function reset_data() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set the importing state. |
||
| 331 | * |
||
| 332 | * @access public |
||
| 333 | * @static |
||
| 334 | * |
||
| 335 | * @param boolean $is_importing Whether WordPress is currently importing. |
||
| 336 | */ |
||
| 337 | public static function set_importing( $is_importing ) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Whether WordPress is currently importing. |
||
| 344 | * |
||
| 345 | * @access public |
||
| 346 | * @static |
||
| 347 | * |
||
| 348 | * @return boolean Whether WordPress is currently importing. |
||
| 349 | */ |
||
| 350 | public static function is_importing() { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Whether sync is enabled. |
||
| 360 | * |
||
| 361 | * @access public |
||
| 362 | * @static |
||
| 363 | * |
||
| 364 | * @return boolean Whether sync is enabled. |
||
| 365 | */ |
||
| 366 | public static function is_sync_enabled() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set the WP cron state. |
||
| 372 | * |
||
| 373 | * @access public |
||
| 374 | * @static |
||
| 375 | * |
||
| 376 | * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron. |
||
| 377 | */ |
||
| 378 | public static function set_doing_cron( $is_doing_cron ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Whether WordPress is currently doing WP cron. |
||
| 385 | * |
||
| 386 | * @access public |
||
| 387 | * @static |
||
| 388 | * |
||
| 389 | * @return boolean Whether WordPress is currently doing WP cron. |
||
| 390 | */ |
||
| 391 | public static function is_doing_cron() { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Whether we are currently syncing. |
||
| 401 | * |
||
| 402 | * @access public |
||
| 403 | * @static |
||
| 404 | * |
||
| 405 | * @return boolean Whether we are currently syncing. |
||
| 406 | */ |
||
| 407 | public static function is_syncing() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set the syncing state. |
||
| 413 | * |
||
| 414 | * @access public |
||
| 415 | * @static |
||
| 416 | * |
||
| 417 | * @param boolean $is_syncing Whether we are currently syncing. |
||
| 418 | */ |
||
| 419 | public static function set_is_syncing( $is_syncing ) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Whether we are currently sending sync items. |
||
| 425 | * |
||
| 426 | * @access public |
||
| 427 | * @static |
||
| 428 | * |
||
| 429 | * @return boolean Whether we are currently sending sync items. |
||
| 430 | */ |
||
| 431 | public static function is_sending() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Set the sending state. |
||
| 437 | * |
||
| 438 | * @access public |
||
| 439 | * @static |
||
| 440 | * |
||
| 441 | * @param boolean $is_sending Whether we are currently sending sync items. |
||
| 442 | */ |
||
| 443 | public static function set_is_sending( $is_sending ) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Whether should send from the queue |
||
| 449 | * |
||
| 450 | * @access public |
||
| 451 | * @static |
||
| 452 | * |
||
| 453 | * @param string $queue_id The queue identifier. |
||
| 454 | * |
||
| 455 | * @return boolean Whether sync is enabled. |
||
| 456 | */ |
||
| 457 | public static function is_sender_enabled( $queue_id ) { |
||
| 460 | |||
| 461 | } |
||
| 462 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.