Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Configuration implements LoggerAwareInterface |
||
21 | { |
||
22 | const HIGHLIGHTER_ENABLED = 'highlighter-enabled'; |
||
23 | |||
24 | const DEFAULT_NAME = '_config.yml'; |
||
25 | const IMPORT_KEYWORD = 'import'; |
||
26 | const CACHE_FOLDER = '.stakx-cache'; |
||
27 | |||
28 | private static $configImports = array(); |
||
29 | |||
30 | /** |
||
31 | * A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
||
32 | * compiled website as an asset. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/'); |
||
37 | |||
38 | /** |
||
39 | * An array representation of the main Yaml configuration. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | private $configuration; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $parentConfig; |
||
49 | |||
50 | /** @var string */ |
||
51 | private $currentFile; |
||
52 | |||
53 | /** |
||
54 | * @var LoggerInterface |
||
55 | */ |
||
56 | private $output; |
||
57 | |||
58 | /** |
||
59 | * @var Filesystem |
||
60 | */ |
||
61 | private $fs; |
||
|
|||
62 | |||
63 | /** |
||
64 | * Configuration constructor. |
||
65 | */ |
||
66 | 50 | public function __construct() |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 50 | public function setLogger(LoggerInterface $logger) |
|
79 | |||
80 | /// |
||
81 | // Getters |
||
82 | /// |
||
83 | |||
84 | /** |
||
85 | * @return bool |
||
86 | */ |
||
87 | 15 | public function isDebug() |
|
91 | |||
92 | /** |
||
93 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
94 | * |
||
95 | * @return mixed|null |
||
96 | */ |
||
97 | 4 | public function getBaseUrl() |
|
109 | |||
110 | /** |
||
111 | * @return string[] |
||
112 | */ |
||
113 | 1 | public function getDataFolders() |
|
117 | |||
118 | /** |
||
119 | * @return string[] |
||
120 | */ |
||
121 | 1 | public function getDataSets() |
|
125 | |||
126 | /** |
||
127 | * @return string[] |
||
128 | */ |
||
129 | 1 | public function getIncludes() |
|
133 | |||
134 | /** |
||
135 | * @return string[] |
||
136 | */ |
||
137 | 1 | public function getExcludes() |
|
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getHighlighterCustomLanguages() |
||
149 | |||
150 | /** |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isHighlighterEnabled() |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | 15 | public function getTheme() |
|
165 | |||
166 | /** |
||
167 | * @return array |
||
168 | */ |
||
169 | 7 | public function getConfiguration() |
|
173 | |||
174 | /** |
||
175 | * @return string[] |
||
176 | */ |
||
177 | 1 | public function getPageViewFolders() |
|
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | 2 | public function getTargetFolder() |
|
189 | |||
190 | /** |
||
191 | * @return string[][] |
||
192 | */ |
||
193 | 1 | public function getCollectionsFolders() |
|
197 | |||
198 | /** |
||
199 | * @return bool |
||
200 | */ |
||
201 | 15 | public function getTwigAutoescape() |
|
205 | |||
206 | /** |
||
207 | * @return false|string |
||
208 | */ |
||
209 | public function getRedirectTemplate() |
||
213 | |||
214 | /** |
||
215 | * Return the specified configuration option if available, otherwise return the default. |
||
216 | * |
||
217 | * @param string $name The configuration option to lookup |
||
218 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
219 | * |
||
220 | * @return mixed|null |
||
221 | */ |
||
222 | 50 | private function returnConfigOption($name, $default = null) |
|
226 | |||
227 | /// |
||
228 | // Parsing |
||
229 | /// |
||
230 | |||
231 | /** |
||
232 | * Safely read a YAML configuration file and return an array representation of it. |
||
233 | * |
||
234 | * This function will only read files from within the website folder. |
||
235 | * |
||
236 | * @param string $filePath |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | 36 | private static function readFile($filePath) |
|
248 | |||
249 | /** |
||
250 | * Parse a configuration file. |
||
251 | * |
||
252 | * @param string|null $configFile |
||
253 | */ |
||
254 | 50 | public function parse($configFile = null) |
|
265 | |||
266 | /** |
||
267 | * Parse a given configuration file and return an associative array representation. |
||
268 | * |
||
269 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
270 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
271 | * |
||
272 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
273 | * used |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | 50 | private function parseConfig($configFile = null) |
|
315 | |||
316 | /** |
||
317 | * Merge the default configuration with the parsed configuration. |
||
318 | */ |
||
319 | 50 | private function mergeDefaultConfiguration() |
|
347 | |||
348 | /** |
||
349 | * Warn about deprecated keywords in the configuration file. |
||
350 | */ |
||
351 | 50 | private function handleDeprecations() |
|
361 | |||
362 | /** |
||
363 | * Recursively resolve imports for a given array. |
||
364 | * |
||
365 | * This modifies the array in place. |
||
366 | * |
||
367 | * @param array $configuration |
||
368 | */ |
||
369 | 36 | private function handleImports(array &$configuration) |
|
394 | |||
395 | /** |
||
396 | * Resolve a single import definition. |
||
397 | * |
||
398 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
399 | * configuration |
||
400 | * @param string $parentConfLoc The path to the parent configuration |
||
401 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
||
402 | */ |
||
403 | 15 | private function handleImport($importDef, $parentConfLoc, array &$configuration) |
|
447 | |||
448 | /** |
||
449 | * Check whether a given file path is a valid import. |
||
450 | * |
||
451 | * @param string $filePath |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | 12 | private function isValidImport($filePath) |
|
487 | |||
488 | /** |
||
489 | * Check whether or not a filename has already been imported in a given process. |
||
490 | * |
||
491 | * @param string $filePath |
||
492 | */ |
||
493 | 36 | private function isRecursiveImport($filePath) |
|
504 | |||
505 | /** |
||
506 | * Merge the given array with existing configuration. |
||
507 | * |
||
508 | * @param array $importedConfig |
||
509 | * @param array $existingConfig |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
521 | } |
||
522 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.