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 | * @param array $config |
||
| 141 | * @param array|\ArrayAccess|\Iterator|object|string $modification |
||
| 142 | * @param mixed $value |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | 10 | public function modifyConfig(array $config, $modification, $value = null) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | * @param array|\ArrayAccess|\Iterator|object|string|null $config |
||
| 173 | * @param boolean $export |
||
| 174 | * @return mixed |
||
| 175 | * @throws \RuntimeException |
||
| 176 | */ |
||
| 177 | 54 | public function getConfig($config = null, $export = false) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | * @param array|\ArrayAccess|\Iterator|object|string $config |
||
| 231 | * @param mixed $value |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | 35 | public function setConfig($config, $value = null) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Attempt to convert given configuration information to array |
||
| 253 | * |
||
| 254 | * @param array|\ArrayAccess|\Iterator|object|string $config Value to convert to array |
||
| 255 | * @param mixed $value OPTIONAL Array entry value for inline array entry |
||
| 256 | * @param boolean $inline OPTIONAL TRUE to allow treating given string values as array entry |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | 45 | protected function configToArray($config, $value = null, $inline = false) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Check that given value of configuration option is valid |
||
| 284 | * |
||
| 285 | * This method is mean to be overridden in a case if additional validation |
||
| 286 | * of configuration option value should be performed before using it |
||
| 287 | * Method should validate and, if required, normalize given value |
||
| 288 | * of configuration option and return true if option can be used and false if not |
||
| 289 | * It is important that this method will be: |
||
| 290 | * - as simple as possible to optimize performance |
||
| 291 | * - will not call other methods that attempts to modify or merge object configuration |
||
| 292 | * to avoid infinite loop |
||
| 293 | * Normally this method should look like this: |
||
| 294 | * |
||
| 295 | * <code> |
||
| 296 | * switch($name) { |
||
| 297 | * case 'option': |
||
| 298 | * // $value validation and normalization code |
||
| 299 | * break; |
||
| 300 | * default: |
||
| 301 | * return parent::validateConfig($name, $value); |
||
| 302 | * break; |
||
| 303 | * } |
||
| 304 | * </code> |
||
| 305 | * |
||
| 306 | * @param string $name Configuration option name |
||
| 307 | * @param mixed $value Option value (passed by reference) |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | 2 | protected function validateConfig($name, &$value) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Perform required operations when configuration option value is changed |
||
| 317 | * |
||
| 318 | * This method is mean to be overridden in a case if some kind of additional logic |
||
| 319 | * is required to be performed upon setting value of configuration option. |
||
| 320 | * |
||
| 321 | * @param string $name Configuration option name |
||
| 322 | * @param mixed $value Configuration option value |
||
| 323 | * @return void |
||
| 324 | */ |
||
| 325 | 10 | protected function onConfigChange($name, $value) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Resolve lazy initialization of configuration options |
||
| 331 | * |
||
| 332 | * @param string $name OPTIONAL Configuration option to perform lazy initialization of |
||
| 333 | * @throws \RuntimeException |
||
| 334 | * @return void |
||
| 335 | */ |
||
| 336 | 45 | protected function resolveLazyConfigInit($name = null) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Perform "lazy initialization" of configuration option with given name |
||
| 359 | * |
||
| 360 | * @param string $name Configuration option name |
||
| 361 | * @return mixed |
||
| 362 | */ |
||
| 363 | protected function lazyConfigInit($name) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Merge given configuration options with current configuration options |
||
| 370 | * |
||
| 371 | * @param array $config Configuration options to merge |
||
| 372 | * @throws \RuntimeException |
||
| 373 | * @throws \InvalidArgumentException |
||
| 374 | * @return void |
||
| 375 | */ |
||
| 376 | 65 | protected function mergeConfig(array $config) |
|
| 404 | } |
||
| 405 |