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 | const CACHE_FOLDER = '.stakx-cache'; |
||
| 25 | |||
| 26 | private static $configImports = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
||
| 30 | * compiled website as an asset. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/'); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * An array representation of the main Yaml configuration. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $configuration; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $parentConfig; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $currentFile; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var LoggerInterface |
||
| 53 | */ |
||
| 54 | private $output; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Filesystem |
||
| 58 | */ |
||
| 59 | private $fs; |
||
|
|
|||
| 60 | |||
| 61 | /** |
||
| 62 | * Configuration constructor. |
||
| 63 | 50 | */ |
|
| 64 | public function __construct() |
||
| 65 | 50 | { |
|
| 66 | 50 | $this->configuration = array(); |
|
| 67 | 50 | $this->fs = new Filesystem(); |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | 50 | */ |
|
| 73 | public function setLogger(LoggerInterface $logger) |
||
| 74 | 50 | { |
|
| 75 | 50 | $this->output = $logger; |
|
| 76 | } |
||
| 77 | |||
| 78 | /// |
||
| 79 | // Getters |
||
| 80 | /// |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return bool |
||
| 84 | 15 | */ |
|
| 85 | public function isDebug() |
||
| 86 | 15 | { |
|
| 87 | return $this->returnConfigOption('debug', false); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
| 92 | * |
||
| 93 | * @return mixed|null |
||
| 94 | 4 | */ |
|
| 95 | public function getBaseUrl() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string[] |
||
| 110 | 1 | */ |
|
| 111 | public function getDataFolders() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return string[] |
||
| 118 | 1 | */ |
|
| 119 | public function getDataSets() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return string[] |
||
| 126 | 1 | */ |
|
| 127 | public function getIncludes() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return string[] |
||
| 134 | 1 | */ |
|
| 135 | public function getExcludes() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | 15 | */ |
|
| 143 | public function getTheme() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | 7 | */ |
|
| 151 | public function getConfiguration() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return string[] |
||
| 158 | 1 | */ |
|
| 159 | public function getPageViewFolders() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | 2 | */ |
|
| 167 | public function getTargetFolder() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string[][] |
||
| 174 | 1 | */ |
|
| 175 | public function getCollectionsFolders() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return bool |
||
| 182 | 15 | */ |
|
| 183 | public function getTwigAutoescape() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return false|string |
||
| 190 | */ |
||
| 191 | public function getRedirectTemplate() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Return the specified configuration option if available, otherwise return the default. |
||
| 198 | * |
||
| 199 | * @param string $name The configuration option to lookup |
||
| 200 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
| 201 | * |
||
| 202 | * @return mixed|null |
||
| 203 | 50 | */ |
|
| 204 | private function returnConfigOption($name, $default = null) |
||
| 208 | |||
| 209 | /// |
||
| 210 | // Parsing |
||
| 211 | /// |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Safely read a YAML configuration file and return an array representation of it. |
||
| 215 | * |
||
| 216 | * This function will only read files from within the website folder. |
||
| 217 | * |
||
| 218 | * @param string $filePath |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | 36 | */ |
|
| 222 | private static function readFile($filePath) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Parse a configuration file. |
||
| 233 | * |
||
| 234 | * @param string|null $configFile |
||
| 235 | 50 | */ |
|
| 236 | public function parse($configFile = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Parse a given configuration file and return an associative array representation. |
||
| 250 | * |
||
| 251 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
| 252 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
| 253 | * |
||
| 254 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
| 255 | * used |
||
| 256 | * |
||
| 257 | * @return array |
||
| 258 | 50 | */ |
|
| 259 | private function parseConfig($configFile = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Merge the default configuration with the parsed configuration. |
||
| 300 | 50 | */ |
|
| 301 | private function mergeDefaultConfiguration() |
||
| 325 | |||
| 326 | /** |
||
| 327 | 50 | * Warn about deprecated keywords in the configuration file. |
|
| 328 | */ |
||
| 329 | private function handleDeprecations() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Recursively resolve imports for a given array. |
||
| 342 | * |
||
| 343 | * This modifies the array in place. |
||
| 344 | * |
||
| 345 | 36 | * @param array $configuration |
|
| 346 | */ |
||
| 347 | 36 | private function handleImports(array &$configuration) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Resolve a single import definition. |
||
| 375 | * |
||
| 376 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
| 377 | * configuration |
||
| 378 | * @param string $parentConfLoc The path to the parent configuration |
||
| 379 | 15 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
|
| 380 | */ |
||
| 381 | 15 | private function handleImport($importDef, $parentConfLoc, array &$configuration) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Check whether a given file path is a valid import. |
||
| 428 | * |
||
| 429 | * @param string $filePath |
||
| 430 | * |
||
| 431 | 12 | * @return bool |
|
| 432 | */ |
||
| 433 | 12 | private function isValidImport($filePath) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Check whether or not a filename has already been imported in a given process. |
||
| 468 | * |
||
| 469 | 36 | * @param string $filePath |
|
| 470 | */ |
||
| 471 | 36 | private function isRecursiveImport($filePath) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Merge the given array with existing configuration. |
||
| 485 | * |
||
| 486 | * @param array $importedConfig |
||
| 487 | * @param array $existingConfig |
||
| 488 | * |
||
| 489 | 7 | * @return array |
|
| 490 | */ |
||
| 491 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
| 499 | } |
||
| 500 |
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.