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\user */ |
||
21 | protected $user; |
||
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\user $user User 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 | 51 | public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \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 | 8 | public function get_edit_form(array $block_data, array $default_settings) |
|
76 | |||
77 | /** |
||
78 | * @param array $default_settings |
||
79 | * @return array|void |
||
80 | */ |
||
81 | 6 | public function get_submitted_settings(array $default_settings) |
|
96 | |||
97 | /** |
||
98 | * @param array $default_settings |
||
99 | * @param array $cfg_array |
||
100 | * @return array |
||
101 | */ |
||
102 | 6 | protected function validate_block_settings(array $default_settings, array $cfg_array) |
|
116 | |||
117 | /** |
||
118 | * As a workaround to prevent mod_security and similar from preventing us |
||
119 | * from posting html/script, we use encodeURI before submitting data. |
||
120 | * This decodes the data before submitting to the database |
||
121 | * |
||
122 | * @param array $cfg_array |
||
123 | * @return array |
||
124 | */ |
||
125 | 6 | private function decode_source_html(array $cfg_array) |
|
134 | |||
135 | /** |
||
136 | * Get the html form |
||
137 | * |
||
138 | * @param array $block_data |
||
139 | * @return string |
||
140 | */ |
||
141 | 8 | private function get_form(array $block_data) |
|
160 | |||
161 | /** |
||
162 | * Generate block configuration fields |
||
163 | * |
||
164 | * @param array $db_settings |
||
165 | * @param array $default_settings |
||
166 | */ |
||
167 | 8 | private function generate_config_fields(array &$db_settings, array $default_settings) |
|
189 | |||
190 | /** |
||
191 | * Get the field html |
||
192 | * |
||
193 | * @param string $field |
||
194 | * @param array $db_settings |
||
195 | * @param array $vars |
||
196 | * @return string |
||
197 | */ |
||
198 | 7 | private function get_field_template($field, array &$db_settings, array &$vars) |
|
214 | |||
215 | /** |
||
216 | * Set field legend |
||
217 | * |
||
218 | * @param string $field |
||
219 | * @param string|array $vars |
||
220 | * @return boolean |
||
221 | */ |
||
222 | 7 | private function set_legend($field, $vars) |
|
236 | |||
237 | /** |
||
238 | * Get field details |
||
239 | * |
||
240 | * @param array $vars |
||
241 | * @return mixed|string |
||
242 | */ |
||
243 | 7 | private function explain_field(array $vars) |
|
253 | |||
254 | /** |
||
255 | * Add text after field |
||
256 | * |
||
257 | * @param array $vars |
||
258 | * @return mixed|string |
||
259 | */ |
||
260 | 7 | private function append_field(array $vars) |
|
270 | |||
271 | /** |
||
272 | * Set field parameters |
||
273 | * |
||
274 | * @param string $field |
||
275 | * @param array $vars |
||
276 | * @param array $settings |
||
277 | */ |
||
278 | 7 | private function set_params($field, array &$vars, array $settings) |
|
286 | |||
287 | /** |
||
288 | * Get field value |
||
289 | * |
||
290 | * @param string $field |
||
291 | * @param mixed $default |
||
292 | * @param array $db_settings |
||
293 | * @return mixed |
||
294 | */ |
||
295 | 7 | private function get_field_value($field, $default, array $db_settings) |
|
299 | |||
300 | /** |
||
301 | * @param array $vars |
||
302 | */ |
||
303 | 1 | private function prep_select_field_for_display(array &$vars) |
|
309 | |||
310 | /** |
||
311 | * @param array $vars |
||
312 | * @param array $type |
||
313 | * @param string $field |
||
314 | */ |
||
315 | 3 | private function prep_checkbox_field_for_display(array &$vars, array &$type, $field) |
|
323 | |||
324 | /** |
||
325 | * @param array $vars |
||
326 | * @param array $type |
||
327 | * @param string $field |
||
328 | */ |
||
329 | 2 | private function prep_radio_field_for_display(array &$vars, array &$type, $field) |
|
340 | |||
341 | /** |
||
342 | * @param array $vars |
||
343 | * @param array $type |
||
344 | * @param string $field |
||
345 | */ |
||
346 | 1 | private function prep_multi_select_field_for_display(array &$vars, array &$type, $field) |
|
352 | |||
353 | /** |
||
354 | * @param array $vars |
||
355 | * @param array $type |
||
356 | */ |
||
357 | 1 | private function prep_hidden_field_for_display(array &$vars, array &$type) |
|
364 | |||
365 | /** |
||
366 | * @param array $vars |
||
367 | * @param array $type |
||
368 | */ |
||
369 | 1 | private function prep_custom_field_for_display(array &$vars, array &$type) |
|
374 | |||
375 | /** |
||
376 | * this looks bad but its the only way without modifying phpbb code |
||
377 | * this is for select items that do not need to be translated |
||
378 | * @param array $options |
||
379 | */ |
||
380 | 4 | private function add_lang_vars(array $options) |
|
390 | |||
391 | /** |
||
392 | * @param array $cfg_array |
||
393 | * @param array $df_settings |
||
394 | */ |
||
395 | 5 | private function get_multi_select(array &$cfg_array, array $df_settings) |
|
405 | } |
||
406 |