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 | use LoggerAwareTrait; |
||
| 23 | |||
| 24 | const HIGHLIGHTER_ENABLED = 'highlighter-enabled'; |
||
| 25 | |||
| 26 | const DEFAULT_NAME = '_config.yml'; |
||
| 27 | const IMPORT_KEYWORD = 'import'; |
||
| 28 | const CACHE_FOLDER = '.stakx-cache'; |
||
| 29 | |||
| 30 | private static $configImports = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
||
| 34 | * compiled website as an asset. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | public static $stakxSourceFiles = array('/^_(?!themes).*/', '/.twig$/'); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * An array representation of the main Yaml configuration. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $configuration; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The master configuration file for the current build. |
||
| 49 | * |
||
| 50 | * This is the file that will be handling imports, if any. |
||
| 51 | * |
||
| 52 | * @var File |
||
| 53 | */ |
||
| 54 | private $configFile; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The current configuration file being processed. |
||
| 58 | * |
||
| 59 | * If there are no imports used, this value will equal $this->configFile. Otherwise, this file will equal to the |
||
| 60 | * current imported configuration file that is being evaluated. |
||
| 61 | * |
||
| 62 | * @var File |
||
| 63 | */ |
||
| 64 | private $currentFile; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Configuration constructor. |
||
| 68 | */ |
||
| 69 | 73 | public function __construct() |
|
| 73 | |||
| 74 | /// |
||
| 75 | // Getters |
||
| 76 | /// |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | 1 | public function isDebug() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return string|null |
||
| 88 | */ |
||
| 89 | 2 | public function getBaseUrl() |
|
| 93 | |||
| 94 | public function hasDataItems() |
||
| 98 | |||
| 99 | public function hasCollections() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string[] |
||
| 106 | */ |
||
| 107 | 1 | public function getDataFolders() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return string[] |
||
| 114 | */ |
||
| 115 | 1 | public function getDataSets() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return string[] |
||
| 122 | */ |
||
| 123 | 1 | public function getIncludes() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @return string[] |
||
| 130 | */ |
||
| 131 | 1 | public function getExcludes() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function getHighlighterCustomLanguages() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function isHighlighterEnabled() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 1 | public function getTheme() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | 7 | public function getConfiguration() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return string[] |
||
| 170 | */ |
||
| 171 | 1 | public function getPageViewFolders() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 32 | public function getTargetFolder() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @return string[][] |
||
| 186 | */ |
||
| 187 | 1 | public function getCollectionsFolders() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | 1 | public function getTwigAutoescape() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @return false|string |
||
| 202 | */ |
||
| 203 | public function getRedirectTemplate() |
||
| 207 | |||
| 208 | /// |
||
| 209 | // Parsing |
||
| 210 | /// |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Parse a configuration file. |
||
| 214 | * |
||
| 215 | * @param File|null $configFile |
||
| 216 | */ |
||
| 217 | 32 | public function parse(File $configFile = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Parse a given configuration file and return an associative array representation. |
||
| 232 | * |
||
| 233 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
| 234 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
| 235 | * |
||
| 236 | * @param File|null $configFile The path to the configuration file. If null, the default configuration will be |
||
| 237 | * used |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | 32 | private function parseConfig(File $configFile = null) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Merge the default configuration with the parsed configuration. |
||
| 288 | */ |
||
| 289 | 32 | private function mergeDefaultConfiguration() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Warn about deprecated keywords in the configuration file. |
||
| 323 | */ |
||
| 324 | 32 | private function handleDeprecations() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Recursively resolve imports for a given array. |
||
| 331 | * |
||
| 332 | * This modifies the array in place. |
||
| 333 | * |
||
| 334 | * @param array $configuration |
||
| 335 | */ |
||
| 336 | 32 | private function handleImports(array &$configuration) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Resolve a single import definition. |
||
| 362 | * |
||
| 363 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
| 364 | * configuration |
||
| 365 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
||
| 366 | */ |
||
| 367 | 14 | private function handleImport($importDef, array &$configuration) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Check whether a given file path is a valid import. |
||
| 407 | * |
||
| 408 | * @param File $filePath |
||
| 409 | * |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | 11 | private function isValidImport(File $filePath) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Check whether or not a filename has already been imported in a given process. |
||
| 447 | * |
||
| 448 | * @param File $filePath |
||
| 449 | */ |
||
| 450 | 32 | private function isRecursiveImport(File $filePath) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Merge the given array with existing configuration. |
||
| 464 | * |
||
| 465 | * @param array $importedConfig |
||
| 466 | * @param array $existingConfig |
||
| 467 | * |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
| 478 | |||
| 479 | 32 | private function handleDefaultOperations() |
|
| 488 | } |
||
| 489 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: