Complex classes like AbstractConfig 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 AbstractConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class AbstractConfig implements ConfigurableInterface |
||
9 | { |
||
10 | /** |
||
11 | * Mapping table between class name and its configuration options set |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | private static $configCache = [ |
||
16 | 'classes_map' => [], |
||
17 | 'config' => [], |
||
18 | 'lazy_init' => [], |
||
19 | ]; |
||
20 | /** |
||
21 | * Configuration options |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $config; |
||
26 | /** |
||
27 | * List of configuration options that are not yet initialized |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $configPendingLazyInit = []; |
||
32 | /** |
||
33 | * TRUE if configuration options bootstrap is being performed, FALSE otherwise |
||
34 | * |
||
35 | * @var boolean |
||
36 | */ |
||
37 | private $configInBootstrap = false; |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 2 | public function isConfigExists($name) |
|
52 | |||
53 | /** |
||
54 | * Bootstrap object configuration options |
||
55 | * |
||
56 | * @return void |
||
57 | */ |
||
58 | 63 | protected function bootstrapConfig() |
|
80 | |||
81 | /** |
||
82 | * Get Id of configuration class that is used for given class |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 43 | protected function getConfigClassId() |
|
116 | |||
117 | /** |
||
118 | * Initialize list of configuration options |
||
119 | * |
||
120 | * This method is mean to be overridden to provide configuration options set. |
||
121 | * To allow inheritance of configuration options sets across several levels |
||
122 | * of inherited classes - this method in nested classes should look like this: |
||
123 | * |
||
124 | * <code> |
||
125 | * parent::initConfig(); |
||
126 | * $this->mergeConfig([ |
||
127 | * 'option' => 'default value', |
||
128 | * ]); |
||
129 | * </code> |
||
130 | * |
||
131 | * @return void |
||
132 | */ |
||
133 | 63 | protected function initConfig() |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 10 | public function modifyConfig(array $config, $modification, $value = null) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | * @param string|array|null $config |
||
169 | * @param boolean $export |
||
170 | * @return mixed |
||
171 | * @throws \RuntimeException |
||
172 | */ |
||
173 | 54 | public function getConfig($config = null, $export = false) |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 35 | public function setConfig($config, $value = null) |
|
251 | |||
252 | /** |
||
253 | * Attempt to convert given configuration information to array |
||
254 | * |
||
255 | * @param mixed $config Value to convert to array |
||
256 | * @param mixed $value OPTIONAL Array entry value for inline array entry |
||
257 | * @param boolean $inline OPTIONAL TRUE to allow treating given string values as array entry |
||
258 | * @return mixed |
||
259 | */ |
||
260 | 45 | protected function configToArray($config, $value = null, $inline = false) |
|
283 | |||
284 | /** |
||
285 | * Check that given value of configuration option is valid |
||
286 | * |
||
287 | * This method is mean to be overridden in a case if additional validation |
||
288 | * of configuration option value should be performed before using it |
||
289 | * Method should validate and, if required, normalize given value |
||
290 | * of configuration option and return true if option can be used and false if not |
||
291 | * It is important that this method will be: |
||
292 | * - as simple as possible to optimize performance |
||
293 | * - will not call other methods that attempts to modify or merge object configuration |
||
294 | * to avoid infinite loop |
||
295 | * Normally this method should look like this: |
||
296 | * |
||
297 | * <code> |
||
298 | * switch($name) { |
||
299 | * case 'option': |
||
300 | * // $value validation and normalization code |
||
301 | * break; |
||
302 | * default: |
||
303 | * return parent::validateConfig($name, $value); |
||
304 | * break; |
||
305 | * } |
||
306 | * </code> |
||
307 | * |
||
308 | * @param string $name Configuration option name |
||
309 | * @param mixed $value Option value (passed by reference) |
||
310 | * @return boolean |
||
311 | */ |
||
312 | 2 | protected function validateConfig($name, &$value) |
|
316 | |||
317 | /** |
||
318 | * Perform required operations when configuration option value is changed |
||
319 | * |
||
320 | * This method is mean to be overridden in a case if some kind of additional logic |
||
321 | * is required to be performed upon setting value of configuration option. |
||
322 | * |
||
323 | * @param string $name Configuration option name |
||
324 | * @param mixed $value Configuration option value |
||
325 | * @return void |
||
326 | */ |
||
327 | 10 | protected function onConfigChange($name, $value) |
|
330 | |||
331 | /** |
||
332 | * Resolve lazy initialization of configuration options |
||
333 | * |
||
334 | * @param string $name OPTIONAL Configuration option to perform lazy initialization of |
||
335 | * @throws \RuntimeException |
||
336 | * @return void |
||
337 | */ |
||
338 | 45 | protected function resolveLazyConfigInit($name = null) |
|
358 | |||
359 | /** |
||
360 | * Perform "lazy initialization" of configuration option with given name |
||
361 | * |
||
362 | * @param string $name Configuration option name |
||
363 | * @return mixed |
||
364 | */ |
||
365 | protected function lazyConfigInit($name) |
||
369 | |||
370 | /** |
||
371 | * Merge given configuration options with current configuration options |
||
372 | * |
||
373 | * @param array $config Configuration options to merge |
||
374 | * @throws \RuntimeException |
||
375 | * @throws \InvalidArgumentException |
||
376 | * @return void |
||
377 | */ |
||
378 | 65 | protected function mergeConfig(array $config) |
|
406 | } |
||
407 |