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 | 45 | 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) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | 45 | public function setLogger(LoggerInterface $logger) |
|
| 141 | |||
| 142 | 14 | public function isDebug() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
| 149 | * |
||
| 150 | * @return mixed|null |
||
| 151 | */ |
||
| 152 | 4 | public function getBaseUrl() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @return string[] |
||
| 167 | */ |
||
| 168 | 1 | public function getDataFolders() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @return string[] |
||
| 175 | */ |
||
| 176 | 1 | public function getDataSets() |
|
| 180 | |||
| 181 | 1 | public function getIncludes() |
|
| 185 | |||
| 186 | 1 | public function getExcludes() |
|
| 190 | |||
| 191 | 14 | public function getTheme() |
|
| 195 | |||
| 196 | 7 | public function getConfiguration() |
|
| 200 | |||
| 201 | 1 | public function getPageViewFolders() |
|
| 205 | |||
| 206 | 2 | public function getTargetFolder() |
|
| 210 | |||
| 211 | 1 | public function getCollectionsFolders() |
|
| 215 | |||
| 216 | 14 | public function getTwigAutoescape() |
|
| 220 | |||
| 221 | public function getRedirectTemplate() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return the specified configuration option if available, otherwise return the default. |
||
| 228 | * |
||
| 229 | * @param string $name The configuration option to lookup |
||
| 230 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
| 231 | * |
||
| 232 | * @return mixed|null |
||
| 233 | */ |
||
| 234 | 45 | private function returnConfigOption($name, $default = null) |
|
| 238 | |||
| 239 | 45 | private function mergeDefaultConfiguration() |
|
| 262 | |||
| 263 | 45 | private function handleDeprecations() |
|
| 273 | |||
| 274 | 45 | private function handleImports(&$configuration) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Check whether a given file path is a valid import |
||
| 343 | * |
||
| 344 | * @param string $filePath |
||
| 345 | * |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | 12 | private function isValidImport($filePath) |
|
| 381 | |||
| 382 | 45 | private function isRecursiveImport($filePath) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Merge the given array with existing configuration |
||
| 396 | * |
||
| 397 | * @param array $importedConfig |
||
| 398 | * @param array $existingConfig |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
| 410 | } |
||
| 411 |
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.