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 = []; |
||
| 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 = ['/^_(?!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) |
|
| 242 | { |
||
| 243 | 32 | if ($configFile === null) |
|
| 244 | 32 | { |
|
| 245 | 32 | return []; |
|
| 246 | } |
||
| 247 | |||
| 248 | 32 | $this->currentFile = $configFile; |
|
| 249 | |||
| 250 | try |
||
| 251 | { |
||
| 252 | 32 | $this->isRecursiveImport($configFile); |
|
| 253 | |||
| 254 | 32 | $parsedConfig = Yaml::parse($configFile->getContents()); |
|
| 255 | |||
| 256 | 32 | if ($parsedConfig === null) |
|
| 257 | 32 | { |
|
| 258 | $parsedConfig = []; |
||
| 259 | } |
||
| 260 | |||
| 261 | 32 | $this->handleImports($parsedConfig); |
|
| 262 | |||
| 263 | 32 | unset($parsedConfig[self::IMPORT_KEYWORD]); |
|
| 264 | |||
| 265 | 32 | return $parsedConfig; |
|
| 266 | } |
||
| 267 | 3 | catch (ParseException $e) |
|
| 268 | { |
||
| 269 | 1 | $this->logger->error('{file}: parsing failed... {message}', [ |
|
| 270 | 1 | 'message' => $e->getMessage(), |
|
| 271 | 1 | 'file' => $configFile, |
|
| 272 | 1 | ]); |
|
| 273 | 1 | $this->logger->error('Using default configuration...'); |
|
| 274 | 1 | } |
|
| 275 | 2 | catch (RecursiveConfigurationException $e) |
|
| 276 | { |
||
| 277 | 1 | $this->logger->error("{file}: you can't recursively import a file that's already been imported: {import}", [ |
|
| 278 | 1 | 'file' => $configFile, |
|
| 279 | 1 | 'import' => $e->getRecursiveImport(), |
|
| 280 | 1 | ]); |
|
| 281 | } |
||
| 282 | |||
| 283 | 2 | return []; |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Merge the default configuration with the parsed configuration. |
||
| 288 | */ |
||
| 289 | 32 | private function mergeDefaultConfiguration() |
|
| 290 | { |
||
| 291 | $defaultConfig = [ |
||
| 292 | 32 | 'baseurl' => '', |
|
| 293 | 32 | 'target' => '_site', |
|
| 294 | 'twig' => [ |
||
| 295 | 32 | 'autoescape' => false, |
|
| 296 | 32 | ], |
|
| 297 | 'include' => [ |
||
| 298 | 32 | '.htaccess', |
|
| 299 | 32 | ], |
|
| 300 | 'exclude' => [ |
||
| 301 | 32 | 'node_modules/', |
|
| 302 | 32 | 'stakx-theme.yml', |
|
| 303 | 32 | '/tmp___$/', |
|
| 304 | 32 | self::DEFAULT_NAME, |
|
| 305 | 32 | ], |
|
| 306 | 'templates' => [ |
||
| 307 | 32 | 'redirect' => false, |
|
| 308 | 32 | ], |
|
| 309 | 'highlighter' => [ |
||
| 310 | 32 | 'enabled' => true, |
|
| 311 | 32 | 'languages' => [], |
|
| 312 | 32 | ], |
|
| 313 | 'build' => [ |
||
| 314 | 32 | 'preserveCase' => false, |
|
| 315 | 32 | ], |
|
| 316 | 32 | ]; |
|
| 317 | |||
| 318 | 32 | $this->configuration = ArrayUtilities::array_merge_defaults($defaultConfig, $this->configuration, 'name'); |
|
| 319 | 32 | } |
|
| 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) |
|
| 337 | { |
||
| 338 | 32 | if (!isset($configuration[self::IMPORT_KEYWORD])) |
|
| 339 | 32 | { |
|
| 340 | 32 | $this->logger->debug('{file}: does not import any other files', [ |
|
| 341 | 32 | 'file' => $this->currentFile->getRelativeFilePath(), |
|
| 342 | 32 | ]); |
|
| 343 | |||
| 344 | 32 | return; |
|
| 345 | } |
||
| 346 | |||
| 347 | 17 | if (!is_array($imports = $configuration[self::IMPORT_KEYWORD])) |
|
| 348 | 17 | { |
|
| 349 | 3 | $this->logger->error('{file}: the reserved "import" keyword can only be an array'); |
|
| 350 | |||
| 351 | 3 | return; |
|
| 352 | } |
||
| 353 | |||
| 354 | 14 | foreach ($imports as $import) |
|
| 355 | { |
||
| 356 | 14 | $this->handleImport($import, $configuration); |
|
| 357 | 14 | } |
|
| 358 | 14 | } |
|
| 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) |
|
| 368 | { |
||
| 369 | 14 | if (!is_string($importDef)) |
|
| 370 | 14 | { |
|
| 371 | 3 | $this->logger->error('{file}: invalid import: {message}', [ |
|
| 372 | 3 | 'file' => $this->configFile->getRelativeFilePath(), |
|
| 373 | 3 | 'message' => $importDef, |
|
| 374 | 3 | ]); |
|
| 375 | |||
| 376 | 3 | return; |
|
| 377 | } |
||
| 378 | |||
| 379 | 11 | $import = $this->configFile->createFileForRelativePath($importDef); |
|
| 380 | |||
| 381 | 11 | if (!$this->isValidImport($import)) |
|
| 382 | 11 | { |
|
| 383 | 3 | return; |
|
| 384 | } |
||
| 385 | |||
| 386 | 8 | $this->logger->debug('{file}: imports additional file: {import}', [ |
|
| 387 | 8 | 'file' => $this->configFile->getRelativeFilePath(), |
|
| 388 | 8 | 'import' => $import->getRelativeFilePath(), |
|
| 389 | 8 | ]); |
|
| 390 | |||
| 391 | try |
||
| 392 | { |
||
| 393 | 8 | $importedConfig = $this->parseConfig($import); |
|
| 394 | 7 | $configuration = $this->mergeImports($importedConfig, $configuration); |
|
| 395 | } |
||
| 396 | 8 | catch (FileNotFoundException $e) |
|
| 397 | { |
||
| 398 | 1 | $this->logger->warning('{file}: could not find file to import: {import}', [ |
|
| 399 | 1 | 'file' => $this->configFile->getRelativeFilePath(), |
|
| 400 | 1 | 'import' => $import, |
|
| 401 | 1 | ]); |
|
| 402 | } |
||
| 403 | 8 | } |
|
| 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) |
|
| 413 | { |
||
| 414 | 11 | $errorMsg = ''; |
|
| 415 | |||
| 416 | 11 | if ($filePath->isDir()) |
|
| 417 | 11 | { |
|
| 418 | 1 | $errorMsg = 'a directory'; |
|
| 419 | 1 | } |
|
| 420 | 10 | elseif ($filePath->isLink()) |
|
| 421 | { |
||
| 422 | $errorMsg = 'a symbolically linked file'; |
||
| 423 | } |
||
| 424 | 10 | elseif ($this->currentFile->getAbsolutePath() == $filePath->getAbsolutePath()) |
|
| 425 | { |
||
| 426 | 1 | $errorMsg = 'yourself'; |
|
| 427 | 1 | } |
|
| 428 | 9 | elseif (($ext = $filePath->getExtension()) != 'yml' && $ext != 'yaml') |
|
| 429 | { |
||
| 430 | 1 | $errorMsg = 'a non-YAML configuration'; |
|
| 431 | 1 | } |
|
| 432 | |||
| 433 | 11 | if (!($noErrors = empty($errorMsg))) |
|
| 434 | 11 | { |
|
| 435 | 3 | $this->logger->error("{file}: you can't import {message}: {import}", [ |
|
| 436 | 3 | 'file' => $this->configFile->getRelativeFilePath(), |
|
| 437 | 3 | 'message' => $errorMsg, |
|
| 438 | 3 | 'import' => $filePath, |
|
| 439 | 3 | ]); |
|
| 440 | 3 | } |
|
| 441 | |||
| 442 | 11 | return $noErrors; |
|
| 443 | } |
||
| 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) |
|
| 451 | { |
||
| 452 | 32 | if (in_array($filePath->getRelativeFilePath(), self::$configImports)) |
|
| 453 | 32 | { |
|
| 454 | 1 | throw new RecursiveConfigurationException($filePath, sprintf( |
|
| 455 | 1 | 'The %s file has already been imported', $filePath->getRelativeFilePath() |
|
| 456 | 1 | )); |
|
| 457 | } |
||
| 458 | |||
| 459 | 32 | self::$configImports[] = $filePath->getRelativeFilePath(); |
|
| 460 | 32 | } |
|
| 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() |
|
| 480 | { |
||
| 481 | 32 | if (substr($this->getTargetFolder(), 0, 1) != '_') |
|
| 482 | 32 | { |
|
| 483 | $this->configuration['exclude'][] = $this->getTargetFolder(); |
||
| 484 | } |
||
| 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: