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 |
||
18 | class Configuration implements LoggerAwareInterface |
||
19 | { |
||
20 | const DEFAULT_NAME = '_config.yml'; |
||
21 | const IMPORT_KEYWORD = 'import'; |
||
22 | |||
23 | /** |
||
24 | * A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
||
25 | * compiled website as an asset. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/'); |
||
30 | |||
31 | /** |
||
32 | * An array representation of the main Yaml configuration. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private $configuration; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $configFile; |
||
42 | |||
43 | /** |
||
44 | * @var LoggerInterface |
||
45 | */ |
||
46 | private $output; |
||
47 | |||
48 | /** |
||
49 | 30 | * @var Filesystem |
|
50 | */ |
||
51 | 30 | private $fs; |
|
|
|||
52 | 30 | ||
53 | 30 | /** |
|
54 | * Configuration constructor. |
||
55 | */ |
||
56 | public function __construct() |
||
57 | { |
||
58 | $this->configuration = array(); |
||
59 | $this->fs = new Filesystem(); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | 30 | * Parse a given configuration file and configure this Configuration instance. |
|
64 | * |
||
65 | 30 | * This function should be called with 'null' passed when "configuration-less" mode is used |
|
66 | * |
||
67 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
68 | * used |
||
69 | 17 | * @param bool $ignoreDefaults When set to true, the default configuration will not be merged |
|
70 | */ |
||
71 | public function parseConfiguration($configFile = null, $ignoreDefaults = false) |
||
72 | { |
||
73 | $this->configFile = $configFile; |
||
74 | |||
75 | if ($this->fs->exists($configFile)) |
||
76 | { |
||
77 | $configContents = $this->fs->safeReadFile($this->configFile); |
||
78 | |||
79 | try |
||
80 | 30 | { |
|
81 | 30 | $this->configuration = Yaml::parse($configContents); |
|
82 | 30 | } |
|
83 | catch (ParseException $e) |
||
84 | { |
||
85 | $this->output->error('Parsing the configuration failed: {message}', array( |
||
86 | 'message' => $e->getMessage(), |
||
87 | 17 | )); |
|
88 | $this->output->error('Using default configuration...'); |
||
89 | 17 | } |
|
90 | 17 | ||
91 | $this->handleImports(); |
||
92 | 14 | } |
|
93 | |||
94 | 14 | !$ignoreDefaults && $this->mergeDefaultConfiguration(); |
|
95 | |||
96 | $this->handleDeprecations(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 4 | public function setLogger(LoggerInterface $logger) |
|
106 | |||
107 | 4 | public function isDebug() |
|
111 | |||
112 | 1 | /** |
|
113 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
114 | * |
||
115 | * @return mixed|null |
||
116 | */ |
||
117 | public function getBaseUrl() |
||
118 | 1 | { |
|
119 | $base = $this->returnConfigOption('base'); |
||
120 | 1 | $baseUrl = $this->returnConfigOption('baseurl'); |
|
121 | |||
122 | if (is_null($base) || (!empty($baseUrl))) |
||
123 | { |
||
124 | return $baseUrl; |
||
125 | } |
||
126 | 1 | ||
127 | return $base; |
||
128 | 1 | } |
|
129 | |||
130 | /** |
||
131 | 1 | * @return string[] |
|
132 | */ |
||
133 | 1 | public function getDataFolders() |
|
137 | |||
138 | 1 | /** |
|
139 | * @return string[] |
||
140 | */ |
||
141 | 14 | public function getDataSets() |
|
145 | |||
146 | 1 | public function getIncludes() |
|
150 | |||
151 | 1 | public function getExcludes() |
|
155 | |||
156 | 3 | public function getTheme() |
|
160 | |||
161 | 1 | public function getConfiguration() |
|
165 | |||
166 | 14 | public function getPageViewFolders() |
|
170 | |||
171 | public function getTargetFolder() |
||
172 | { |
||
173 | return $this->returnConfigOption('target'); |
||
174 | } |
||
175 | |||
176 | public function getCollectionsFolders() |
||
177 | { |
||
178 | return $this->returnConfigOption('collections'); |
||
179 | } |
||
180 | |||
181 | public function getTwigAutoescape() |
||
182 | { |
||
183 | return $this->configuration['twig']['autoescape']; |
||
184 | 30 | } |
|
185 | |||
186 | 30 | public function getRedirectTemplate() |
|
187 | { |
||
188 | return $this->configuration['templates']['redirect']; |
||
189 | 30 | } |
|
190 | |||
191 | /** |
||
192 | 30 | * Return the specified configuration option if available, otherwise return the default. |
|
193 | 30 | * |
|
194 | * @param string $name The configuration option to lookup |
||
195 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
196 | * |
||
197 | * @return mixed|null |
||
198 | */ |
||
199 | private function returnConfigOption($name, $default = null) |
||
203 | 30 | ||
204 | private function mergeDefaultConfiguration() |
||
205 | { |
||
206 | $defaultConfig = array( |
||
207 | 'baseurl' => '', |
||
208 | 'target' => '_site', |
||
209 | 'twig' => array( |
||
210 | 30 | 'autoescape' => false, |
|
227 | |||
228 | private function handleDeprecations() |
||
238 | |||
239 | private function handleImports() |
||
281 | |||
282 | /** |
||
283 | * Check whether a given file path is a valid import |
||
284 | * |
||
285 | * @param string $filePath |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | private function configImportIsValid($filePath) |
||
322 | |||
323 | /** |
||
324 | * Import a given configuration to be merged with the parent configuration |
||
325 | * |
||
326 | * @param string $filePath |
||
327 | * |
||
328 | * @return Configuration |
||
329 | */ |
||
330 | private function parseOther($filePath) |
||
338 | |||
339 | /** |
||
340 | * Merge the given array with existing configuration |
||
341 | * |
||
342 | * @param array $importedConfig |
||
343 | */ |
||
344 | private function mergeImports(array $importedConfig) |
||
352 | } |
||
353 |
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.