Complex classes like cfg_handler 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 cfg_handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class cfg_handler extends cfg_fields |
||
| 13 | { |
||
| 14 | /** @var \phpbb\request\request_interface */ |
||
| 15 | protected $request; |
||
| 16 | |||
| 17 | /** @var \phpbb\template\template */ |
||
| 18 | protected $template; |
||
| 19 | |||
| 20 | /** @var \phpbb\language\language */ |
||
| 21 | protected $translator; |
||
| 22 | |||
| 23 | /** @var \blitze\sitemaker\services\groups */ |
||
| 24 | protected $groups; |
||
| 25 | |||
| 26 | /** @var string phpBB root path */ |
||
| 27 | protected $phpbb_root_path; |
||
| 28 | |||
| 29 | /** @var string phpEx */ |
||
| 30 | protected $php_ext; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor |
||
| 34 | * |
||
| 35 | * @param \phpbb\request\request_interface $request Request object |
||
| 36 | * @param \phpbb\template\template $template Template object |
||
| 37 | * @param \phpbb\language\language $translator Language object |
||
| 38 | * @param \blitze\sitemaker\services\groups $groups Groups object |
||
| 39 | * @param string $phpbb_root_path phpBB root path |
||
| 40 | * @param string $php_ext phpEx |
||
| 41 | */ |
||
| 42 | 55 | public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\language\language $translator, \blitze\sitemaker\services\groups $groups, $phpbb_root_path, $php_ext) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param array $block_data |
||
| 56 | * @param array $default_settings |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 9 | public function get_edit_form(array $block_data, array $default_settings) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param array $default_settings |
||
| 75 | * @return array|void |
||
| 76 | */ |
||
| 77 | 7 | public function get_submitted_settings(array $default_settings) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $default_settings |
||
| 95 | * @param array $cfg_array |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 7 | protected function validate_block_settings(array $default_settings, array $cfg_array) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * As a workaround to prevent mod_security and similar from preventing us from posting html/script, |
||
| 115 | * we use encodeURI before submitting data via ajax (see develop/blocks/manager.js). |
||
| 116 | * This decodes the data before submitting to the database |
||
| 117 | * |
||
| 118 | * @param array $cfg_array |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | 7 | private function decode_source_html(array $cfg_array) |
|
| 122 | { |
||
| 123 | 7 | if (isset($cfg_array['source'])) |
|
| 124 | 7 | { |
|
| 125 | 1 | $cfg_array['source'] = urldecode($cfg_array['source']); |
|
| 126 | 1 | } |
|
| 127 | |||
| 128 | 7 | return $cfg_array; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the html form |
||
| 133 | * |
||
| 134 | * @param array $block_data |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | 9 | private function get_form(array $block_data) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Generate block configuration fields |
||
| 159 | * |
||
| 160 | * @param array $db_settings |
||
| 161 | * @param array $default_settings |
||
| 162 | */ |
||
| 163 | 9 | private function generate_config_fields(array &$db_settings, array $default_settings) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get the field html |
||
| 187 | * |
||
| 188 | * @param string $field |
||
| 189 | * @param array $db_settings |
||
| 190 | * @param array $vars |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | 8 | private function get_field_template($field, array &$db_settings, array &$vars) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param array $vars |
||
| 211 | * @param array $type |
||
| 212 | * @param array $db_settings |
||
| 213 | * @param string $field |
||
| 214 | * @return object |
||
| 215 | */ |
||
| 216 | 8 | private function get_field_object(array &$vars, array &$type, array &$db_settings, $field) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Set field legend |
||
| 240 | * |
||
| 241 | * @param string $field |
||
| 242 | * @param string|array $vars |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | 8 | private function is_input_field($field, $vars) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Set field legend |
||
| 252 | * |
||
| 253 | * @param string $field |
||
| 254 | * @param string|array $vars |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | 8 | private function set_legend($field, $vars) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get field details |
||
| 274 | * |
||
| 275 | * @param array $vars |
||
| 276 | * @return mixed|string |
||
| 277 | */ |
||
| 278 | 8 | private function explain_field(array $vars) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Add text after field |
||
| 291 | * |
||
| 292 | * @param array $vars |
||
| 293 | * @return mixed|string |
||
| 294 | */ |
||
| 295 | 8 | private function append_field(array $vars) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Set field parameters |
||
| 308 | * |
||
| 309 | * @param string $field |
||
| 310 | * @param array $vars |
||
| 311 | * @param array $settings |
||
| 312 | */ |
||
| 313 | 8 | private function set_params($field, array &$vars, array $settings) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get field value |
||
| 324 | * |
||
| 325 | * @param string $field |
||
| 326 | * @param mixed $default |
||
| 327 | * @param array $db_settings |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | 8 | private function get_field_value($field, $default, array $db_settings) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $vars |
||
| 337 | * @param array $type |
||
| 338 | * @param string $field |
||
| 339 | */ |
||
| 340 | 1 | private function prep_select_field_for_display(array &$vars, array &$type, $field) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * @param array $vars |
||
| 349 | * @param array $type |
||
| 350 | * @param string $field |
||
| 351 | */ |
||
| 352 | 2 | private function prep_checkbox_field_for_display(array &$vars, array &$type, $field) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param array $vars |
||
| 361 | * @param array $type |
||
| 362 | * @param string $field |
||
| 363 | */ |
||
| 364 | 2 | private function prep_radio_field_for_display(array &$vars, array &$type, $field) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param array $vars |
||
| 376 | * @param array $type |
||
| 377 | * @param string $field |
||
| 378 | */ |
||
| 379 | 1 | private function prep_multi_select_field_for_display(array &$vars, array &$type, $field) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param array $vars |
||
| 388 | * @param array $type |
||
| 389 | */ |
||
| 390 | 1 | private function prep_hidden_field_for_display(array &$vars, array &$type) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param array $vars |
||
| 400 | * @param array $type |
||
| 401 | */ |
||
| 402 | 1 | private function prep_custom_field_for_display(array &$vars, array &$type) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param array $cfg_array |
||
| 410 | * @param array $df_settings |
||
| 411 | */ |
||
| 412 | 6 | private function get_multi_select(array &$cfg_array, array $df_settings) |
|
| 422 | } |
||
| 423 |