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 | * SiteAliasFileLoader constructor |
||
| 32 | * |
||
| 33 | * @param SiteAliasFileDiscovery|null $discovery |
||
| 34 | */ |
||
| 35 | public function __construct($discovery = null) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Allow configuration data to be used in replacements in the alias file. |
||
| 44 | */ |
||
| 45 | public function setReferenceData($data) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Add a search location to our discovery object. |
||
| 52 | * |
||
| 53 | * @param string $path |
||
| 54 | * |
||
| 55 | * @return $this |
||
| 56 | */ |
||
| 57 | public function addSearchLocation($path) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Return our discovery object. |
||
| 65 | * |
||
| 66 | * @return SiteAliasFileDiscovery |
||
| 67 | */ |
||
| 68 | public function discovery() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Load the file containing the specified alias name. |
||
| 75 | * |
||
| 76 | * @param SiteAliasName $aliasName |
||
| 77 | * |
||
| 78 | * @return SiteAlias|false |
||
| 79 | */ |
||
| 80 | public function load(SiteAliasName $aliasName) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Given only a site name, load the default environment from it. |
||
| 107 | */ |
||
| 108 | protected function loadDefaultEnvFromSitename($sitename) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return a list of all site aliases loadable from any findable path. |
||
| 127 | * |
||
| 128 | * @return SiteAlias[] |
||
| 129 | */ |
||
| 130 | public function loadAll() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return a list of all available alias files. Does not include |
||
| 148 | * legacy files. |
||
| 149 | * |
||
| 150 | * @param string $location Only consider alias files in the specified location. |
||
| 151 | * @return string[] |
||
| 152 | */ |
||
| 153 | public function listAll($location = '') |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Given an alias name that might represent multiple sites, |
||
| 160 | * return a list of all matching alias records. If nothing was found, |
||
| 161 | * or the name represents a single site + env, then we take |
||
| 162 | * no action and return `false`. |
||
| 163 | * |
||
| 164 | * @param string $sitename The site name to return all environments for. |
||
| 165 | * @return SiteAlias[]|false |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function loadMultiple($sitename, $location = null) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Given a location, return all alias files located there. |
||
| 185 | * |
||
| 186 | * @param string $location The location to filter. |
||
| 187 | * @return SiteAlias[] |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function loadLocation($location) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param array $siteData list of sites with its respective data |
||
| 208 | * |
||
| 209 | * @param SiteAliasName $aliasName The name of the record being created |
||
| 210 | * @param $siteData An associative array of envrionment => site data |
||
| 211 | * @return SiteAlias[] |
||
| 212 | */ |
||
| 213 | protected function createSiteAliassFromSiteData($sitename, $siteData, $location = '') |
||
| 214 | { |
||
| 215 | $result = []; |
||
| 216 | if (!is_array($siteData) || empty($siteData)) { |
||
| 217 | return $result; |
||
| 218 | } |
||
| 219 | foreach ($siteData as $envName => $data) { |
||
| 220 | if (is_array($data) && $this->isValidEnvName($envName)) { |
||
| 221 | $aliasName = new SiteAliasName($sitename, $envName, $location); |
||
| 222 | |||
| 223 | $processor = new ConfigProcessor(); |
||
| 224 | $oneRecord = $this->fetchSiteAliasFromSiteAliasData($aliasName, $processor, $siteData); |
||
| 225 | $this->storeSiteAliasInResut($result, $oneRecord); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | return $result; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * isValidEnvName determines if a given entry should be skipped or not |
||
| 233 | * (e.g. the "common" entry). |
||
| 234 | * |
||
| 235 | * @param string $envName The environment name to test |
||
| 236 | */ |
||
| 237 | protected function isValidEnvName($envName) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Store an alias record in a list. If the alias record has |
||
| 244 | * a known name, then the key of the list will be the record's name. |
||
| 245 | * Otherwise, append the record to the end of the list with |
||
| 246 | * a numeric index. |
||
| 247 | * |
||
| 248 | * @param &SiteAlias[] $result list of alias records |
||
| 249 | * @param SiteAlias $aliasRecord one more alias to store in the result |
||
| 250 | */ |
||
| 251 | protected function storeSiteAliasInResut(&$result, SiteAlias $aliasRecord) |
||
| 252 | { |
||
| 253 | if (!$aliasRecord) { |
||
| 254 | return; |
||
| 255 | } |
||
| 256 | $key = $aliasRecord->name(); |
||
| 257 | if (empty($key)) { |
||
| 258 | $result[] = $aliasRecord; |
||
| 259 | return; |
||
| 260 | } |
||
| 261 | $result[$key] = $aliasRecord; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * If the alias name is '@sitename', or if it is '@sitename.env', then |
||
| 266 | * look for a sitename.site.yml file that contains it. We also handle |
||
| 267 | * '@location.sitename.env' here as well. |
||
| 268 | * |
||
| 269 | * @param SiteAliasName $aliasName |
||
| 270 | * |
||
| 271 | * @return SiteAlias|false |
||
| 272 | */ |
||
| 273 | protected function loadSingleAliasFile(SiteAliasName $aliasName) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Given only the path to an alias file `site.alias.yml`, return all |
||
| 288 | * of the alias records for every environment stored in that file. |
||
| 289 | * |
||
| 290 | * @param string $path |
||
| 291 | * @return SiteAlias[] |
||
| 292 | */ |
||
| 293 | protected function loadSingleSiteAliasFileAtPath($path) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Given the path to a single site alias file `site.alias.yml`, |
||
| 305 | * return the `site` part. |
||
| 306 | * |
||
| 307 | * @param string $path |
||
| 308 | */ |
||
| 309 | protected function siteNameFromPath($path) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Chop off the `aliases.yml` or `alias.yml` part of a path. This works |
||
| 320 | * just like `basename`, except it will throw if the provided path |
||
| 321 | * does not end in the specified extension. |
||
| 322 | * |
||
| 323 | * @param string $path |
||
| 324 | * @param string $extension |
||
| 325 | * @return string |
||
| 326 | * @throws \Exception |
||
| 327 | */ |
||
| 328 | protected function basenameWithoutExtension($path, $extension) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Given an alias name and a path, load the data from the path |
||
| 340 | * and process it as needed to generate the alias record. |
||
| 341 | * |
||
| 342 | * @param SiteAliasName $aliasName |
||
| 343 | * @param string $path |
||
| 344 | * @return SiteAlias|false |
||
| 345 | */ |
||
| 346 | protected function loadSingleAliasFileWithNameAtPath(SiteAliasName $aliasName, $path) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Load the yml from the given path |
||
| 358 | * |
||
| 359 | * @param string $path |
||
| 360 | * @return array|bool |
||
| 361 | */ |
||
| 362 | protected function loadSiteDataFromPath($path) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Given an array of site aliases, find the first one that is |
||
| 375 | * local (has no 'host' item) and also contains a 'self.site.yml' file. |
||
| 376 | * @param array $data |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | protected function findSelfSiteAliases($site_aliases) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Load the contents of the specified file. |
||
| 396 | * |
||
| 397 | * @param string $path Path to file to load |
||
| 398 | * @return array |
||
| 399 | */ |
||
| 400 | protected function loadData($path) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return DataFileLoaderInterface |
||
| 414 | */ |
||
| 415 | public function getLoader($extension) |
||
| 422 | |||
| 423 | public function addLoader($extension, DataFileLoaderInterface $loader) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Given an array containing site alias data, return an alias record |
||
| 430 | * containing the data for the requested record. If there is a 'common' |
||
| 431 | * section, then merge that in as well. |
||
| 432 | * |
||
| 433 | * @param SiteAliasName $aliasName the alias we are loading |
||
| 434 | * @param array $data |
||
| 435 | * |
||
| 436 | * @return SiteAlias|false |
||
| 437 | */ |
||
| 438 | protected function fetchSiteAliasFromSiteAliasData(SiteAliasName $aliasName, ConfigProcessor $processor, array $data) |
||
| 439 | { |
||
| 440 | $data = $this->adjustIfSingleAlias($data); |
||
| 441 | $env = $this->getEnvironmentName($aliasName, $data); |
||
| 442 | $env_data = $this->getRequestedEnvData($data, $env); |
||
| 443 | if (!$env_data) { |
||
| 444 | return false; |
||
| 445 | } |
||
| 446 | |||
| 447 | // Add the 'common' section if it exists. |
||
| 448 | if ($this->siteEnvExists($data, 'common')) { |
||
| 449 | $processor->add($data['common']); |
||
| 450 | } |
||
| 451 | |||
| 452 | // Then add the data from the desired environment. |
||
| 453 | $processor->add($env_data); |
||
| 454 | |||
| 455 | // Export the combined data and create an SiteAlias object to manage it. |
||
| 456 | return new SiteAlias($processor->export($this->referenceData + ['env-name' => $env]), '@' . $aliasName->sitenameWithLocation(), $env); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * getRequestedEnvData fetches the data for the specified environment |
||
| 461 | * from the provided site record data. |
||
| 462 | * |
||
| 463 | * @param array $data The site alias data |
||
| 464 | * @param string $env The name of the environment desired |
||
| 465 | * @return array|false |
||
| 466 | */ |
||
| 467 | protected function getRequestedEnvData(array $data, $env) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Determine whether there is a valid-looking environment '$env' in the |
||
| 484 | * provided site alias data. |
||
| 485 | * |
||
| 486 | * @param array $data |
||
| 487 | * @param string $env |
||
| 488 | * @return bool |
||
| 489 | */ |
||
| 490 | protected function siteEnvExists(array $data, $env) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Adjust the alias data for a single-site alias. Usually, a .yml alias |
||
| 501 | * file will contain multiple entries, one for each of the environments |
||
| 502 | * of an alias. If there are no environments |
||
| 503 | * |
||
| 504 | * @param array $data |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | protected function adjustIfSingleAlias($data) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * A single-environment alias looks something like this: |
||
| 522 | * |
||
| 523 | * --- |
||
| 524 | * root: /path/to/drupal |
||
| 525 | * uri: https://mysite.org |
||
| 526 | * |
||
| 527 | * A multiple-environment alias looks something like this: |
||
| 528 | * |
||
| 529 | * --- |
||
| 530 | * default: dev |
||
| 531 | * dev: |
||
| 532 | * root: /path/to/dev |
||
| 533 | * uri: https://dev.mysite.org |
||
| 534 | * stage: |
||
| 535 | * root: /path/to/stage |
||
| 536 | * uri: https://stage.mysite.org |
||
| 537 | * |
||
| 538 | * The differentiator between these two is that the multi-environment |
||
| 539 | * alias always has top-level elements that are associative arrays, and |
||
| 540 | * the single-environment alias never does. |
||
| 541 | * |
||
| 542 | * @param array $data |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | protected function detectSingleAlias($data) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Return the name of the environment requested. |
||
| 557 | * |
||
| 558 | * @param SiteAliasName $aliasName the alias we are loading |
||
| 559 | * @param array $data |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | protected function getEnvironmentName(SiteAliasName $aliasName, array $data) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Given a data array containing site alias environments, determine which |
||
| 575 | * envirionmnet should be used as the default environment. |
||
| 576 | * |
||
| 577 | * @param array $data |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | protected function getDefaultEnvironmentName(array $data) |
||
| 596 | } |
||
| 597 |
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: