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 | 52 | 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 | 9 | public function get_edit_form(array $block_data, array $default_settings) |
|
70 | |||
71 | /** |
||
72 | * @param array $default_settings |
||
73 | * @return array|void |
||
74 | */ |
||
75 | 6 | public function get_submitted_settings(array $default_settings) |
|
90 | |||
91 | /** |
||
92 | * @param array $default_settings |
||
93 | * @param array $cfg_array |
||
94 | * @return array |
||
95 | */ |
||
96 | 6 | protected function validate_block_settings(array $default_settings, array $cfg_array) |
|
110 | |||
111 | /** |
||
112 | * As a workaround to prevent mod_security and similar from preventing us |
||
113 | * from posting html/script, we use encodeURI before submitting data. |
||
114 | * This decodes the data before submitting to the database |
||
115 | * |
||
116 | * @param array $cfg_array |
||
117 | * @return array |
||
118 | */ |
||
119 | 6 | private function decode_source_html(array $cfg_array) |
|
128 | |||
129 | /** |
||
130 | * Get the html form |
||
131 | * |
||
132 | * @param array $block_data |
||
133 | * @return string |
||
134 | */ |
||
135 | 9 | private function get_form(array $block_data) |
|
154 | |||
155 | /** |
||
156 | * Generate block configuration fields |
||
157 | * |
||
158 | * @param array $db_settings |
||
159 | * @param array $default_settings |
||
160 | */ |
||
161 | 9 | private function generate_config_fields(array &$db_settings, array $default_settings) |
|
183 | |||
184 | /** |
||
185 | * Get the field html |
||
186 | * |
||
187 | * @param string $field |
||
188 | * @param array $db_settings |
||
189 | * @param array $vars |
||
190 | * @return string |
||
191 | */ |
||
192 | 8 | private function get_field_template($field, array &$db_settings, array &$vars) |
|
224 | |||
225 | /** |
||
226 | * Set field legend |
||
227 | * |
||
228 | * @param string $field |
||
229 | * @param string|array $vars |
||
230 | * @return boolean |
||
231 | */ |
||
232 | 8 | private function set_legend($field, $vars) |
|
246 | |||
247 | /** |
||
248 | * Get field details |
||
249 | * |
||
250 | * @param array $vars |
||
251 | * @return mixed|string |
||
252 | */ |
||
253 | 8 | private function explain_field(array $vars) |
|
263 | |||
264 | /** |
||
265 | * Add text after field |
||
266 | * |
||
267 | * @param array $vars |
||
268 | * @return mixed|string |
||
269 | */ |
||
270 | 8 | private function append_field(array $vars) |
|
280 | |||
281 | /** |
||
282 | * Set field parameters |
||
283 | * |
||
284 | * @param string $field |
||
285 | * @param array $vars |
||
286 | * @param array $settings |
||
287 | */ |
||
288 | 8 | private function set_params($field, array &$vars, array $settings) |
|
296 | |||
297 | /** |
||
298 | * Get field value |
||
299 | * |
||
300 | * @param string $field |
||
301 | * @param mixed $default |
||
302 | * @param array $db_settings |
||
303 | * @return mixed |
||
304 | */ |
||
305 | 8 | private function get_field_value($field, $default, array $db_settings) |
|
309 | |||
310 | /** |
||
311 | * @param array $vars |
||
312 | */ |
||
313 | 2 | private function prep_select_field_for_display(array &$vars) |
|
319 | |||
320 | /** |
||
321 | * @param array $vars |
||
322 | * @param array $type |
||
323 | * @param string $field |
||
324 | */ |
||
325 | 2 | private function prep_checkbox_field_for_display(array &$vars, array &$type, $field) |
|
333 | |||
334 | /** |
||
335 | * @param array $vars |
||
336 | * @param array $type |
||
337 | * @param string $field |
||
338 | */ |
||
339 | 2 | private function prep_radio_field_for_display(array &$vars, array &$type, $field) |
|
350 | |||
351 | /** |
||
352 | * @param array $vars |
||
353 | * @param array $type |
||
354 | * @param string $field |
||
355 | */ |
||
356 | 1 | private function prep_multi_select_field_for_display(array &$vars, array &$type, $field) |
|
362 | |||
363 | /** |
||
364 | * @param array $vars |
||
365 | * @param array $type |
||
366 | */ |
||
367 | 1 | private function prep_hidden_field_for_display(array &$vars, array &$type) |
|
374 | |||
375 | /** |
||
376 | * @param array $vars |
||
377 | * @param array $type |
||
378 | */ |
||
379 | 1 | private function prep_custom_field_for_display(array &$vars, array &$type) |
|
384 | |||
385 | /** |
||
386 | * this looks bad but its the only way without modifying phpbb code |
||
387 | * this is for select items that do not need to be translated |
||
388 | * @param array $options |
||
389 | */ |
||
390 | 4 | private function add_lang_vars(array $options) |
|
400 | |||
401 | /** |
||
402 | * @param array $cfg_array |
||
403 | * @param array $df_settings |
||
404 | */ |
||
405 | 5 | private function get_multi_select(array &$cfg_array, array $df_settings) |
|
415 | } |
||
416 |