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() |
|
64 | { |
||
65 | 45 | $this->configuration = array(); |
|
66 | 45 | $this->fs = new Filesystem(); |
|
67 | 45 | } |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 45 | public function setLogger(LoggerInterface $logger) |
|
73 | { |
||
74 | 45 | $this->output = $logger; |
|
75 | 45 | } |
|
76 | |||
77 | /// |
||
78 | // Getters |
||
79 | /// |
||
80 | |||
81 | /** |
||
82 | * @return bool |
||
83 | */ |
||
84 | 14 | public function isDebug() |
|
85 | { |
||
86 | 14 | return $this->returnConfigOption('debug', false); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
91 | * |
||
92 | * @return mixed|null |
||
93 | */ |
||
94 | 4 | public function getBaseUrl() |
|
95 | { |
||
96 | 4 | $base = $this->returnConfigOption('base'); |
|
97 | 4 | $baseUrl = $this->returnConfigOption('baseurl'); |
|
98 | |||
99 | 4 | if (is_null($base) || (!empty($baseUrl))) |
|
100 | 4 | { |
|
101 | 3 | return $baseUrl; |
|
102 | } |
||
103 | |||
104 | 1 | return $base; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return string[] |
||
109 | */ |
||
110 | 1 | public function getDataFolders() |
|
111 | { |
||
112 | 1 | return $this->returnConfigOption('data'); |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return string[] |
||
117 | */ |
||
118 | 1 | public function getDataSets() |
|
119 | { |
||
120 | 1 | return $this->returnConfigOption('datasets'); |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return string[] |
||
125 | */ |
||
126 | 1 | public function getIncludes() |
|
127 | { |
||
128 | 1 | return $this->returnConfigOption('include', array()); |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return string[] |
||
133 | */ |
||
134 | 1 | public function getExcludes() |
|
135 | { |
||
136 | 1 | return $this->returnConfigOption('exclude', array()); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | 14 | public function getTheme() |
|
143 | { |
||
144 | 14 | return $this->returnConfigOption('theme'); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return array |
||
149 | */ |
||
150 | 7 | public function getConfiguration() |
|
151 | { |
||
152 | 7 | return $this->configuration; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return string[] |
||
157 | */ |
||
158 | 1 | public function getPageViewFolders() |
|
159 | { |
||
160 | 1 | return $this->returnConfigOption('pageviews'); |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | 2 | public function getTargetFolder() |
|
167 | { |
||
168 | 2 | return $this->returnConfigOption('target'); |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return string[][] |
||
173 | */ |
||
174 | 1 | public function getCollectionsFolders() |
|
175 | { |
||
176 | 1 | return $this->returnConfigOption('collections'); |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return bool |
||
181 | */ |
||
182 | 14 | public function getTwigAutoescape() |
|
183 | { |
||
184 | 14 | return $this->configuration['twig']['autoescape']; |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return false|string |
||
189 | */ |
||
190 | public function getRedirectTemplate() |
||
194 | |||
195 | /** |
||
196 | * Return the specified configuration option if available, otherwise return the default. |
||
197 | * |
||
198 | * @param string $name The configuration option to lookup |
||
199 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
200 | * |
||
201 | * @return mixed|null |
||
202 | */ |
||
203 | 45 | private function returnConfigOption($name, $default = null) |
|
207 | |||
208 | /// |
||
209 | // Parsing |
||
210 | /// |
||
211 | |||
212 | /** |
||
213 | * Safely read a YAML configuration file and return an array representation of it. |
||
214 | * |
||
215 | * This function will only read files from within the website folder. |
||
216 | * |
||
217 | * @param string $filePath |
||
218 | * |
||
219 | * @return array |
||
220 | */ |
||
221 | 32 | private static function readFile($filePath) |
|
229 | |||
230 | /** |
||
231 | * Parse a configuration file. |
||
232 | * |
||
233 | * @param string|null $configFile |
||
234 | */ |
||
235 | 45 | public function parse($configFile = null) |
|
246 | |||
247 | /** |
||
248 | * Parse a given configuration file and return an associative array representation. |
||
249 | * |
||
250 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
251 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
252 | * |
||
253 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
254 | * used |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 45 | private function parseConfig($configFile = null) |
|
296 | |||
297 | /** |
||
298 | * Merge the default configuration with the parsed configuration. |
||
299 | */ |
||
300 | 45 | private function mergeDefaultConfiguration() |
|
323 | |||
324 | /** |
||
325 | * Warn about deprecated keywords in the configuration file. |
||
326 | */ |
||
327 | 45 | private function handleDeprecations() |
|
337 | |||
338 | /** |
||
339 | * Recursively resolve imports for a given array. |
||
340 | * |
||
341 | * This modifies the array in place. |
||
342 | * |
||
343 | * @param array $configuration |
||
344 | */ |
||
345 | 32 | private function handleImports(array &$configuration) |
|
411 | |||
412 | /** |
||
413 | * Check whether a given file path is a valid import. |
||
414 | * |
||
415 | * @param string $filePath |
||
416 | * |
||
417 | * @return bool |
||
418 | */ |
||
419 | 12 | private function isValidImport($filePath) |
|
452 | |||
453 | /** |
||
454 | * Check whether or not a filename has already been imported in a given process. |
||
455 | * |
||
456 | * @param string $filePath |
||
457 | */ |
||
458 | 32 | private function isRecursiveImport($filePath) |
|
469 | |||
470 | /** |
||
471 | * Merge the given array with existing configuration. |
||
472 | * |
||
473 | * @param array $importedConfig |
||
474 | * @param array $existingConfig |
||
475 | * |
||
476 | * @return array |
||
477 | */ |
||
478 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
486 | } |
||
487 |
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.