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) |
|
105 | |||
106 | /** |
||
107 | * As a workaround to prevent mod_security and similar from preventing us |
||
108 | * from posting html/script, we use encodeURI before submitting data. |
||
109 | * This decodes the data before submitting to the database |
||
110 | * |
||
111 | * @param array $cfg_array |
||
112 | * @return array |
||
113 | */ |
||
114 | 6 | private function decode_source_html(array $cfg_array) |
|
122 | |||
123 | /** |
||
124 | * Get the html form |
||
125 | * |
||
126 | * @param array $block_data |
||
127 | * @return string |
||
128 | */ |
||
129 | 8 | private function get_form(array $block_data) |
|
148 | |||
149 | /** |
||
150 | * Generate block configuration fields |
||
151 | * |
||
152 | * @param array $db_settings |
||
153 | * @param array $default_settings |
||
154 | */ |
||
155 | 8 | private function generate_config_fields(array &$db_settings, array $default_settings) |
|
177 | |||
178 | /** |
||
179 | * Get the field html |
||
180 | * |
||
181 | * @param string $field |
||
182 | * @param array $db_settings |
||
183 | * @param array $vars |
||
184 | * @return string |
||
185 | */ |
||
186 | 7 | private function get_field_template($field, array &$db_settings, array &$vars) |
|
202 | |||
203 | /** |
||
204 | * Set field legend |
||
205 | * |
||
206 | * @param string $field |
||
207 | * @param string|array $vars |
||
208 | * @return boolean |
||
209 | */ |
||
210 | 7 | private function set_legend($field, $vars) |
|
224 | |||
225 | /** |
||
226 | * Get field details |
||
227 | * |
||
228 | * @param array $vars |
||
229 | * @return mixed|string |
||
230 | */ |
||
231 | 7 | private function explain_field(array $vars) |
|
241 | |||
242 | /** |
||
243 | * Add text after field |
||
244 | * |
||
245 | * @param array $vars |
||
246 | * @return mixed|string |
||
247 | */ |
||
248 | 7 | private function append_field(array $vars) |
|
258 | |||
259 | /** |
||
260 | * Set field parameters |
||
261 | * |
||
262 | * @param string $field |
||
263 | * @param array $vars |
||
264 | * @param array $settings |
||
265 | */ |
||
266 | 7 | private function set_params($field, array &$vars, array $settings) |
|
274 | |||
275 | /** |
||
276 | * Get field value |
||
277 | * |
||
278 | * @param string $field |
||
279 | * @param mixed $default |
||
280 | * @param array $db_settings |
||
281 | * @return mixed |
||
282 | */ |
||
283 | 7 | private function get_field_value($field, $default, array $db_settings) |
|
287 | |||
288 | /** |
||
289 | * @param array $vars |
||
290 | */ |
||
291 | 1 | private function prep_select_field_for_display(array &$vars) |
|
297 | |||
298 | /** |
||
299 | * @param array $vars |
||
300 | * @param array $type |
||
301 | * @param string $field |
||
302 | */ |
||
303 | 2 | private function prep_checkbox_field_for_display(array &$vars, array &$type, $field) |
|
311 | |||
312 | /** |
||
313 | * @param array $vars |
||
314 | * @param array $type |
||
315 | * @param string $field |
||
316 | */ |
||
317 | 2 | private function prep_radio_field_for_display(array &$vars, array &$type, $field) |
|
328 | |||
329 | /** |
||
330 | * @param array $vars |
||
331 | * @param array $type |
||
332 | * @param string $field |
||
333 | */ |
||
334 | 1 | private function prep_multi_select_field_for_display(array &$vars, array &$type, $field) |
|
340 | |||
341 | /** |
||
342 | * @param array $vars |
||
343 | * @param array $type |
||
344 | */ |
||
345 | 1 | private function prep_hidden_field_for_display(array &$vars, array &$type) |
|
352 | |||
353 | /** |
||
354 | * @param array $vars |
||
355 | * @param array $type |
||
356 | */ |
||
357 | 1 | private function prep_custom_field_for_display(array &$vars, array &$type) |
|
362 | |||
363 | /** |
||
364 | * this looks bad but its the only way without modifying phpbb code |
||
365 | * this is for select items that do not need to be translated |
||
366 | * @param array $options |
||
367 | */ |
||
368 | 4 | private function add_lang_vars(array $options) |
|
378 | |||
379 | /** |
||
380 | * @param array $cfg_array |
||
381 | * @param array $df_settings |
||
382 | */ |
||
383 | 5 | private function get_multi_select(array &$cfg_array, array $df_settings) |
|
393 | } |
||
394 |