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 | * Configuration constructor. |
||
| 60 | */ |
||
| 61 | 91 | public function __construct() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 50 | public function setLogger(LoggerInterface $logger) |
|
| 73 | |||
| 74 | /// |
||
| 75 | // Getters |
||
| 76 | /// |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function isDebug() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @TODO 1.0.0 Remove support for 'base' in next major release; it has been replaced by 'baseurl' |
||
| 88 | * |
||
| 89 | * @return mixed|null |
||
| 90 | */ |
||
| 91 | public function getBaseUrl() |
||
| 103 | |||
| 104 | public function hasDataItems() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return string[] |
||
| 111 | */ |
||
| 112 | public function getDataFolders() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string[] |
||
| 119 | */ |
||
| 120 | public function getDataSets() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string[] |
||
| 127 | */ |
||
| 128 | public function getIncludes() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string[] |
||
| 135 | */ |
||
| 136 | public function getExcludes() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getHighlighterCustomLanguages() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | public function isHighlighterEnabled() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function getTheme() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | public function getConfiguration() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return string[] |
||
| 175 | */ |
||
| 176 | public function getPageViewFolders() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | 14 | public function getTargetFolder() |
|
| 188 | |||
| 189 | public function hasCollections() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return string[][] |
||
| 196 | */ |
||
| 197 | public function getCollectionsFolders() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function getTwigAutoescape() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return false|string |
||
| 212 | */ |
||
| 213 | public function getRedirectTemplate() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Return the specified configuration option if available, otherwise return the default. |
||
| 220 | * |
||
| 221 | * @param string $name The configuration option to lookup |
||
| 222 | * @param mixed|null $default The default value returned if the configuration option isn't found |
||
| 223 | * |
||
| 224 | * @return mixed|null |
||
| 225 | */ |
||
| 226 | 14 | private function returnConfigOption($name, $default = null) |
|
| 230 | |||
| 231 | /// |
||
| 232 | // Parsing |
||
| 233 | /// |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Safely read a YAML configuration file and return an array representation of it. |
||
| 237 | * |
||
| 238 | * This function will only read files from within the website folder. |
||
| 239 | * |
||
| 240 | * @param string $filePath |
||
| 241 | * |
||
| 242 | * @return array |
||
|
|
|||
| 243 | */ |
||
| 244 | 36 | private static function readFile($filePath) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Parse a configuration file. |
||
| 254 | * |
||
| 255 | * @param string|null $configFile |
||
| 256 | */ |
||
| 257 | 50 | public function parse($configFile = null) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Parse a given configuration file and return an associative array representation. |
||
| 272 | * |
||
| 273 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
| 274 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
| 275 | * |
||
| 276 | * @param string|null $configFile The path to the configuration file. If null, the default configuration will be |
||
| 277 | * used |
||
| 278 | * |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | 50 | private function parseConfig($configFile = null) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Merge the default configuration with the parsed configuration. |
||
| 322 | */ |
||
| 323 | 14 | private function mergeDefaultConfiguration() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Warn about deprecated keywords in the configuration file. |
||
| 357 | */ |
||
| 358 | 14 | private function handleDeprecations() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Recursively resolve imports for a given array. |
||
| 371 | * |
||
| 372 | * This modifies the array in place. |
||
| 373 | * |
||
| 374 | * @param array $configuration |
||
| 375 | */ |
||
| 376 | private function handleImports(array &$configuration) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Resolve a single import definition. |
||
| 404 | * |
||
| 405 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
| 406 | * configuration |
||
| 407 | * @param string $parentConfLoc The path to the parent configuration |
||
| 408 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
||
| 409 | */ |
||
| 410 | private function handleImport($importDef, $parentConfLoc, array &$configuration) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Check whether a given file path is a valid import. |
||
| 457 | * |
||
| 458 | * @param string $filePath |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | private function isValidImport($filePath) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Check whether or not a filename has already been imported in a given process. |
||
| 497 | * |
||
| 498 | * @param string $filePath |
||
| 499 | */ |
||
| 500 | 36 | private function isRecursiveImport($filePath) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Merge the given array with existing configuration. |
||
| 514 | * |
||
| 515 | * @param array $importedConfig |
||
| 516 | * @param array $existingConfig |
||
| 517 | * |
||
| 518 | * @return array |
||
| 519 | */ |
||
| 520 | private function mergeImports(array $importedConfig, array $existingConfig) |
||
| 528 | |||
| 529 | 14 | private function handleDefaultOperations() |
|
| 538 | } |
||
| 539 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.