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 |
||
| 5 | class Settings { |
||
| 6 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
| 7 | |||
| 8 | static $valid_settings = array( |
||
| 9 | 'dequeue_max_bytes' => true, |
||
| 10 | 'upload_max_bytes' => true, |
||
| 11 | 'upload_max_rows' => true, |
||
| 12 | 'sync_wait_time' => true, |
||
| 13 | 'sync_wait_threshold' => true, |
||
| 14 | 'enqueue_wait_time' => true, |
||
| 15 | 'max_queue_size' => true, |
||
| 16 | 'max_queue_lag' => true, |
||
| 17 | 'queue_max_writes_sec' => true, |
||
| 18 | 'post_types_blacklist' => true, |
||
| 19 | 'disable' => true, |
||
| 20 | 'network_disable' => true, |
||
| 21 | 'render_filtered_content' => true, |
||
| 22 | 'post_meta_whitelist' => true, |
||
| 23 | 'comment_meta_whitelist' => true, |
||
| 24 | 'max_enqueue_full_sync' => true, |
||
| 25 | 'max_queue_size_full_sync' => true, |
||
| 26 | 'sync_via_cron' => true, |
||
| 27 | 'cron_sync_time_limit' => true, |
||
| 28 | 'known_importers' => true, |
||
| 29 | ); |
||
| 30 | |||
| 31 | static $is_importing; |
||
| 32 | static $is_doing_cron; |
||
| 33 | static $is_syncing; |
||
| 34 | static $is_sending; |
||
| 35 | |||
| 36 | static $settings_cache = array(); // some settings can be expensive to compute - let's cache them |
||
| 37 | |||
| 38 | static function get_settings() { |
||
| 46 | |||
| 47 | // Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
| 48 | // autoloaded on page load rather than re-queried every time. |
||
| 49 | static function get_setting( $setting ) { |
||
| 112 | |||
| 113 | static function update_settings( $new_settings ) { |
||
| 135 | |||
| 136 | static function is_network_setting( $setting ) { |
||
| 139 | |||
| 140 | // returns escapted SQL that can be injected into a WHERE clause |
||
| 141 | static function get_blacklisted_post_types_sql() { |
||
| 144 | |||
| 145 | static function get_whitelisted_post_meta_sql() { |
||
| 148 | |||
| 149 | static function get_whitelisted_comment_meta_sql() { |
||
| 152 | |||
| 153 | static function get_comments_filter_sql() { |
||
| 156 | |||
| 157 | static function reset_data() { |
||
| 168 | |||
| 169 | static function set_importing( $is_importing ) { |
||
| 173 | |||
| 174 | static function is_importing() { |
||
| 181 | |||
| 182 | static function is_sync_enabled() { |
||
| 185 | |||
| 186 | static function set_doing_cron( $is_doing_cron ) { |
||
| 190 | |||
| 191 | static function is_doing_cron() { |
||
| 198 | |||
| 199 | static function is_syncing() { |
||
| 202 | |||
| 203 | static function set_is_syncing( $is_syncing ) { |
||
| 206 | |||
| 207 | static function is_sending() { |
||
| 210 | |||
| 211 | static function set_is_sending( $is_sending ) { |
||
| 214 | } |
||
| 215 |
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.