Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SiteAliasFileLoader 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 SiteAliasFileLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class SiteAliasFileLoader |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var SiteAliasFileDiscovery |
||
| 17 | */ |
||
| 18 | protected $discovery; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $referenceData; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $loader; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $root; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * SiteAliasFileLoader constructor |
||
| 37 | * |
||
| 38 | * @param SiteAliasFileDiscovery|null $discovery |
||
| 39 | */ |
||
| 40 | public function __construct($discovery = null) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Allow configuration data to be used in replacements in the alias file. |
||
| 49 | */ |
||
| 50 | public function setReferenceData($data) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Allow 'self.site.yml' to be applied to any alias record found. |
||
| 57 | */ |
||
| 58 | public function setRoot($root) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Add a search location to our discovery object. |
||
| 65 | * |
||
| 66 | * @param string $path |
||
| 67 | * |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | public function addSearchLocation($path) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return our discovery object. |
||
| 78 | * |
||
| 79 | * @return SiteAliasFileDiscovery |
||
| 80 | */ |
||
| 81 | public function discovery() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Load the file containing the specified alias name. |
||
| 88 | * |
||
| 89 | * @param SiteAliasName $aliasName |
||
| 90 | * |
||
| 91 | * @return SiteAlias|false |
||
| 92 | */ |
||
| 93 | public function load(SiteAliasName $aliasName) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Given only a site name, load the default environment from it. |
||
| 120 | */ |
||
| 121 | protected function loadDefaultEnvFromSitename($sitename) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Return a list of all site aliases loadable from any findable path. |
||
| 140 | * |
||
| 141 | * @return SiteAlias[] |
||
| 142 | */ |
||
| 143 | public function loadAll() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return a list of all available alias files. Does not include |
||
| 161 | * legacy files. |
||
| 162 | * |
||
| 163 | * @param string $location Only consider alias files in the specified location. |
||
| 164 | * @return string[] |
||
| 165 | */ |
||
| 166 | public function listAll($location = '') |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Given an alias name that might represent multiple sites, |
||
| 173 | * return a list of all matching alias records. If nothing was found, |
||
| 174 | * or the name represents a single site + env, then we take |
||
| 175 | * no action and return `false`. |
||
| 176 | * |
||
| 177 | * @param string $sitename The site name to return all environments for. |
||
| 178 | * @return SiteAlias[]|false |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function loadMultiple($sitename, $location = null) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Given a location, return all alias files located there. |
||
| 198 | * |
||
| 199 | * @param string $location The location to filter. |
||
| 200 | * @return SiteAlias[] |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function loadLocation($location) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param array $siteData list of sites with its respective data |
||
| 221 | * |
||
| 222 | * @param SiteAliasName $aliasName The name of the record being created |
||
| 223 | * @param $siteData An associative array of envrionment => site data |
||
| 224 | * @return SiteAlias[] |
||
| 225 | */ |
||
| 226 | protected function createSiteAliassFromSiteData($sitename, $siteData, $location = '') |
||
| 243 | |||
| 244 | /** |
||
| 245 | * isValidEnvName determines if a given entry should be skipped or not |
||
| 246 | * (e.g. the "common" entry). |
||
| 247 | * |
||
| 248 | * @param string $envName The environment name to test |
||
| 249 | */ |
||
| 250 | protected function isValidEnvName($envName) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Store an alias record in a list. If the alias record has |
||
| 257 | * a known name, then the key of the list will be the record's name. |
||
| 258 | * Otherwise, append the record to the end of the list with |
||
| 259 | * a numeric index. |
||
| 260 | * |
||
| 261 | * @param &SiteAlias[] $result list of alias records |
||
| 262 | * @param SiteAlias $aliasRecord one more alias to store in the result |
||
| 263 | */ |
||
| 264 | protected function storeSiteAliasInResut(&$result, SiteAlias $aliasRecord) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * If the alias name is '@sitename', or if it is '@sitename.env', then |
||
| 279 | * look for a sitename.site.yml file that contains it. We also handle |
||
| 280 | * '@location.sitename.env' here as well. |
||
| 281 | * |
||
| 282 | * @param SiteAliasName $aliasName |
||
| 283 | * |
||
| 284 | * @return SiteAlias|false |
||
| 285 | */ |
||
| 286 | protected function loadSingleAliasFile(SiteAliasName $aliasName) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Given only the path to an alias file `site.alias.yml`, return all |
||
| 301 | * of the alias records for every environment stored in that file. |
||
| 302 | * |
||
| 303 | * @param string $path |
||
| 304 | * @return SiteAlias[] |
||
| 305 | */ |
||
| 306 | protected function loadSingleSiteAliasFileAtPath($path) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Given the path to a single site alias file `site.alias.yml`, |
||
| 318 | * return the `site` part. |
||
| 319 | * |
||
| 320 | * @param string $path |
||
| 321 | */ |
||
| 322 | protected function siteNameFromPath($path) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Chop off the `aliases.yml` or `alias.yml` part of a path. This works |
||
| 333 | * just like `basename`, except it will throw if the provided path |
||
| 334 | * does not end in the specified extension. |
||
| 335 | * |
||
| 336 | * @param string $path |
||
| 337 | * @param string $extension |
||
| 338 | * @return string |
||
| 339 | * @throws \Exception |
||
| 340 | */ |
||
| 341 | protected function basenameWithoutExtension($path, $extension) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Given an alias name and a path, load the data from the path |
||
| 353 | * and process it as needed to generate the alias record. |
||
| 354 | * |
||
| 355 | * @param SiteAliasName $aliasName |
||
| 356 | * @param string $path |
||
| 357 | * @return SiteAlias|false |
||
| 358 | */ |
||
| 359 | protected function loadSingleAliasFileWithNameAtPath(SiteAliasName $aliasName, $path) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Load the yml from the given path |
||
| 371 | * |
||
| 372 | * @param string $path |
||
| 373 | * @return array|bool |
||
| 374 | */ |
||
| 375 | protected function loadSiteDataFromPath($path) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Given an array of site aliases, find the first one that is |
||
| 388 | * local (has no 'host' item) and also contains a 'self.site.yml' file. |
||
| 389 | * @param array $data |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | protected function findSelfSiteAliases($site_aliases, $path) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Check to see if there is a 'drush/sites/self.site.yml' file at |
||
| 408 | * the provided root, or one directory up from there. |
||
| 409 | */ |
||
| 410 | protected function loadSelfSiteData($root) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Load the contents of the specified file. |
||
| 426 | * |
||
| 427 | * @param string $path Path to file to load |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | protected function loadData($path) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @return DataFileLoaderInterface |
||
| 444 | */ |
||
| 445 | public function getLoader($extension) |
||
| 452 | |||
| 453 | public function addLoader($extension, DataFileLoaderInterface $loader) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Given an array containing site alias data, return an alias record |
||
| 460 | * containing the data for the requested record. If there is a 'common' |
||
| 461 | * section, then merge that in as well. |
||
| 462 | * |
||
| 463 | * @param SiteAliasName $aliasName the alias we are loading |
||
| 464 | * @param array $data |
||
| 465 | * |
||
| 466 | * @return SiteAlias|false |
||
| 467 | */ |
||
| 468 | protected function fetchSiteAliasFromSiteAliasData(SiteAliasName $aliasName, ConfigProcessor $processor, array $data) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * getRequestedEnvData fetches the data for the specified environment |
||
| 491 | * from the provided site record data. |
||
| 492 | * |
||
| 493 | * @param array $data The site alias data |
||
| 494 | * @param string $env The name of the environment desired |
||
| 495 | * @return array|false |
||
| 496 | */ |
||
| 497 | protected function getRequestedEnvData(array $data, $env) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Determine whether there is a valid-looking environment '$env' in the |
||
| 514 | * provided site alias data. |
||
| 515 | * |
||
| 516 | * @param array $data |
||
| 517 | * @param string $env |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | protected function siteEnvExists(array $data, $env) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Adjust the alias data for a single-site alias. Usually, a .yml alias |
||
| 531 | * file will contain multiple entries, one for each of the environments |
||
| 532 | * of an alias. If there are no environments |
||
| 533 | * |
||
| 534 | * @param array $data |
||
| 535 | * @return array |
||
| 536 | */ |
||
| 537 | protected function adjustIfSingleAlias($data) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * A single-environment alias looks something like this: |
||
| 552 | * |
||
| 553 | * --- |
||
| 554 | * root: /path/to/drupal |
||
| 555 | * uri: https://mysite.org |
||
| 556 | * |
||
| 557 | * A multiple-environment alias looks something like this: |
||
| 558 | * |
||
| 559 | * --- |
||
| 560 | * default: dev |
||
| 561 | * dev: |
||
| 562 | * root: /path/to/dev |
||
| 563 | * uri: https://dev.mysite.org |
||
| 564 | * stage: |
||
| 565 | * root: /path/to/stage |
||
| 566 | * uri: https://stage.mysite.org |
||
| 567 | * |
||
| 568 | * The differentiator between these two is that the multi-environment |
||
| 569 | * alias always has top-level elements that are associative arrays, and |
||
| 570 | * the single-environment alias never does. |
||
| 571 | * |
||
| 572 | * @param array $data |
||
| 573 | * @return bool |
||
| 574 | */ |
||
| 575 | protected function detectSingleAlias($data) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Return the name of the environment requested. |
||
| 587 | * |
||
| 588 | * @param SiteAliasName $aliasName the alias we are loading |
||
| 589 | * @param array $data |
||
| 590 | * |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | protected function getEnvironmentName(SiteAliasName $aliasName, array $data) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Given a data array containing site alias environments, determine which |
||
| 605 | * envirionmnet should be used as the default environment. |
||
| 606 | * |
||
| 607 | * @param array $data |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | protected function getDefaultEnvironmentName(array $data) |
||
| 626 | } |
||
| 627 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: