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 |
||
| 16 | class Options { |
||
| 17 | /** |
||
| 18 | * Get formats for all supported options |
||
| 19 | * |
||
| 20 | * Format includes option type, default value, supported values or ranges, whether option is password or is multilingual |
||
| 21 | * |
||
| 22 | * @return array[] |
||
| 23 | */ |
||
| 24 | 4 | public static function get_formatting () { |
|
| 25 | 4 | $formatting = static::get_formatting_const(); |
|
| 26 | 4 | $formatting['set_single']['language']['values'] = static::get_active_languages(); |
|
| 27 | 4 | $formatting['set_single']['timezone']['values'] = get_timezones_list(); |
|
| 28 | 4 | $formatting['set_single']['default_module']['values'] = static::get_modules_that_can_be_default(); |
|
| 29 | 4 | $formatting['set_single']['theme']['values'] = static::get_themes(); |
|
| 30 | 4 | $formatting['set_multiple']['active_languages']['values'] = static::get_languages(); |
|
| 31 | 4 | return static::get_formatting_normalize($formatting); |
|
| 32 | } |
||
| 33 | /** |
||
| 34 | * @return array[] |
||
| 35 | */ |
||
| 36 | 72 | protected static function get_formatting_const () { |
|
| 37 | return [ |
||
| 38 | 'array' => [ |
||
| 39 | 'url' => [], |
||
| 40 | 'cookie_domain' => [] |
||
| 41 | 72 | ], |
|
| 42 | 'int_bool' => [ |
||
| 43 | 60 | 'site_mode' => 1, |
|
| 44 | 60 | 'title_reverse' => 0, |
|
| 45 | 60 | 'cache_compress_js_css' => 1, |
|
| 46 | 60 | 'frontend_load_optimization' => 1, |
|
| 47 | 60 | 'vulcanization' => 1, |
|
| 48 | 60 | 'put_js_after_body' => 1, |
|
| 49 | 60 | 'disable_webcomponents' => 0, |
|
| 50 | 60 | 'multilingual' => 0, |
|
| 51 | 60 | 'db_balance' => 0, |
|
| 52 | 60 | 'db_mirror_mode' => DB::MIRROR_MODE_MASTER_MASTER, |
|
| 53 | 60 | 'gravatar_support' => 0, |
|
| 54 | 60 | 'smtp' => 0, |
|
| 55 | 60 | 'smtp_auth' => 0, |
|
| 56 | 60 | 'allow_user_registration' => 1, |
|
| 57 | 60 | 'require_registration_confirmation' => 1, |
|
| 58 | 60 | 'auto_sign_in_after_registration' => 1, |
|
| 59 | 60 | 'registration_confirmation_time' => 1, |
|
| 60 | 60 | 'remember_user_ip' => 0, |
|
| 61 | 60 | 'simple_admin_mode' => 1 |
|
| 62 | ], |
||
| 63 | 'int_range' => [ |
||
| 64 | 'inserts_limit' => [ |
||
| 65 | 'min' => 1, |
||
| 66 | 'value' => 1000 |
||
| 67 | ], |
||
| 68 | 'key_expire' => [ |
||
| 69 | 'min' => 1, |
||
| 70 | 'value' => 60 * 2 |
||
| 71 | ], |
||
| 72 | 'session_expire' => [ |
||
| 73 | 'min' => 1, |
||
| 74 | 'value' => 3600 * 24 * 30 |
||
| 75 | ], |
||
| 76 | 'update_ratio' => [ |
||
| 77 | 'min' => 0, |
||
| 78 | 'max' => 100, |
||
| 79 | 'value' => 75 |
||
| 80 | ], |
||
| 81 | 'password_min_length' => [ |
||
| 82 | 'min' => 1, |
||
| 83 | 'value' => 4 |
||
| 84 | ], |
||
| 85 | 'password_min_strength' => [ |
||
| 86 | 'min' => 0, |
||
| 87 | 'max' => 7, |
||
| 88 | 'value' => 3 |
||
| 89 | ] |
||
| 90 | ], |
||
| 91 | 'set_single' => [ |
||
| 92 | 'smtp_secure' => [ |
||
| 93 | 'value' => '', |
||
| 94 | 'values' => ['', 'ssl', 'tls'] |
||
| 95 | ], |
||
| 96 | 'language' => [ |
||
| 97 | 'value' => 'English', |
||
| 98 | 'source' => 'active_languages' |
||
| 99 | ], |
||
| 100 | 'timezone' => [ |
||
| 101 | 'value' => 'UTC' |
||
| 102 | ], |
||
| 103 | 'default_module' => [ |
||
| 104 | 'value' => Config::SYSTEM_MODULE |
||
| 105 | ], |
||
| 106 | 'theme' => [ |
||
| 107 | 'value' => Config::SYSTEM_THEME |
||
| 108 | ] |
||
| 109 | ], |
||
| 110 | 'set_multiple' => [ |
||
| 111 | 'active_languages' => [ |
||
| 112 | 'value' => [ |
||
| 113 | 'English' |
||
| 114 | ] |
||
| 115 | ] |
||
| 116 | ], |
||
| 117 | 'string' => [ |
||
| 118 | 'admin_email' => '', |
||
| 119 | 'cookie_prefix' => '', |
||
| 120 | 'smtp_host' => '', |
||
| 121 | 'smtp_port' => '', |
||
| 122 | 'smtp_user' => '', |
||
| 123 | 'smtp_password' => [ |
||
| 124 | 'value' => '', |
||
| 125 | 'password' => true |
||
| 126 | ], |
||
| 127 | 'mail_from' => '' |
||
| 128 | ], |
||
| 129 | 'text' => [ |
||
| 130 | 'title_delimiter' => ' | ', |
||
| 131 | 'site_name' => [ |
||
| 132 | 'multilingual' => true, |
||
| 133 | 'value' => '' |
||
| 134 | ], |
||
| 135 | 'closed_title' => [ |
||
| 136 | 'multilingual' => true, |
||
| 137 | 'value' => 'Site closed' |
||
| 138 | ], |
||
| 139 | 'mail_from_name' => [ |
||
| 140 | 'multilingual' => true, |
||
| 141 | 'value' => 'Administrator' |
||
| 142 | ] |
||
| 143 | ], |
||
| 144 | 'html' => [ |
||
| 145 | 'closed_text' => [ |
||
| 146 | 'multilingual' => true, |
||
| 147 | 'value' => '<p>Site closed for maintenance</p>' |
||
| 148 | ], |
||
| 149 | 'mail_signature' => [ |
||
| 150 | 'multilingual' => true, |
||
| 151 | 'value' => '' |
||
| 152 | ] |
||
| 153 | ] |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | /** |
||
| 157 | * @return array[] |
||
| 158 | */ |
||
| 159 | 72 | protected static function get_formatting_const_plain () { |
|
| 160 | 72 | static $formatting; |
|
| 161 | 72 | if (!isset($formatting)) { |
|
| 162 | 72 | $formatting = array_merge(...array_values(static::get_formatting_const())); |
|
| 163 | } |
||
| 164 | 72 | return $formatting; |
|
| 165 | } |
||
| 166 | /** |
||
| 167 | * @return string[] |
||
| 168 | */ |
||
| 169 | 4 | protected static function get_languages () { |
|
| 181 | /** |
||
| 182 | * @return string[] |
||
| 183 | */ |
||
| 184 | 4 | protected static function get_themes () { |
|
| 189 | /** |
||
| 190 | * @return string[] |
||
| 191 | */ |
||
| 192 | 4 | protected static function get_modules_that_can_be_default () { |
|
| 205 | /** |
||
| 206 | * @return string[] |
||
| 207 | */ |
||
| 208 | 4 | protected static function get_active_languages () { |
|
| 216 | /** |
||
| 217 | * @param array[] $format |
||
| 218 | * |
||
| 219 | * @return array[] |
||
| 220 | */ |
||
| 221 | 4 | protected static function get_formatting_normalize ($format) { |
|
| 238 | /** |
||
| 239 | * Get default values for all supported options |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | 72 | public static function get_defaults () { |
|
| 252 | /** |
||
| 253 | * Get list of multilingual options |
||
| 254 | * |
||
| 255 | * @return string[] |
||
| 256 | */ |
||
| 257 | 12 | public static function get_multilingual () { |
|
| 267 | /** |
||
| 268 | * Take options and check each value according to needed format, correct value or use default if needed |
||
| 269 | * |
||
| 270 | * @param array $target_options |
||
| 271 | * |
||
| 272 | * @return array |
||
| 273 | */ |
||
| 274 | 4 | public static function apply_formatting ($target_options) { |
|
| 322 | } |
||
| 323 |