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) |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 35 | public function setConfig($config, $value = null) |
|
243 | |||
244 | /** |
||
245 | * Attempt to convert given configuration information to array |
||
246 | * |
||
247 | * @param mixed $config Value to convert to array |
||
248 | * @param mixed $value OPTIONAL Array entry value for inline array entry |
||
249 | * @param boolean $inline OPTIONAL TRUE to allow treating given string values as array entry |
||
250 | * @return mixed |
||
251 | */ |
||
252 | 45 | protected function configToArray($config, $value = null, $inline = false) |
|
274 | |||
275 | /** |
||
276 | * Check that given value of configuration option is valid |
||
277 | * |
||
278 | * This method is mean to be overridden in a case if additional validation |
||
279 | * of configuration option value should be performed before using it |
||
280 | * Method should validate and, if required, normalize given value |
||
281 | * of configuration option and return true if option can be used and false if not |
||
282 | * It is important that this method will be: |
||
283 | * - as simple as possible to optimize performance |
||
284 | * - will not call other methods that attempts to modify or merge object configuration |
||
285 | * to avoid infinite loop |
||
286 | * Normally this method should look like this: |
||
287 | * |
||
288 | * <code> |
||
289 | * switch($name) { |
||
290 | * case 'option': |
||
291 | * // $value validation and normalization code |
||
292 | * break; |
||
293 | * default: |
||
294 | * return parent::validateConfig($name, $value); |
||
295 | * break; |
||
296 | * } |
||
297 | * </code> |
||
298 | * |
||
299 | * @param string $name Configuration option name |
||
300 | * @param mixed $value Option value (passed by reference) |
||
301 | * @return boolean |
||
302 | */ |
||
303 | 2 | protected function validateConfig($name, &$value) |
|
307 | |||
308 | /** |
||
309 | * Perform required operations when configuration option value is changed |
||
310 | * |
||
311 | * This method is mean to be overridden in a case if some kind of additional logic |
||
312 | * is required to be performed upon setting value of configuration option. |
||
313 | * |
||
314 | * @param string $name Configuration option name |
||
315 | * @param mixed $value Configuration option value |
||
316 | * @return void |
||
317 | */ |
||
318 | 10 | protected function onConfigChange($name, $value) |
|
321 | |||
322 | /** |
||
323 | * Resolve lazy initialization of configuration options |
||
324 | * |
||
325 | * @param string $name OPTIONAL Configuration option to perform lazy initialization of |
||
326 | * @throws \RuntimeException |
||
327 | * @return void |
||
328 | */ |
||
329 | 45 | protected function resolveLazyConfigInit($name = null) |
|
349 | |||
350 | /** |
||
351 | * Perform "lazy initialization" of configuration option with given name |
||
352 | * |
||
353 | * @param string $name Configuration option name |
||
354 | * @return mixed |
||
355 | */ |
||
356 | protected function lazyConfigInit($name) |
||
360 | |||
361 | /** |
||
362 | * Merge given configuration options with current configuration options |
||
363 | * |
||
364 | * @param array $config Configuration options to merge |
||
365 | * @throws \RuntimeException |
||
366 | * @throws \InvalidArgumentException |
||
367 | * @return void |
||
368 | */ |
||
369 | 65 | protected function mergeConfig(array $config) |
|
397 | } |
||
398 |