Complex classes like PodsConfig 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 PodsConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class PodsConfig { |
||
8 | |||
9 | /** |
||
10 | * @var array List of registered config types. |
||
11 | */ |
||
12 | protected $registered_config_types = array( |
||
|
|||
13 | 'json' => 'json', |
||
14 | 'yml' => 'yml', |
||
15 | ); |
||
16 | |||
17 | /** |
||
18 | * @var array List of registered config item types. |
||
19 | */ |
||
20 | protected $registered_config_item_types = array( |
||
21 | 'pods' => 'pods', |
||
22 | 'templates' => 'templates', |
||
23 | 'pages' => 'pages', |
||
24 | 'helpers' => 'helpers', |
||
25 | ); |
||
26 | |||
27 | /** |
||
28 | * @var array List of registered paths. |
||
29 | */ |
||
30 | protected $registered_paths = array(); |
||
31 | |||
32 | /** |
||
33 | * @var array List of registered Pods configs. |
||
34 | */ |
||
35 | protected $pods = array(); |
||
36 | |||
37 | /** |
||
38 | * @var array List of registered Pods Template configs. |
||
39 | */ |
||
40 | protected $templates = array(); |
||
41 | |||
42 | /** |
||
43 | * @var array List of registered Pods Page configs. |
||
44 | */ |
||
45 | protected $pages = array(); |
||
46 | |||
47 | /** |
||
48 | * @var array List of registered Pods Helper configs. |
||
49 | */ |
||
50 | protected $helpers = array(); |
||
51 | |||
52 | /** |
||
53 | * @var array Associative array list of other registered configs. |
||
54 | */ |
||
55 | protected $custom_configs = array(); |
||
56 | |||
57 | /** |
||
58 | * @var array List of config names for each file path. |
||
59 | */ |
||
60 | protected $file_path_configs = array(); |
||
61 | |||
62 | /** |
||
63 | * PodsConfig constructor. |
||
64 | */ |
||
65 | public function __construct() { |
||
68 | |||
69 | /** |
||
70 | * Setup initial registered paths and load configs. |
||
71 | */ |
||
72 | public function setup() { |
||
85 | |||
86 | /** |
||
87 | * Register a config type. |
||
88 | * |
||
89 | * @param string $config_type Config type. |
||
90 | */ |
||
91 | public function register_config_type( string $config_type ) { |
||
99 | |||
100 | /** |
||
101 | * Unregister a config type. |
||
102 | * |
||
103 | * @param string $config_type Config type. |
||
104 | */ |
||
105 | public function unregister_config_type( string $config_type ) { |
||
115 | |||
116 | /** |
||
117 | * Register a config item type. |
||
118 | * |
||
119 | * @param string $item_type Config item type. |
||
120 | */ |
||
121 | public function register_config_item_type( string $item_type ) { |
||
129 | |||
130 | /** |
||
131 | * Unregister a config item type. |
||
132 | * |
||
133 | * @param string $item_type Config item type. |
||
134 | */ |
||
135 | public function unregister_config_item_type( string $item_type ) { |
||
145 | |||
146 | /** |
||
147 | * Register a config file path. |
||
148 | * |
||
149 | * @param string $path File path. |
||
150 | */ |
||
151 | public function register_path( string $path ) { |
||
162 | |||
163 | /** |
||
164 | * Unregister a config file path. |
||
165 | * |
||
166 | * @param string $path File path. |
||
167 | */ |
||
168 | public function unregister_path( string $path ) { |
||
181 | |||
182 | /** |
||
183 | * Get file configs based on registered config types and config item types. |
||
184 | * |
||
185 | * @return array File configs. |
||
186 | */ |
||
187 | public function get_file_configs() { |
||
221 | |||
222 | /** |
||
223 | * Load configs from registered file paths. |
||
224 | */ |
||
225 | public function load_configs() { |
||
262 | |||
263 | /** |
||
264 | * Load config from registered file path. |
||
265 | * |
||
266 | * @param string $config_type Config type. |
||
267 | * @param string $raw_config Raw config content. |
||
268 | * @param string $file_path File path. |
||
269 | */ |
||
270 | public function load_config( string $config_type, string $raw_config, string $file_path ) { |
||
298 | |||
299 | /** |
||
300 | * Register config for different item types. |
||
301 | * |
||
302 | * @param array $config Config data. |
||
303 | * @param string $file_path Config file path. |
||
304 | */ |
||
305 | public function register_config( array $config, string $file_path = '' ) { |
||
334 | |||
335 | /** |
||
336 | * Register pod configs. |
||
337 | * |
||
338 | * @param array $items Config items. |
||
339 | * @param string $file_path Config file path. |
||
340 | */ |
||
341 | public function register_config_pods( array $items, string $file_path = '' ) { |
||
363 | |||
364 | /** |
||
365 | * Register template configs. |
||
366 | * |
||
367 | * @param array $items Config items. |
||
368 | * @param string $file_path Config file path. |
||
369 | */ |
||
370 | public function register_config_templates( array $items, string $file_path = '' ) { |
||
388 | |||
389 | /** |
||
390 | * Register page configs. |
||
391 | * |
||
392 | * @param array $items Config items. |
||
393 | * @param string $file_path Config file path. |
||
394 | */ |
||
395 | public function register_config_pages( array $items, string $file_path = '' ) { |
||
413 | |||
414 | /** |
||
415 | * Register helper configs. |
||
416 | * |
||
417 | * @param array $items Config items. |
||
418 | * @param string $file_path Config file path. |
||
419 | */ |
||
420 | public function register_config_helpers( array $items, string $file_path = '' ) { |
||
438 | |||
439 | /** |
||
440 | * Register config items for custom config item type. |
||
441 | * |
||
442 | * @param string $item_type Config Item type. |
||
443 | * @param array $items Config items. |
||
444 | * @param string $file_path Config file path. |
||
445 | */ |
||
446 | public function register_config_custom_item_type( string $item_type, array $items, string $file_path = '' ) { |
||
479 | |||
480 | } |
||
481 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.