Complex classes like Config 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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Config implements ConfigInterface |
||
39 | { |
||
40 | /** |
||
41 | * @const string DEFAULT_CONFIG Name of the default configuration file |
||
42 | */ |
||
43 | const DEFAULT_CONFIG = 'config.default.json'; |
||
44 | |||
45 | /** |
||
46 | * The delimiter for values names as they are used externally |
||
47 | * |
||
48 | * @const string VALUE_NAME_DELIMITER |
||
49 | */ |
||
50 | const VALUE_NAME_DELIMITER = '/'; |
||
51 | |||
52 | /** |
||
53 | * @var string $context The context for this instance e.g. app based configurations |
||
54 | */ |
||
55 | protected $context; |
||
56 | |||
57 | /** |
||
58 | * @var array $config Configuration array |
||
59 | */ |
||
60 | protected $config = array(); |
||
61 | |||
62 | /** |
||
63 | * Default constructor |
||
64 | */ |
||
65 | public function __construct() |
||
69 | |||
70 | /** |
||
71 | * Will flatten a multi-level associative array into a one-level one |
||
72 | * |
||
73 | * @param array $array The array to flatten |
||
74 | * @param string $parentKey The key of the parent array, used within recursion |
||
75 | * @param bool $initialCall Is this the initial call or recursion? |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | protected function flattenArray(array $array, $parentKey = '', $initialCall = true) |
||
107 | |||
108 | /** |
||
109 | * Sets a value to specific content |
||
110 | * |
||
111 | * @param string $valueName The value to set |
||
112 | * @param mixed $value The actual content for the value |
||
113 | * |
||
114 | * @return void |
||
115 | */ |
||
116 | public function setValue($valueName, $value) |
||
121 | |||
122 | /** |
||
123 | * Might be called after the all config data has been loaded. |
||
124 | * Will store all config object instances which might be needed later in the instance container util. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function storeInstances() |
||
141 | |||
142 | /** |
||
143 | * Extends a value by specific content. If the value is an array it will be merged, otherwise it will be |
||
144 | * string-concatinated to the end of the current value |
||
145 | * |
||
146 | * @param string $valueName The value to extend |
||
147 | * @param string $value The actual content for the value we want to add to the original |
||
148 | * |
||
149 | * @return void |
||
150 | */ |
||
151 | public function extendValue($valueName, $value) |
||
172 | |||
173 | /** |
||
174 | * Will extract an logger instance from the configuration array. |
||
175 | * Returns false on error. |
||
176 | * |
||
177 | * @param array $configArray The config to extract the logger instance from |
||
178 | * |
||
179 | * @return object|boolean |
||
180 | */ |
||
181 | protected function extractLoggerInstance(array $configArray) |
||
197 | |||
198 | /** |
||
199 | * Unsets a specific config value |
||
200 | * |
||
201 | * @param string $value The value to unset |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function unsetValue($value) |
||
212 | |||
213 | /** |
||
214 | * Returns the content of a specific config value |
||
215 | * |
||
216 | * @param string $value The value to get the content for |
||
217 | * |
||
218 | * @throws \AppserverIo\Doppelgaenger\Exceptions\ConfigException |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | public function getValue($value) |
||
232 | |||
233 | /** |
||
234 | * Checks if value exists for given value |
||
235 | * |
||
236 | * @param string $value The value to check |
||
237 | * |
||
238 | * @return boolean Weather it has value (true) or not (false) |
||
239 | */ |
||
240 | public function hasValue($value) |
||
249 | |||
250 | /** |
||
251 | * Will load a certain configuration file into this instance. Might throw an exception if the file is not valid |
||
252 | * |
||
253 | * @param string $file The path of the configuration file we should load |
||
254 | * |
||
255 | * @return \AppserverIo\Doppelgaenger\Config |
||
256 | * |
||
257 | * @throws \AppserverIo\Doppelgaenger\Exceptions\ConfigException |
||
258 | */ |
||
259 | public function load($file) |
||
271 | |||
272 | /** |
||
273 | * Will validate a potential configuration file. Returns false if file is no valid Doppelgaenger configuration, true otherwise |
||
274 | * |
||
275 | * @param string $file Path of the potential configuration file |
||
276 | * |
||
277 | * @return boolean |
||
278 | * @throws \AppserverIo\Doppelgaenger\Exceptions\ConfigException |
||
279 | */ |
||
280 | public function isValidConfigFile($file) |
||
284 | |||
285 | /** |
||
286 | * Will normalize directories mentioned within a configuration aspect. |
||
287 | * If there is an error false will be returned. If not we will return the given configuration array containing only |
||
288 | * normalized paths. |
||
289 | * |
||
290 | * @param string $configAspect The aspect to check for non-normal dirs |
||
291 | * @param array $configArray The array to check within |
||
292 | * |
||
293 | * @return array|bool |
||
294 | */ |
||
295 | protected function normalizeConfigDirs($configAspect, array $configArray) |
||
331 | |||
332 | /** |
||
333 | * Will return the whole configuration or, if $aspect is given, certain parts of it |
||
334 | * |
||
335 | * @param string $aspect The aspect of the configuration we are interested in e.g. 'autoloader' |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | public function getConfig($aspect = null) |
||
359 | |||
360 | /** |
||
361 | * Will validate a potential configuration file. Returns false if file is no valid Doppelgaenger configuration. |
||
362 | * Will return the validated configuration on success |
||
363 | * |
||
364 | * @param string $file Path of the potential configuration file |
||
365 | * |
||
366 | * @return array|boolean |
||
367 | * @throws \AppserverIo\Doppelgaenger\Exceptions\ConfigException |
||
368 | */ |
||
369 | protected function validate($file) |
||
415 | |||
416 | /** |
||
417 | * Will return true if the processing part of the config candidate array is valid. Will return false if not |
||
418 | * |
||
419 | * @param array $configCandidate The config candidate we want to validate in terms of processing |
||
420 | * |
||
421 | * @return boolean |
||
422 | * |
||
423 | * @todo move everything other than logger check to JSON scheme validation |
||
424 | */ |
||
425 | protected function validateProcessing(array $configCandidate) |
||
451 | } |
||
452 |