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() |
|
67 | { |
||
68 | 50 | $this->configuration = array(); |
|
69 | 50 | $this->fs = new Filesystem(); |
|
70 | 50 | } |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 50 | public function setLogger(LoggerInterface $logger) |
|
76 | { |
||
77 | 50 | $this->output = $logger; |
|
78 | 50 | } |
|
79 | |||
80 | /// |
||
81 | // Getters |
||
82 | /// |
||
83 | |||
84 | /** |
||
85 | * @return bool |
||
86 | */ |
||
87 | 15 | public function isDebug() |
|
88 | { |
||
89 | 15 | return $this->returnConfigOption('debug', false); |
|
90 | } |
||
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() |
|
98 | { |
||
99 | 4 | $base = $this->returnConfigOption('base'); |
|
100 | 4 | $baseUrl = $this->returnConfigOption('baseurl'); |
|
101 | |||
102 | 4 | if (is_null($base) || (!empty($baseUrl))) |
|
103 | 4 | { |
|
104 | 3 | return $baseUrl; |
|
105 | } |
||
106 | |||
107 | 1 | return $base; |
|
108 | } |
||
109 | |||
110 | public function hasDataItems() |
||
111 | { |
||
112 | return ($this->getDataFolders() !== null || $this->getDataSets() !== null); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return string[] |
||
117 | */ |
||
118 | 1 | public function getDataFolders() |
|
122 | |||
123 | /** |
||
124 | * @return string[] |
||
125 | */ |
||
126 | 1 | public function getDataSets() |
|
130 | |||
131 | /** |
||
132 | * @return string[] |
||
133 | */ |
||
134 | 1 | public function getIncludes() |
|
135 | { |
||
138 | |||
139 | /** |
||
140 | * @return string[] |
||
141 | */ |
||
142 | 1 | public function getExcludes() |
|
146 | |||
147 | /** |
||
148 | * @return array |
||
149 | */ |
||
150 | public function getHighlighterCustomLanguages() |
||
154 | |||
155 | /** |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function isHighlighterEnabled() |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | 15 | public function getTheme() |
|
170 | |||
171 | /** |
||
172 | * @return array |
||
173 | */ |
||
174 | 7 | public function getConfiguration() |
|
178 | |||
179 | /** |
||
180 | * @return string[] |
||
181 | */ |
||
182 | 1 | public function getPageViewFolders() |
|
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | 50 | public function getTargetFolder() |
|
194 | |||
195 | public function hasCollections() |
||
199 | |||
200 | /** |
||
201 | * @return string[][] |
||
202 | */ |
||
203 | 1 | public function getCollectionsFolders() |
|
207 | |||
208 | /** |
||
209 | * @return bool |
||
210 | */ |
||
211 | 15 | public function getTwigAutoescape() |
|
215 | |||
216 | /** |
||
217 | * @return false|string |
||
218 | */ |
||
219 | public function getRedirectTemplate() |
||
223 | |||
224 | /** |
||
225 | * Return the specified configuration option if available, otherwise return the default. |
||
226 | * |
||
227 | * @param string $name The configuration option to lookup |
||
228 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
229 | * |
||
230 | * @return mixed|null |
||
231 | */ |
||
232 | 50 | private function returnConfigOption($name, $default = null) |
|
236 | |||
237 | /// |
||
238 | // Parsing |
||
239 | /// |
||
240 | |||
241 | /** |
||
242 | * Safely read a YAML configuration file and return an array representation of it. |
||
243 | * |
||
244 | * This function will only read files from within the website folder. |
||
245 | * |
||
246 | * @param string $filePath |
||
247 | * |
||
248 | * @return array |
||
249 | */ |
||
250 | 36 | private static function readFile($filePath) |
|
258 | |||
259 | /** |
||
260 | * Parse a configuration file. |
||
261 | * |
||
262 | * @param string|null $configFile |
||
263 | */ |
||
264 | 50 | public function parse($configFile = null) |
|
276 | |||
277 | /** |
||
278 | * Parse a given configuration file and return an associative array representation. |
||
279 | * |
||
280 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
281 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
282 | * |
||
283 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
284 | * used |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | 50 | private function parseConfig($configFile = null) |
|
326 | |||
327 | /** |
||
328 | * Merge the default configuration with the parsed configuration. |
||
329 | */ |
||
330 | 50 | private function mergeDefaultConfiguration() |
|
361 | |||
362 | /** |
||
363 | * Warn about deprecated keywords in the configuration file. |
||
364 | */ |
||
365 | 50 | private function handleDeprecations() |
|
375 | |||
376 | /** |
||
377 | * Recursively resolve imports for a given array. |
||
378 | * |
||
379 | * This modifies the array in place. |
||
380 | * |
||
381 | * @param array $configuration |
||
382 | */ |
||
383 | 36 | private function handleImports(array &$configuration) |
|
408 | |||
409 | /** |
||
410 | * Resolve a single import definition. |
||
411 | * |
||
412 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
413 | * configuration |
||
414 | * @param string $parentConfLoc The path to the parent configuration |
||
415 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
||
416 | */ |
||
417 | 15 | private function handleImport($importDef, $parentConfLoc, array &$configuration) |
|
461 | |||
462 | /** |
||
463 | * Check whether a given file path is a valid import. |
||
464 | * |
||
465 | * @param string $filePath |
||
466 | * |
||
467 | * @return bool |
||
468 | */ |
||
469 | 12 | private function isValidImport($filePath) |
|
501 | |||
502 | /** |
||
503 | * Check whether or not a filename has already been imported in a given process. |
||
504 | * |
||
505 | * @param string $filePath |
||
506 | */ |
||
507 | 36 | private function isRecursiveImport($filePath) |
|
518 | |||
519 | /** |
||
520 | * Merge the given array with existing configuration. |
||
521 | * |
||
522 | * @param array $importedConfig |
||
523 | * @param array $existingConfig |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
535 | |||
536 | 50 | private function handleDefaultOperations() |
|
543 | } |
||
544 |
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.