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 DEFAULT_NAME = '_config.yml'; |
||
23 | const IMPORT_KEYWORD = 'import'; |
||
24 | |||
25 | private static $configImports = array(); |
||
26 | |||
27 | /** |
||
28 | * A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
||
29 | * compiled website as an asset. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/'); |
||
34 | |||
35 | /** |
||
36 | * An array representation of the main Yaml configuration. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private $configuration; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $parentConfig; |
||
46 | |||
47 | /** @var string */ |
||
48 | private $currentFile; |
||
49 | |||
50 | /** |
||
51 | * @var LoggerInterface |
||
52 | */ |
||
53 | private $output; |
||
54 | |||
55 | /** |
||
56 | * @var Filesystem |
||
57 | */ |
||
58 | private $fs; |
||
|
|||
59 | |||
60 | /** |
||
61 | * Configuration constructor. |
||
62 | */ |
||
63 | 45 | public function __construct() |
|
68 | |||
69 | 32 | private static function readFile($filePath) |
|
77 | |||
78 | 45 | public function parse($configFile = null) |
|
89 | |||
90 | /** |
||
91 | * Parse a given configuration file and configure this Configuration instance. |
||
92 | * |
||
93 | * This function should be called with 'null' passed when "configuration-less" mode is used |
||
94 | * |
||
95 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
96 | * used |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | 45 | private function parseConfig($configFile = null) |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 45 | public function setLogger(LoggerInterface $logger) |
|
146 | |||
147 | 14 | public function isDebug() |
|
151 | |||
152 | /** |
||
153 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
154 | * |
||
155 | * @return mixed|null |
||
156 | */ |
||
157 | 4 | public function getBaseUrl() |
|
169 | |||
170 | /** |
||
171 | * @return string[] |
||
172 | */ |
||
173 | 1 | public function getDataFolders() |
|
177 | |||
178 | /** |
||
179 | * @return string[] |
||
180 | */ |
||
181 | 1 | public function getDataSets() |
|
185 | |||
186 | 1 | public function getIncludes() |
|
190 | |||
191 | 1 | public function getExcludes() |
|
195 | |||
196 | 14 | public function getTheme() |
|
200 | |||
201 | 7 | public function getConfiguration() |
|
205 | |||
206 | 1 | public function getPageViewFolders() |
|
210 | |||
211 | 2 | public function getTargetFolder() |
|
215 | |||
216 | 1 | public function getCollectionsFolders() |
|
220 | |||
221 | 14 | public function getTwigAutoescape() |
|
225 | |||
226 | public function getRedirectTemplate() |
||
230 | |||
231 | /** |
||
232 | * Return the specified configuration option if available, otherwise return the default. |
||
233 | * |
||
234 | * @param string $name The configuration option to lookup |
||
235 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
236 | * |
||
237 | * @return mixed|null |
||
238 | */ |
||
239 | 45 | private function returnConfigOption($name, $default = null) |
|
243 | |||
244 | 45 | private function mergeDefaultConfiguration() |
|
267 | |||
268 | 45 | private function handleDeprecations() |
|
278 | |||
279 | 32 | private function handleImports(&$configuration) |
|
345 | |||
346 | /** |
||
347 | * Check whether a given file path is a valid import |
||
348 | * |
||
349 | * @param string $filePath |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | 12 | private function isValidImport($filePath) |
|
386 | |||
387 | 32 | private function isRecursiveImport($filePath) |
|
398 | |||
399 | /** |
||
400 | * Merge the given array with existing configuration |
||
401 | * |
||
402 | * @param array $importedConfig |
||
403 | * @param array $existingConfig |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
415 | } |
||
416 |
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.