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