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 AliasRecord|false |
||
79 | */ |
||
80 | public function load(SiteAliasName $aliasName) |
||
100 | |||
101 | /** |
||
102 | * Given only a site name, load the default environment from it. |
||
103 | */ |
||
104 | protected function loadDefaultEnvFromSitename($sitename) |
||
120 | |||
121 | /** |
||
122 | * Return a list of all site aliases loadable from any findable path. |
||
123 | * |
||
124 | * @return AliasRecord[] |
||
125 | */ |
||
126 | public function loadAll() |
||
141 | |||
142 | /** |
||
143 | * Return a list of all available alias files. Does not include |
||
144 | * legacy files. |
||
145 | * |
||
146 | * @return string[] |
||
147 | */ |
||
148 | public function listAll() |
||
152 | |||
153 | /** |
||
154 | * Given an alias name that might represent multiple sites, |
||
155 | * return a list of all matching alias records. If nothing was found, |
||
156 | * or the name represents a single site + env, then we take |
||
157 | * no action and return `false`. |
||
158 | * |
||
159 | * @param string $sitename The site name to return all environments for. |
||
160 | * @return AliasRecord[]|false |
||
161 | */ |
||
162 | public function loadMultiple($sitename) |
||
172 | |||
173 | /** |
||
174 | * @param array $siteData list of sites with its respective data |
||
175 | * |
||
176 | * @param SiteAliasName $aliasName The name of the record being created |
||
177 | * @param $siteData An associative array of envrionment => site data |
||
178 | * @return AliasRecord[] |
||
179 | */ |
||
180 | protected function createAliasRecordsFromSiteData($sitename, $siteData) |
||
194 | |||
195 | /** |
||
196 | * Store an alias record in a list. If the alias record has |
||
197 | * a known name, then the key of the list will be the record's name. |
||
198 | * Otherwise, append the record to the end of the list with |
||
199 | * a numeric index. |
||
200 | * |
||
201 | * @param &AliasRecord[] $result list of alias records |
||
202 | * @param AliasRecord $aliasRecord one more alias to store in the result |
||
203 | */ |
||
204 | protected function storeAliasRecordInResut(&$result, AliasRecord $aliasRecord) |
||
216 | |||
217 | /** |
||
218 | * If the alias name is '@sitename', or if it is '@sitename.env', then |
||
219 | * look for a sitename.site.yml file that contains it. |
||
220 | * |
||
221 | * @param SiteAliasName $aliasName |
||
222 | * |
||
223 | * @return AliasRecord|false |
||
224 | */ |
||
225 | protected function loadSingleAliasFile(SiteAliasName $aliasName) |
||
235 | |||
236 | /** |
||
237 | * Given only the path to an alias file `site.alias.yml`, return all |
||
238 | * of the alias records for every environment stored in that file. |
||
239 | * |
||
240 | * @param string $path |
||
241 | * @return AliasRecord[] |
||
242 | */ |
||
243 | protected function loadSingleSiteAliasFileAtPath($path) |
||
251 | |||
252 | /** |
||
253 | * Given the path to a single site alias file `site.alias.yml`, |
||
254 | * return the `site` part. |
||
255 | * |
||
256 | * @param string $path |
||
257 | */ |
||
258 | protected function siteNameFromPath($path) |
||
262 | |||
263 | /** |
||
264 | * Chop off the `aliases.yml` or `alias.yml` part of a path. This works |
||
265 | * just like `basename`, except it will throw if the provided path |
||
266 | * does not end in the specified extension. |
||
267 | * |
||
268 | * @param string $path |
||
269 | * @param string $extension |
||
270 | * @return string |
||
271 | * @throws \Exception |
||
272 | */ |
||
273 | protected function basenameWithoutExtension($path, $extension) |
||
282 | |||
283 | /** |
||
284 | * Given an alias name and a path, load the data from the path |
||
285 | * and process it as needed to generate the alias record. |
||
286 | * |
||
287 | * @param SiteAliasName $aliasName |
||
288 | * @param string $path |
||
289 | * @return AliasRecord|false |
||
290 | */ |
||
291 | protected function loadSingleAliasFileWithNameAtPath(SiteAliasName $aliasName, $path) |
||
300 | |||
301 | /** |
||
302 | * Load the yml from the given path |
||
303 | * |
||
304 | * @param string $path |
||
305 | * @return array|bool |
||
306 | */ |
||
307 | protected function loadSiteDataFromPath($path) |
||
317 | |||
318 | /** |
||
319 | * Given an array of site aliases, find the first one that is |
||
320 | * local (has no 'host' item) and also contains a 'self.site.yml' file. |
||
321 | * @param array $data |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function findSelfSiteAliases($site_aliases) |
||
338 | |||
339 | /** |
||
340 | * Load the contents of the specified file. |
||
341 | * |
||
342 | * @param string $path Path to file to load |
||
343 | * @return array |
||
344 | */ |
||
345 | protected function loadData($path) |
||
356 | |||
357 | /** |
||
358 | * @return DataFileLoaderInterface |
||
359 | */ |
||
360 | public function getLoader($extension) |
||
367 | |||
368 | public function addLoader($extension, DataFileLoaderInterface $loader) |
||
372 | |||
373 | /** |
||
374 | * Given an array containing site alias data, return an alias record |
||
375 | * containing the data for the requested record. If there is a 'common' |
||
376 | * section, then merge that in as well. |
||
377 | * |
||
378 | * @param SiteAliasName $aliasName the alias we are loading |
||
379 | * @param array $data |
||
380 | * |
||
381 | * @return AliasRecord|false |
||
382 | */ |
||
383 | protected function fetchAliasRecordFromSiteAliasData(SiteAliasName $aliasName, ConfigProcessor $processor, array $data) |
||
402 | |||
403 | /** |
||
404 | * Determine whether there is a valid-looking environment '$env' in the |
||
405 | * provided site alias data. |
||
406 | * |
||
407 | * @param array $data |
||
408 | * @param string $env |
||
409 | * @return bool |
||
410 | */ |
||
411 | protected function siteEnvExists(array $data, $env) |
||
419 | |||
420 | /** |
||
421 | * Adjust the alias data for a single-site alias. Usually, a .yml alias |
||
422 | * file will contain multiple entries, one for each of the environments |
||
423 | * of an alias. If there are no environments |
||
424 | * |
||
425 | * @param array $data |
||
426 | * @return array |
||
427 | */ |
||
428 | protected function adjustIfSingleAlias($data) |
||
440 | |||
441 | /** |
||
442 | * A single-environment alias looks something like this: |
||
443 | * |
||
444 | * --- |
||
445 | * root: /path/to/drupal |
||
446 | * uri: https://mysite.org |
||
447 | * |
||
448 | * A multiple-environment alias looks something like this: |
||
449 | * |
||
450 | * --- |
||
451 | * default: dev |
||
452 | * dev: |
||
453 | * root: /path/to/dev |
||
454 | * uri: https://dev.mysite.org |
||
455 | * stage: |
||
456 | * root: /path/to/stage |
||
457 | * uri: https://stage.mysite.org |
||
458 | * |
||
459 | * The differentiator between these two is that the multi-environment |
||
460 | * alias always has top-level elements that are associative arrays, and |
||
461 | * the single-environment alias never does. |
||
462 | * |
||
463 | * @param array $data |
||
464 | * @return bool |
||
465 | */ |
||
466 | protected function detectSingleAlias($data) |
||
475 | |||
476 | /** |
||
477 | * Return the name of the environment requested. |
||
478 | * |
||
479 | * @param SiteAliasName $aliasName the alias we are loading |
||
480 | * @param array $data |
||
481 | * |
||
482 | * @return string |
||
483 | */ |
||
484 | protected function getEnvironmentName(SiteAliasName $aliasName, array $data) |
||
493 | |||
494 | /** |
||
495 | * Given a data array containing site alias environments, determine which |
||
496 | * envirionmnet should be used as the default environment. |
||
497 | * |
||
498 | * @param array $data |
||
499 | * @return string |
||
500 | */ |
||
501 | protected function getDefaultEnvironmentName(array $data) |
||
517 | } |
||
518 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.