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 |
||
| 12 | class Country |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The attributes array. |
||
| 16 | * |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $attributes; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Create a new Country instance. |
||
| 23 | * |
||
| 24 | * @param array $attributes |
||
| 25 | * |
||
| 26 | * @throws \Exception |
||
| 27 | */ |
||
| 28 | public function __construct($attributes) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Set the attributes. |
||
| 43 | * |
||
| 44 | * @param array $attributes |
||
| 45 | * |
||
| 46 | * @return $this |
||
| 47 | */ |
||
| 48 | public function setAttributes($attributes) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the attributes. |
||
| 57 | * |
||
| 58 | * @return array|null |
||
| 59 | */ |
||
| 60 | public function getAttributes(): ?array |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Set single attribute. |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * @param mixed $value |
||
| 70 | * |
||
| 71 | * @return $this |
||
| 72 | */ |
||
| 73 | public function set($key, $value) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get an item from attributes array using "dot" notation. |
||
| 82 | * |
||
| 83 | * @param string $key |
||
| 84 | * @param mixed $default |
||
| 85 | * |
||
| 86 | * @return mixed |
||
| 87 | */ |
||
| 88 | public function get($key, $default = null) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the common name. |
||
| 113 | * |
||
| 114 | * @return string|null |
||
| 115 | */ |
||
| 116 | public function getName(): ?string |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the official name. |
||
| 123 | * |
||
| 124 | * @return string|null |
||
| 125 | */ |
||
| 126 | public function getOfficialName(): ?string |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the given native name or fallback to first native name. |
||
| 133 | * |
||
| 134 | * @param string|null $languageCode |
||
| 135 | * |
||
| 136 | * @return string|null |
||
| 137 | */ |
||
| 138 | public function getNativeName($languageCode = null): ?string |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the given native official name or fallback to first native official name. |
||
| 148 | * |
||
| 149 | * @param string|null $languageCode |
||
| 150 | * |
||
| 151 | * @return string|null |
||
| 152 | */ |
||
| 153 | public function getNativeOfficialName($languageCode = null): ?string |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the native names. |
||
| 163 | * |
||
| 164 | * @return array|null |
||
| 165 | */ |
||
| 166 | public function getNativeNames(): ?array |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the demonym. |
||
| 173 | * |
||
| 174 | * @return string|null |
||
| 175 | */ |
||
| 176 | public function getDemonym(): ?string |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the capital. |
||
| 183 | * |
||
| 184 | * @return string|null |
||
| 185 | */ |
||
| 186 | public function getCapital(): ?string |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get the ISO 3166-1 alpha2. |
||
| 193 | * |
||
| 194 | * @return string|null |
||
| 195 | */ |
||
| 196 | public function getIsoAlpha2() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get the ISO 3166-1 alpha3. |
||
| 203 | * |
||
| 204 | * @return string|null |
||
| 205 | */ |
||
| 206 | public function getIsoAlpha3() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the ISO 3166-1 numeric. |
||
| 213 | * |
||
| 214 | * @return string|null |
||
| 215 | */ |
||
| 216 | public function getIsoNumeric(): ?string |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the given currency or fallback to first currency. |
||
| 223 | * |
||
| 224 | * @param string|null $currency |
||
| 225 | * |
||
| 226 | * @return array|null |
||
| 227 | */ |
||
| 228 | public function getCurrency($currency = null): ?array |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the currencies. |
||
| 237 | * |
||
| 238 | * @return array|null |
||
| 239 | */ |
||
| 240 | public function getCurrencies(): ?array |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Get the TLD. |
||
| 247 | * |
||
| 248 | * @return string|null |
||
| 249 | */ |
||
| 250 | public function getTld(): ?string |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the TLDs. |
||
| 257 | * |
||
| 258 | * @return array|null |
||
| 259 | */ |
||
| 260 | public function getTlds(): ?array |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Get the alternative spellings. |
||
| 267 | * |
||
| 268 | * @return array|null |
||
| 269 | */ |
||
| 270 | public function getAltSpellings(): ?array |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the given language or fallback to first language. |
||
| 277 | * |
||
| 278 | * @param string|null $languageCode |
||
| 279 | * |
||
| 280 | * @return string|null |
||
| 281 | */ |
||
| 282 | public function getLanguage($languageCode = null): ?string |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the languages. |
||
| 291 | * |
||
| 292 | * @return array|null |
||
| 293 | */ |
||
| 294 | public function getLanguages(): ?array |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get the translations. |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function getTranslations(): array |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the translation. |
||
| 332 | * |
||
| 333 | * @param string|null $languageCode |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | public function getTranslation($languageCode = null): array |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get the geodata. |
||
| 344 | * |
||
| 345 | * @return array|null |
||
| 346 | */ |
||
| 347 | public function getGeodata(): ?array |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the continent. |
||
| 354 | * |
||
| 355 | * @return string|null |
||
| 356 | */ |
||
| 357 | public function getContinent(): ?string |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Determine whether the country uses postal code. |
||
| 364 | * |
||
| 365 | * @return bool|null |
||
| 366 | */ |
||
| 367 | public function usesPostalCode() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the latitude. |
||
| 374 | * |
||
| 375 | * @return string|null |
||
| 376 | */ |
||
| 377 | public function getLatitude(): ?string |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the longitude. |
||
| 384 | * |
||
| 385 | * @return string|null |
||
| 386 | */ |
||
| 387 | public function getLongitude(): ?string |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Get the described latitude. |
||
| 394 | * |
||
| 395 | * @return string|null |
||
| 396 | */ |
||
| 397 | public function getLatitudeDesc(): ?string |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get the described longitude. |
||
| 404 | * |
||
| 405 | * @return string|null |
||
| 406 | */ |
||
| 407 | public function getLongitudeDesc(): ?string |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Get the maximum latitude. |
||
| 414 | * |
||
| 415 | * @return string|null |
||
| 416 | */ |
||
| 417 | public function getMaxLatitude(): ?string |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Get the maximum longitude. |
||
| 424 | * |
||
| 425 | * @return string|null |
||
| 426 | */ |
||
| 427 | public function getMaxLongitude(): ?string |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the minimum latitude. |
||
| 434 | * |
||
| 435 | * @return string|null |
||
| 436 | */ |
||
| 437 | public function getMinLatitude(): ?string |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get the minimum longitude. |
||
| 444 | * |
||
| 445 | * @return string|null |
||
| 446 | */ |
||
| 447 | public function getMinLongitude(): ?string |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Get the area. |
||
| 454 | * |
||
| 455 | * @return int|null |
||
| 456 | */ |
||
| 457 | public function getArea(): ?int |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Get the region. |
||
| 464 | * |
||
| 465 | * @return string|null |
||
| 466 | */ |
||
| 467 | public function getRegion(): ?string |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the subregion. |
||
| 474 | * |
||
| 475 | * @return string|null |
||
| 476 | */ |
||
| 477 | public function getSubregion(): ?string |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Get the world region. |
||
| 484 | * |
||
| 485 | * @return string|null |
||
| 486 | */ |
||
| 487 | public function getWorldRegion(): ?string |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get the region code. |
||
| 494 | * |
||
| 495 | * @return string|null |
||
| 496 | */ |
||
| 497 | public function getRegionCode(): ?string |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Get the subregion code. |
||
| 504 | * |
||
| 505 | * @return string|null |
||
| 506 | */ |
||
| 507 | public function getSubregionCode(): ?string |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Check the landlock status. |
||
| 514 | * |
||
| 515 | * @return bool|null |
||
| 516 | */ |
||
| 517 | public function isLandlocked() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Get the borders. |
||
| 524 | * |
||
| 525 | * @return array|null |
||
| 526 | */ |
||
| 527 | public function getBorders(): ?array |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Determine whether the country is independent. |
||
| 534 | * |
||
| 535 | * @return string|null |
||
| 536 | */ |
||
| 537 | public function isIndependent(): ?string |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get the given calling code or fallback to first calling code. |
||
| 544 | * |
||
| 545 | * @return string|null |
||
| 546 | */ |
||
| 547 | public function getCallingCode(): ?string |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the calling codes. |
||
| 554 | * |
||
| 555 | * @return array|null |
||
| 556 | */ |
||
| 557 | public function getCallingCodes(): ?array |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get the national prefix. |
||
| 564 | * |
||
| 565 | * @return string|null |
||
| 566 | */ |
||
| 567 | public function getNationalPrefix(): ?string |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Get the national number length. |
||
| 574 | * |
||
| 575 | * @return int|null |
||
| 576 | */ |
||
| 577 | public function getNationalNumberLength(): ?int |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get the national number lengths. |
||
| 584 | * |
||
| 585 | * @return array|null |
||
| 586 | */ |
||
| 587 | public function getNationalNumberLengths(): ?array |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get the national destination code length. |
||
| 594 | * |
||
| 595 | * @return int|null |
||
| 596 | */ |
||
| 597 | public function getNationalDestinationCodeLength(): ?int |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get the national destination code lengths. |
||
| 604 | * |
||
| 605 | * @return array|null |
||
| 606 | */ |
||
| 607 | public function getnationaldestinationcodelengths(): ?array |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Get the international prefix. |
||
| 614 | * |
||
| 615 | * @return string|null |
||
| 616 | */ |
||
| 617 | public function getInternationalPrefix(): ?string |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Get the extras. |
||
| 624 | * |
||
| 625 | * @return array|null |
||
| 626 | */ |
||
| 627 | public function getExtra(): ?array |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Get the geonameid. |
||
| 634 | * |
||
| 635 | * @return int|null |
||
| 636 | */ |
||
| 637 | public function getGeonameid(): ?int |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Get the edgar code. |
||
| 644 | * |
||
| 645 | * @return string|null |
||
| 646 | */ |
||
| 647 | public function getEdgar(): ?string |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Get the itu code. |
||
| 654 | * |
||
| 655 | * @return string|null |
||
| 656 | */ |
||
| 657 | public function getItu(): ?string |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get the marc code. |
||
| 664 | * |
||
| 665 | * @return string|null |
||
| 666 | */ |
||
| 667 | public function getMarc(): ?string |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Get the wmo code. |
||
| 674 | * |
||
| 675 | * @return string|null |
||
| 676 | */ |
||
| 677 | public function getWmo(): ?string |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Get the ds code. |
||
| 684 | * |
||
| 685 | * @return string|null |
||
| 686 | */ |
||
| 687 | public function getDs(): ?string |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Get the fifa code. |
||
| 694 | * |
||
| 695 | * @return string|null |
||
| 696 | */ |
||
| 697 | public function getFifa(): ?string |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Get the fips code. |
||
| 704 | * |
||
| 705 | * @return string|null |
||
| 706 | */ |
||
| 707 | public function getFips(): ?string |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Get the gaul code. |
||
| 714 | * |
||
| 715 | * @return int|null |
||
| 716 | */ |
||
| 717 | public function getGaul(): ?int |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Get the ioc code. |
||
| 724 | * |
||
| 725 | * @return string|null |
||
| 726 | */ |
||
| 727 | public function getIoc(): ?string |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Get the cowc code. |
||
| 734 | * |
||
| 735 | * @return string|null |
||
| 736 | */ |
||
| 737 | public function getCowc(): ?string |
||
| 741 | |||
| 742 | /** |
||
| 743 | * Get the cown code. |
||
| 744 | * |
||
| 745 | * @return int|null |
||
| 746 | */ |
||
| 747 | public function getCown(): ?int |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Get the fao code. |
||
| 754 | * |
||
| 755 | * @return int|null |
||
| 756 | */ |
||
| 757 | public function getFao(): ?int |
||
| 761 | |||
| 762 | /** |
||
| 763 | * Get the imf code. |
||
| 764 | * |
||
| 765 | * @return int|null |
||
| 766 | */ |
||
| 767 | public function getImf(): ?int |
||
| 771 | |||
| 772 | /** |
||
| 773 | * Get the ar5 code. |
||
| 774 | * |
||
| 775 | * @return string|null |
||
| 776 | */ |
||
| 777 | public function getAr5() |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Get the address format. |
||
| 784 | * |
||
| 785 | * @return string|null |
||
| 786 | */ |
||
| 787 | public function getAddressFormat(): ?string |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Determine whether the country is EU member. |
||
| 794 | * |
||
| 795 | * @return bool|null |
||
| 796 | */ |
||
| 797 | public function isEuMember() |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Get the VAT rates. |
||
| 804 | * |
||
| 805 | * @return array|null |
||
| 806 | */ |
||
| 807 | public function getVatRates(): ?array |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get the emoji. |
||
| 814 | * |
||
| 815 | * @return string|null |
||
| 816 | */ |
||
| 817 | public function getEmoji(): ?string |
||
| 821 | |||
| 822 | /** |
||
| 823 | * Get the geographic data structure. |
||
| 824 | * |
||
| 825 | * @return string|null |
||
| 826 | */ |
||
| 827 | public function getGeoJson(): ?string |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Get the flag. |
||
| 838 | * |
||
| 839 | * @return string|null |
||
| 840 | */ |
||
| 841 | public function getFlag(): ?string |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Get the divisions. |
||
| 852 | * |
||
| 853 | * @return array|null |
||
| 854 | */ |
||
| 855 | public function getDivisions(): ?array |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Get the divisions. |
||
| 866 | * |
||
| 867 | * @param string $division |
||
| 868 | * |
||
| 869 | * @return array|null |
||
| 870 | */ |
||
| 871 | public function getDivision($division): ?array |
||
| 876 | |||
| 877 | /** |
||
| 878 | * Get the timezones. |
||
| 879 | * |
||
| 880 | * @return array|null |
||
| 881 | */ |
||
| 882 | public function getTimezones() |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Get the locales. |
||
| 893 | * |
||
| 894 | * @return array|null |
||
| 895 | */ |
||
| 896 | public function getLocales() |
||
| 911 | } |
||
| 912 |