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 | * @var Filesystem |
||
| 50 | */ |
||
| 51 | private $fs; |
||
|
|
|||
| 52 | |||
| 53 | /** |
||
| 54 | * Configuration constructor. |
||
| 55 | */ |
||
| 56 | 43 | public function __construct() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Parse a given configuration file and configure this Configuration instance. |
||
| 64 | * |
||
| 65 | * 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 | * @param bool $ignoreDefaults When set to true, the default configuration will not be merged |
||
| 70 | */ |
||
| 71 | 43 | public function parseConfiguration($configFile = null, $ignoreDefaults = false) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 30 | public function setLogger(LoggerInterface $logger) |
|
| 110 | |||
| 111 | 14 | public function isDebug() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
| 118 | * |
||
| 119 | * @return mixed|null |
||
| 120 | */ |
||
| 121 | 4 | public function getBaseUrl() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @return string[] |
||
| 136 | */ |
||
| 137 | 1 | public function getDataFolders() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return string[] |
||
| 144 | */ |
||
| 145 | 1 | public function getDataSets() |
|
| 149 | |||
| 150 | 1 | public function getIncludes() |
|
| 154 | |||
| 155 | 1 | public function getExcludes() |
|
| 159 | |||
| 160 | 14 | public function getTheme() |
|
| 164 | |||
| 165 | 6 | public function getConfiguration() |
|
| 169 | |||
| 170 | 1 | public function getPageViewFolders() |
|
| 174 | |||
| 175 | 2 | public function getTargetFolder() |
|
| 179 | |||
| 180 | 1 | public function getCollectionsFolders() |
|
| 184 | |||
| 185 | 14 | public function getTwigAutoescape() |
|
| 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 | 43 | private function returnConfigOption($name, $default = null) |
|
| 207 | |||
| 208 | 43 | private function mergeDefaultConfiguration() |
|
| 231 | |||
| 232 | 43 | private function handleDeprecations() |
|
| 242 | |||
| 243 | 30 | private function handleImports() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Check whether a given file path is a valid import |
||
| 302 | * |
||
| 303 | * @param string $filePath |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | 10 | private function configImportIsValid($filePath) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Import a given configuration to be merged with the parent configuration |
||
| 343 | * |
||
| 344 | * @param string $filePath |
||
| 345 | * |
||
| 346 | * @return Configuration |
||
| 347 | */ |
||
| 348 | 6 | private function parseOther($filePath) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Merge the given array with existing configuration |
||
| 359 | * |
||
| 360 | * @param array $importedConfig |
||
| 361 | */ |
||
| 362 | 5 | private function mergeImports(array $importedConfig) |
|
| 370 | } |
||
| 371 |
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.