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 bool |
||
| 155 | */ |
||
| 156 | public function isHighlighterUsingLineNumbers() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 1 | public function getTheme() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 7 | public function getConfiguration() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return string[] |
||
| 179 | */ |
||
| 180 | 1 | public function getPageViewFolders() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 32 | public function getTargetFolder() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return string[][] |
||
| 198 | */ |
||
| 199 | 1 | public function getCollectionsFolders() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | 1 | public function getTwigAutoescape() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @return false|string |
||
| 214 | */ |
||
| 215 | public function getRedirectTemplate() |
||
| 219 | |||
| 220 | /// |
||
| 221 | // Parsing |
||
| 222 | /// |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Parse a configuration file. |
||
| 226 | * |
||
| 227 | * @param File|null $configFile |
||
| 228 | */ |
||
| 229 | 32 | public function parse(File $configFile = null) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Parse a given configuration file and return an associative array representation. |
||
| 247 | * |
||
| 248 | * This function will automatically take care of imports in each file, whether it be a child or grandchild config |
||
| 249 | * file. `$configFile` should be called with 'null' when "configuration-less" mode is used. |
||
| 250 | * |
||
| 251 | * @param File|null $configFile The path to the configuration file. If null, the default configuration will be |
||
| 252 | * used |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | 32 | private function parseConfig(File $configFile = null) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Merge the default configuration with the parsed configuration. |
||
| 303 | */ |
||
| 304 | 32 | private function mergeDefaultConfiguration() |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Warn about deprecated keywords in the configuration file. |
||
| 339 | */ |
||
| 340 | 32 | private function handleDeprecations() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Recursively resolve imports for a given array. |
||
| 347 | * |
||
| 348 | * This modifies the array in place. |
||
| 349 | * |
||
| 350 | * @param array $configuration |
||
| 351 | */ |
||
| 352 | 32 | private function handleImports(array &$configuration) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Resolve a single import definition. |
||
| 378 | * |
||
| 379 | * @param string $importDef The path for a given import; this will be treated as a relative path to the parent |
||
| 380 | * configuration |
||
| 381 | * @param array $configuration The array representation of the current configuration; this will be modified in place |
||
| 382 | */ |
||
| 383 | 14 | private function handleImport($importDef, array &$configuration) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Check whether a given file path is a valid import. |
||
| 423 | * |
||
| 424 | * @param File $filePath |
||
| 425 | * |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | 11 | private function isValidImport(File $filePath) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Check whether or not a filename has already been imported in a given process. |
||
| 463 | * |
||
| 464 | * @param File $filePath |
||
| 465 | */ |
||
| 466 | 32 | private function isRecursiveImport(File $filePath) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Merge the given array with existing configuration. |
||
| 480 | * |
||
| 481 | * @param array $importedConfig |
||
| 482 | * @param array $existingConfig |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | 7 | private function mergeImports(array $importedConfig, array $existingConfig) |
|
| 494 | |||
| 495 | 32 | private function handleDefaultOperations() |
|
| 507 | } |
||
| 508 |
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: