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 | 27 | 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 | 27 | public function parseConfiguration($configFile = null, $ignoreDefaults = false) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | 14 | public function setLogger(LoggerInterface $logger) |
|
| 106 | |||
| 107 | 14 | public function isDebug() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
| 114 | * |
||
| 115 | * @return mixed|null |
||
| 116 | */ |
||
| 117 | 2 | public function getBaseUrl() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @return string[] |
||
| 132 | */ |
||
| 133 | 1 | public function getDataFolders() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return string[] |
||
| 140 | */ |
||
| 141 | 1 | public function getDataSets() |
|
| 145 | |||
| 146 | 1 | public function getIncludes() |
|
| 150 | |||
| 151 | 1 | public function getExcludes() |
|
| 155 | |||
| 156 | 14 | public function getTheme() |
|
| 160 | |||
| 161 | 1 | public function getConfiguration() |
|
| 165 | |||
| 166 | 1 | public function getPageViewFolders() |
|
| 170 | |||
| 171 | 2 | public function getTargetFolder() |
|
| 175 | |||
| 176 | 1 | public function getCollectionsFolders() |
|
| 180 | |||
| 181 | 14 | public function getTwigAutoescape() |
|
| 185 | |||
| 186 | public function getRedirectTemplate() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return the specified configuration option if available, otherwise return the default. |
||
| 193 | * |
||
| 194 | * @param string $name The configuration option to lookup |
||
| 195 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
| 196 | * |
||
| 197 | * @return mixed|null |
||
| 198 | */ |
||
| 199 | 27 | private function returnConfigOption($name, $default = null) |
|
| 203 | |||
| 204 | 27 | private function mergeDefaultConfiguration() |
|
| 227 | |||
| 228 | 27 | private function handleDeprecations() |
|
| 238 | |||
| 239 | 14 | private function handleImports() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Check whether a given file path is a valid import |
||
| 284 | * |
||
| 285 | * @param string $filePath |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | private function configImportIsValid($filePath) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Import a given configuration to be merged with the parent configuration |
||
| 325 | * |
||
| 326 | * @param string $filePath |
||
| 327 | * |
||
| 328 | * @return Configuration |
||
| 329 | */ |
||
| 330 | private function parseOther($filePath) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Merge the given array with existing configuration |
||
| 341 | * |
||
| 342 | * @param array $importedConfig |
||
| 343 | */ |
||
| 344 | private function mergeImports(array $importedConfig) |
||
| 352 | } |
||
| 353 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.