Complex classes like Country 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 Country, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Country |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The attributes array. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $attributes; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Create a new Country instance. |
||
| 20 | * |
||
| 21 | * @param array $attributes |
||
| 22 | * |
||
| 23 | * @throws \Exception |
||
| 24 | */ |
||
| 25 | public function __construct($attributes) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Set the attributes. |
||
| 40 | * |
||
| 41 | * @param array $attributes |
||
| 42 | * |
||
| 43 | * @return $this |
||
| 44 | */ |
||
| 45 | public function setAttributes($attributes) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get the attributes. |
||
| 54 | * |
||
| 55 | * @return array|null |
||
| 56 | */ |
||
| 57 | public function getAttributes(): ?array |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set single attribute. |
||
| 64 | * |
||
| 65 | * @param string $key |
||
| 66 | * @param mixed $value |
||
| 67 | * |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | public function set($key, $value) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get an item from attributes array using "dot" notation. |
||
| 79 | * |
||
| 80 | * @param string $key |
||
| 81 | * @param mixed $default |
||
| 82 | * |
||
| 83 | * @return mixed |
||
| 84 | */ |
||
| 85 | public function get($key, $default = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the common name. |
||
| 110 | * |
||
| 111 | * @return string|null |
||
| 112 | */ |
||
| 113 | public function getName(): ?string |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get the official name. |
||
| 120 | * |
||
| 121 | * @return string|null |
||
| 122 | */ |
||
| 123 | public function getOfficialName(): ?string |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get the given native name or fallback to first native name. |
||
| 130 | * |
||
| 131 | * @param string|null $languageCode |
||
| 132 | * |
||
| 133 | * @return string|null |
||
| 134 | */ |
||
| 135 | public function getNativeName($languageCode = null): ?string |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the given native official name or fallback to first native official name. |
||
| 145 | * |
||
| 146 | * @param string|null $languageCode |
||
| 147 | * |
||
| 148 | * @return string|null |
||
| 149 | */ |
||
| 150 | public function getNativeOfficialName($languageCode = null): ?string |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the native names. |
||
| 160 | * |
||
| 161 | * @return array|null |
||
| 162 | */ |
||
| 163 | public function getNativeNames(): ?array |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get the demonym. |
||
| 170 | * |
||
| 171 | * @return string|null |
||
| 172 | */ |
||
| 173 | public function getDemonym(): ?string |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the capital. |
||
| 180 | * |
||
| 181 | * @return string|null |
||
| 182 | */ |
||
| 183 | public function getCapital(): ?string |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get the ISO 3166-1 alpha2. |
||
| 190 | * |
||
| 191 | * @return string|null |
||
| 192 | */ |
||
| 193 | public function getIsoAlpha2() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get the ISO 3166-1 alpha3. |
||
| 200 | * |
||
| 201 | * @return string|null |
||
| 202 | */ |
||
| 203 | public function getIsoAlpha3() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the ISO 3166-1 numeric. |
||
| 210 | * |
||
| 211 | * @return string|null |
||
| 212 | */ |
||
| 213 | public function getIsoNumeric(): ?string |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the given currency or fallback to first currency. |
||
| 220 | * |
||
| 221 | * @param string|null $currency |
||
| 222 | * |
||
| 223 | * @return array|null |
||
| 224 | */ |
||
| 225 | public function getCurrency($currency = null): ?array |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get the currencies. |
||
| 234 | * |
||
| 235 | * @return array|null |
||
| 236 | */ |
||
| 237 | public function getCurrencies(): ?array |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the TLD. |
||
| 244 | * |
||
| 245 | * @return string|null |
||
| 246 | */ |
||
| 247 | public function getTld(): ?string |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the TLDs. |
||
| 254 | * |
||
| 255 | * @return array|null |
||
| 256 | */ |
||
| 257 | public function getTlds(): ?array |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get the alternative spellings. |
||
| 264 | * |
||
| 265 | * @return array|null |
||
| 266 | */ |
||
| 267 | public function getAltSpellings(): ?array |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the given language or fallback to first language. |
||
| 274 | * |
||
| 275 | * @param string|null $languageCode |
||
| 276 | * |
||
| 277 | * @return string|null |
||
| 278 | */ |
||
| 279 | public function getLanguage($languageCode = null): ?string |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the languages. |
||
| 288 | * |
||
| 289 | * @return array|null |
||
| 290 | */ |
||
| 291 | public function getLanguages(): ?array |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Get the translations. |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function getTranslations(): array |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get the translation. |
||
| 329 | * |
||
| 330 | * @param string|null $languageCode |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function getTranslation($languageCode = null): array |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get the geodata. |
||
| 341 | * |
||
| 342 | * @return array|null |
||
| 343 | */ |
||
| 344 | public function getGeodata(): ?array |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get the continent. |
||
| 351 | * |
||
| 352 | * @return string|null |
||
| 353 | */ |
||
| 354 | public function getContinent(): ?string |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Determine whether the country uses postal code. |
||
| 361 | * |
||
| 362 | * @return bool|null |
||
| 363 | */ |
||
| 364 | public function usesPostalCode() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get the latitude. |
||
| 371 | * |
||
| 372 | * @return string|null |
||
| 373 | */ |
||
| 374 | public function getLatitude(): ?string |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the longitude. |
||
| 381 | * |
||
| 382 | * @return string|null |
||
| 383 | */ |
||
| 384 | public function getLongitude(): ?string |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Get the described latitude. |
||
| 391 | * |
||
| 392 | * @return string|null |
||
| 393 | */ |
||
| 394 | public function getLatitudeDesc(): ?string |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get the described longitude. |
||
| 401 | * |
||
| 402 | * @return string|null |
||
| 403 | */ |
||
| 404 | public function getLongitudeDesc(): ?string |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get the maximum latitude. |
||
| 411 | * |
||
| 412 | * @return string|null |
||
| 413 | */ |
||
| 414 | public function getMaxLatitude(): ?string |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get the maximum longitude. |
||
| 421 | * |
||
| 422 | * @return string|null |
||
| 423 | */ |
||
| 424 | public function getMaxLongitude(): ?string |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get the minimum latitude. |
||
| 431 | * |
||
| 432 | * @return string|null |
||
| 433 | */ |
||
| 434 | public function getMinLatitude(): ?string |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get the minimum longitude. |
||
| 441 | * |
||
| 442 | * @return string|null |
||
| 443 | */ |
||
| 444 | public function getMinLongitude(): ?string |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Get the area. |
||
| 451 | * |
||
| 452 | * @return int|null |
||
| 453 | */ |
||
| 454 | public function getArea(): ?int |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Get the region. |
||
| 461 | * |
||
| 462 | * @return string|null |
||
| 463 | */ |
||
| 464 | public function getRegion(): ?string |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get the subregion. |
||
| 471 | * |
||
| 472 | * @return string|null |
||
| 473 | */ |
||
| 474 | public function getSubregion(): ?string |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the world region. |
||
| 481 | * |
||
| 482 | * @return string|null |
||
| 483 | */ |
||
| 484 | public function getWorldRegion(): ?string |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Get the region code. |
||
| 491 | * |
||
| 492 | * @return string|null |
||
| 493 | */ |
||
| 494 | public function getRegionCode(): ?string |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the subregion code. |
||
| 501 | * |
||
| 502 | * @return string|null |
||
| 503 | */ |
||
| 504 | public function getSubregionCode(): ?string |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Check the landlock status. |
||
| 511 | * |
||
| 512 | * @return bool|null |
||
| 513 | */ |
||
| 514 | public function isLandlocked() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get the borders. |
||
| 521 | * |
||
| 522 | * @return array|null |
||
| 523 | */ |
||
| 524 | public function getBorders(): ?array |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Determine whether the country is independent. |
||
| 531 | * |
||
| 532 | * @return string|null |
||
| 533 | */ |
||
| 534 | public function isIndependent(): ?string |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Get the given calling code or fallback to first calling code. |
||
| 541 | * |
||
| 542 | * @return string|null |
||
| 543 | */ |
||
| 544 | public function getCallingCode(): ?string |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get the calling codes. |
||
| 551 | * |
||
| 552 | * @return array|null |
||
| 553 | */ |
||
| 554 | public function getCallingCodes(): ?array |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Get the national prefix. |
||
| 561 | * |
||
| 562 | * @return string|null |
||
| 563 | */ |
||
| 564 | public function getNationalPrefix(): ?string |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Get the national number length. |
||
| 571 | * |
||
| 572 | * @return int|null |
||
| 573 | */ |
||
| 574 | public function getNationalNumberLength(): ?int |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Get the national number lengths. |
||
| 581 | * |
||
| 582 | * @return array|null |
||
| 583 | */ |
||
| 584 | public function getNationalNumberLengths(): ?array |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get the national destination code length. |
||
| 591 | * |
||
| 592 | * @return int|null |
||
| 593 | */ |
||
| 594 | public function getNationalDestinationCodeLength(): ?int |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Get the national destination code lengths. |
||
| 601 | * |
||
| 602 | * @return array|null |
||
| 603 | */ |
||
| 604 | public function getnationaldestinationcodelengths(): ?array |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Get the international prefix. |
||
| 611 | * |
||
| 612 | * @return string|null |
||
| 613 | */ |
||
| 614 | public function getInternationalPrefix(): ?string |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Get the extras. |
||
| 621 | * |
||
| 622 | * @return array|null |
||
| 623 | */ |
||
| 624 | public function getExtra(): ?array |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get the geonameid. |
||
| 631 | * |
||
| 632 | * @return int|null |
||
| 633 | */ |
||
| 634 | public function getGeonameid(): ?int |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Get the edgar code. |
||
| 641 | * |
||
| 642 | * @return string|null |
||
| 643 | */ |
||
| 644 | public function getEdgar(): ?string |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Get the itu code. |
||
| 651 | * |
||
| 652 | * @return string|null |
||
| 653 | */ |
||
| 654 | public function getItu(): ?string |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get the marc code. |
||
| 661 | * |
||
| 662 | * @return string|null |
||
| 663 | */ |
||
| 664 | public function getMarc(): ?string |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Get the wmo code. |
||
| 671 | * |
||
| 672 | * @return string|null |
||
| 673 | */ |
||
| 674 | public function getWmo(): ?string |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Get the ds code. |
||
| 681 | * |
||
| 682 | * @return string|null |
||
| 683 | */ |
||
| 684 | public function getDs(): ?string |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Get the fifa code. |
||
| 691 | * |
||
| 692 | * @return string|null |
||
| 693 | */ |
||
| 694 | public function getFifa(): ?string |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Get the fips code. |
||
| 701 | * |
||
| 702 | * @return string|null |
||
| 703 | */ |
||
| 704 | public function getFips(): ?string |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Get the gaul code. |
||
| 711 | * |
||
| 712 | * @return int|null |
||
| 713 | */ |
||
| 714 | public function getGaul(): ?int |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Get the ioc code. |
||
| 721 | * |
||
| 722 | * @return string|null |
||
| 723 | */ |
||
| 724 | public function getIoc(): ?string |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Get the cowc code. |
||
| 731 | * |
||
| 732 | * @return string|null |
||
| 733 | */ |
||
| 734 | public function getCowc(): ?string |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Get the cown code. |
||
| 741 | * |
||
| 742 | * @return int|null |
||
| 743 | */ |
||
| 744 | public function getCown(): ?int |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Get the fao code. |
||
| 751 | * |
||
| 752 | * @return int|null |
||
| 753 | */ |
||
| 754 | public function getFao(): ?int |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Get the imf code. |
||
| 761 | * |
||
| 762 | * @return int|null |
||
| 763 | */ |
||
| 764 | public function getImf(): ?int |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Get the ar5 code. |
||
| 771 | * |
||
| 772 | * @return string|null |
||
| 773 | */ |
||
| 774 | public function getAr5() |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Get the address format. |
||
| 781 | * |
||
| 782 | * @return string|null |
||
| 783 | */ |
||
| 784 | public function getAddressFormat(): ?string |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Determine whether the country is EU member. |
||
| 791 | * |
||
| 792 | * @return bool|null |
||
| 793 | */ |
||
| 794 | public function isEuMember() |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Get the VAT rates. |
||
| 801 | * |
||
| 802 | * @return array|null |
||
| 803 | */ |
||
| 804 | public function getVatRates(): ?array |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Get the emoji. |
||
| 811 | * |
||
| 812 | * @return string|null |
||
| 813 | */ |
||
| 814 | public function getEmoji(): ?string |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Get the geographic data structure. |
||
| 821 | * |
||
| 822 | * @return string|null |
||
| 823 | */ |
||
| 824 | public function getGeoJson(): ?string |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Get the flag. |
||
| 835 | * |
||
| 836 | * @return string|null |
||
| 837 | */ |
||
| 838 | public function getFlag(): ?string |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Get the divisions. |
||
| 849 | * |
||
| 850 | * @return array|null |
||
| 851 | */ |
||
| 852 | public function getDivisions(): ?array |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Get the divisions. |
||
| 863 | * |
||
| 864 | * @param string $division |
||
| 865 | * |
||
| 866 | * @return array|null |
||
| 867 | */ |
||
| 868 | public function getDivision($division): ?array |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Get the contents of the file by the given directory and code. |
||
| 877 | * |
||
| 878 | * @param string $directory |
||
| 879 | * @param string $filename |
||
| 880 | * @return null|string |
||
| 881 | */ |
||
| 882 | private function getResourceFileContents(string $directory, string $filename): ?string |
||
| 888 | } |
||
| 889 |