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 |
||
| 21 | class Configuration |
||
| 22 | { |
||
| 23 | const DEFAULT_NAME = '_config.yml'; |
||
| 24 | const IMPORT_KEYWORD = 'import'; |
||
| 25 | const CACHE_FOLDER = '.stakx-cache'; |
||
| 26 | |||
| 27 | private static $configImports = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A list of regular expressions or files directly related to stakx websites that should not be copied over to the |
||
| 31 | * compiled website as an asset. |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | public static $stakxSourceFiles = ['/^_(?!themes).*/', '/.twig$/']; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * An array representation of the main Yaml configuration. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $configuration = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The master configuration file for the current build. |
||
| 46 | * |
||
| 47 | * This is the file that will be handling imports, if any. |
||
| 48 | * |
||
| 49 | * @var File |
||
| 50 | */ |
||
| 51 | private $configFile; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The current configuration file being processed. |
||
| 55 | * |
||
| 56 | * If there are no imports used, this value will equal $this->configFile. Otherwise, this file will equal to the |
||
| 57 | * current imported configuration file that is being evaluated. |
||
| 58 | * |
||
| 59 | * @var File |
||
| 60 | */ |
||
| 61 | private $currentFile; |
||
| 62 | |||
| 63 | private $eventDispatcher; |
||
| 64 | private $logger; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Configuration constructor. |
||
| 68 | */ |
||
| 69 | 32 | public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
|
| 74 | |||
| 75 | /// |
||
| 76 | // Getters |
||
| 77 | /// |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | 1 | public function isDebug() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return string|null |
||
| 89 | */ |
||
| 90 | 2 | public function getBaseUrl() |
|
| 94 | |||
| 95 | public function hasDataItems() |
||
| 99 | |||
| 100 | public function hasCollections() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return string[] |
||
| 107 | */ |
||
| 108 | 1 | public function getDataFolders() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return string[] |
||
| 115 | */ |
||
| 116 | 1 | public function getDataSets() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return string[] |
||
| 123 | */ |
||
| 124 | 1 | public function getIncludes() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @return string[] |
||
| 131 | */ |
||
| 132 | 1 | public function getExcludes() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function getHighlighterCustomLanguages() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function isHighlighterEnabled() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 1 | public function getTheme() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 7 | public function getConfiguration() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @return string[] |
||
| 171 | */ |
||
| 172 | 1 | public function getPageViewFolders() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 32 | public function getTargetFolder() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return string[][] |
||
| 190 | */ |
||
| 191 | 1 | public function getCollectionsFolders() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | 1 | public function getTwigAutoescape() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @return false|string |
||
| 206 | */ |
||
| 207 | public function getRedirectTemplate() |
||
| 211 | |||
| 212 | /// |
||
| 213 | // Parsing |
||
| 214 | /// |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Parse a configuration file. |
||
| 218 | * |
||
| 219 | * @param File|null $configFile |
||
| 220 | */ |
||
| 221 | 32 | public function parse(File $configFile = null) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Parse a given configuration file and return an associative array representation. |
||
| 239 | * |
||
| 240 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
| 241 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
| 242 | * |
||
| 243 | * @param File|null $configFile The path to the configuration file. If null, the default configuration will be |
||
| 244 | * used |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | 32 | private function parseConfig(File $configFile = null) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Merge the default configuration with the parsed configuration. |
||
| 295 | */ |
||
| 296 | 32 | private function mergeDefaultConfiguration() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Warn about deprecated keywords in the configuration file. |
||
| 330 | */ |
||
| 331 | 32 | private function handleDeprecations() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Recursively resolve imports for a given array. |
||
| 338 | * |
||
| 339 | * This modifies the array in place. |
||
| 340 | * |
||
| 341 | * @param array $configuration |
||
| 342 | */ |
||
| 343 | 32 | private function handleImports(array &$configuration) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Resolve a single import definition. |
||
| 369 | * |
||
| 370 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
| 371 | * configuration |
||
| 372 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
||
| 373 | */ |
||
| 374 | 14 | private function handleImport($importDef, array &$configuration) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Check whether a given file path is a valid import. |
||
| 414 | * |
||
| 415 | * @param File $filePath |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | 11 | private function isValidImport(File $filePath) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Check whether or not a filename has already been imported in a given process. |
||
| 454 | * |
||
| 455 | * @param File $filePath |
||
| 456 | */ |
||
| 457 | 32 | private function isRecursiveImport(File $filePath) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Merge the given array with existing configuration. |
||
| 471 | * |
||
| 472 | * @param array $importedConfig |
||
| 473 | * @param array $existingConfig |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
| 485 | |||
| 486 | 32 | private function handleDefaultOperations() |
|
| 498 | } |
||
| 499 |
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: