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 |
||
| 20 | class Country |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The attributes array. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $attributes; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Create a new Country instance. |
||
| 31 | * |
||
| 32 | * @param array $attributes |
||
| 33 | * |
||
| 34 | * @throws \Exception |
||
| 35 | */ |
||
| 36 | public function __construct($attributes) |
||
| 37 | { |
||
| 38 | // Set the attributes |
||
| 39 | $this->setAttributes($attributes); |
||
| 40 | |||
| 41 | // Check required mandatory attributes |
||
| 42 | if (empty($this->getName()) || empty($this->getOfficialName()) |
||
| 43 | || empty($this->getNativeName()) || empty($this->getNativeOfficialName()) |
||
| 44 | || empty($this->getIsoAlpha2()) || empty($this->getIsoAlpha3())) { |
||
| 45 | throw new Exception('Missing mandatory country attributes!'); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set the attributes. |
||
| 51 | * |
||
| 52 | * @param array $attributes |
||
| 53 | * |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function setAttributes($attributes) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the attributes. |
||
| 65 | * |
||
| 66 | * @return array|null |
||
| 67 | */ |
||
| 68 | public function getAttributes() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set single attribute. |
||
| 75 | * |
||
| 76 | * @param string $key |
||
| 77 | * @param mixed $value |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | public function set($key, $value) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get an item from attributes array using "dot" notation. |
||
| 90 | * |
||
| 91 | * @param string $key |
||
| 92 | * @param mixed $default |
||
| 93 | * |
||
| 94 | * @return mixed |
||
| 95 | */ |
||
| 96 | public function get($key, $default = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the common name. |
||
| 121 | * |
||
| 122 | * @return string|null |
||
| 123 | */ |
||
| 124 | public function getName() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the official name. |
||
| 131 | * |
||
| 132 | * @return string|null |
||
| 133 | */ |
||
| 134 | public function getOfficialName() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get the given native name or fallback to first native name. |
||
| 141 | * |
||
| 142 | * @param string|null $language |
||
| 143 | * |
||
| 144 | * @return string|null |
||
| 145 | */ |
||
| 146 | public function getNativeName($language = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Get the given native official name or fallback to first native official name. |
||
| 156 | * |
||
| 157 | * @param string|null $language |
||
| 158 | * |
||
| 159 | * @return string|null |
||
| 160 | */ |
||
| 161 | public function getNativeOfficialName($language = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get the native names. |
||
| 171 | * |
||
| 172 | * @return array|null |
||
| 173 | */ |
||
| 174 | public function getNativeNames() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the demonym. |
||
| 181 | * |
||
| 182 | * @return string|null |
||
| 183 | */ |
||
| 184 | public function getDemonym() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the capital. |
||
| 191 | * |
||
| 192 | * @return string|null |
||
| 193 | */ |
||
| 194 | public function getCapital() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the ISO 3166-1 alpha2. |
||
| 201 | * |
||
| 202 | * @return string|null |
||
| 203 | */ |
||
| 204 | public function getIsoAlpha2() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get the ISO 3166-1 alpha3. |
||
| 211 | * |
||
| 212 | * @return string|null |
||
| 213 | */ |
||
| 214 | public function getIsoAlpha3() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the ISO 3166-1 numeric. |
||
| 221 | * |
||
| 222 | * @return string|null |
||
| 223 | */ |
||
| 224 | public function getIsoNumeric() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the given currency or fallback to first currency. |
||
| 231 | * |
||
| 232 | * @param string|null $currency |
||
| 233 | * |
||
| 234 | * @return string|null |
||
| 235 | */ |
||
| 236 | public function getCurrency($currency = null) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Get the currencies. |
||
| 245 | * |
||
| 246 | * @return array|null |
||
| 247 | */ |
||
| 248 | public function getCurrencies() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the TLD. |
||
| 255 | * |
||
| 256 | * @return string|null |
||
| 257 | */ |
||
| 258 | public function getTld() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get the TLDs. |
||
| 265 | * |
||
| 266 | * @return array|null |
||
| 267 | */ |
||
| 268 | public function getTlds() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Get the alternative spellings. |
||
| 275 | * |
||
| 276 | * @return array|null |
||
| 277 | */ |
||
| 278 | public function getAltSpellings() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Get the given language or fallback to first language. |
||
| 285 | * |
||
| 286 | * @param string|null $language |
||
| 287 | * |
||
| 288 | * @return string|null |
||
| 289 | */ |
||
| 290 | public function getLanguage($language = null) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Get the languages. |
||
| 299 | * |
||
| 300 | * @return array|null |
||
| 301 | */ |
||
| 302 | public function getLanguages() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Get the translations. |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getTranslations() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get the translation. |
||
| 340 | * |
||
| 341 | * @param string|null $language |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | public function getTranslation($language = null) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the geodata. |
||
| 353 | * |
||
| 354 | * @return array|null |
||
| 355 | */ |
||
| 356 | public function getGeodata() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the continent. |
||
| 363 | * |
||
| 364 | * @return string|null |
||
| 365 | */ |
||
| 366 | public function getContinent() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Determine whether the country uses postal code. |
||
| 373 | * |
||
| 374 | * @return bool|null |
||
| 375 | */ |
||
| 376 | public function usesPostalCode() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the latitude. |
||
| 383 | * |
||
| 384 | * @return string|null |
||
| 385 | */ |
||
| 386 | public function getLatitude() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Get the longitude. |
||
| 393 | * |
||
| 394 | * @return string|null |
||
| 395 | */ |
||
| 396 | public function getLongitude() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the described latitude. |
||
| 403 | * |
||
| 404 | * @return string|null |
||
| 405 | */ |
||
| 406 | public function getLatitudeDesc() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the described longitude. |
||
| 413 | * |
||
| 414 | * @return string|null |
||
| 415 | */ |
||
| 416 | public function getLongitudeDesc() |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get the maximum latitude. |
||
| 423 | * |
||
| 424 | * @return string|null |
||
| 425 | */ |
||
| 426 | public function getMaxLatitude() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the maximum longitude. |
||
| 433 | * |
||
| 434 | * @return string|null |
||
| 435 | */ |
||
| 436 | public function getMaxLongitude() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get the minimum latitude. |
||
| 443 | * |
||
| 444 | * @return string|null |
||
| 445 | */ |
||
| 446 | public function getMinLatitude() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get the minimum longitude. |
||
| 453 | * |
||
| 454 | * @return string|null |
||
| 455 | */ |
||
| 456 | public function getMinLongitude() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Get the area. |
||
| 463 | * |
||
| 464 | * @return int|null |
||
| 465 | */ |
||
| 466 | public function getArea() |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Get the region. |
||
| 473 | * |
||
| 474 | * @return string|null |
||
| 475 | */ |
||
| 476 | public function getRegion() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Get the subregion. |
||
| 483 | * |
||
| 484 | * @return string|null |
||
| 485 | */ |
||
| 486 | public function getSubregion() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get the world region. |
||
| 493 | * |
||
| 494 | * @return string|null |
||
| 495 | */ |
||
| 496 | public function getWorldRegion() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get the region code. |
||
| 503 | * |
||
| 504 | * @return string|null |
||
| 505 | */ |
||
| 506 | public function getRegionCode() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Get the subregion code. |
||
| 513 | * |
||
| 514 | * @return string|null |
||
| 515 | */ |
||
| 516 | public function getSubregionCode() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Check the landlock status. |
||
| 523 | * |
||
| 524 | * @return bool|null |
||
| 525 | */ |
||
| 526 | public function isLandlocked() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Get the borders. |
||
| 533 | * |
||
| 534 | * @return array|null |
||
| 535 | */ |
||
| 536 | public function getBorders() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Determine whether the country is independent. |
||
| 543 | * |
||
| 544 | * @return string|null |
||
| 545 | */ |
||
| 546 | public function isIndependent() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Get the given calling code or fallback to first calling code. |
||
| 553 | * |
||
| 554 | * @return string|null |
||
| 555 | */ |
||
| 556 | public function getCallingCode() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Get the calling codes. |
||
| 563 | * |
||
| 564 | * @return array|null |
||
| 565 | */ |
||
| 566 | public function getCallingCodes() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Get the national prefix. |
||
| 573 | * |
||
| 574 | * @return string|null |
||
| 575 | */ |
||
| 576 | public function getNationalPrefix() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Get the national number length. |
||
| 583 | * |
||
| 584 | * @return int|null |
||
| 585 | */ |
||
| 586 | public function getNationalNumberLength() |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get the national number lengths. |
||
| 593 | * |
||
| 594 | * @return array|null |
||
| 595 | */ |
||
| 596 | public function getNationalNumberLengths() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Get the national destination code length. |
||
| 603 | * |
||
| 604 | * @return int|null |
||
| 605 | */ |
||
| 606 | public function getNationalDestinationCodeLength() |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Get the national destination code lengths. |
||
| 613 | * |
||
| 614 | * @return array|null |
||
| 615 | */ |
||
| 616 | public function getnationaldestinationcodelengths() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get the international prefix. |
||
| 623 | * |
||
| 624 | * @return string|null |
||
| 625 | */ |
||
| 626 | public function getInternationalPrefix() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get the extras. |
||
| 633 | * |
||
| 634 | * @return array|null |
||
| 635 | */ |
||
| 636 | public function getExtra() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Get the geonameid. |
||
| 643 | * |
||
| 644 | * @return int|null |
||
| 645 | */ |
||
| 646 | public function getGeonameid() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Get the edgar code. |
||
| 653 | * |
||
| 654 | * @return string|null |
||
| 655 | */ |
||
| 656 | public function getEdgar() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Get the itu code. |
||
| 663 | * |
||
| 664 | * @return string|null |
||
| 665 | */ |
||
| 666 | public function getItu() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Get the marc code. |
||
| 673 | * |
||
| 674 | * @return string|null |
||
| 675 | */ |
||
| 676 | public function getMarc() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Get the wmo code. |
||
| 683 | * |
||
| 684 | * @return string|null |
||
| 685 | */ |
||
| 686 | public function getWmo() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Get the ds code. |
||
| 693 | * |
||
| 694 | * @return string|null |
||
| 695 | */ |
||
| 696 | public function getDs() |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Get the fifa code. |
||
| 703 | * |
||
| 704 | * @return string|null |
||
| 705 | */ |
||
| 706 | public function getFifa() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Get the fips code. |
||
| 713 | * |
||
| 714 | * @return string|null |
||
| 715 | */ |
||
| 716 | public function getFips() |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Get the gaul code. |
||
| 723 | * |
||
| 724 | * @return int|null |
||
| 725 | */ |
||
| 726 | public function getGaul() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Get the ioc code. |
||
| 733 | * |
||
| 734 | * @return string|null |
||
| 735 | */ |
||
| 736 | public function getIoc() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Get the cowc code. |
||
| 743 | * |
||
| 744 | * @return string|null |
||
| 745 | */ |
||
| 746 | public function getCowc() |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Get the cown code. |
||
| 753 | * |
||
| 754 | * @return int|null |
||
| 755 | */ |
||
| 756 | public function getCown() |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Get the fao code. |
||
| 763 | * |
||
| 764 | * @return int|null |
||
| 765 | */ |
||
| 766 | public function getFao() |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Get the imf code. |
||
| 773 | * |
||
| 774 | * @return int|null |
||
| 775 | */ |
||
| 776 | public function getImf() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Get the ar5 code. |
||
| 783 | * |
||
| 784 | * @return string|null |
||
| 785 | */ |
||
| 786 | public function getAr5() |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Get the address format. |
||
| 793 | * |
||
| 794 | * @return string|null |
||
| 795 | */ |
||
| 796 | public function getAddressFormat() |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Determine whether the country is EU member. |
||
| 803 | * |
||
| 804 | * @return bool|null |
||
| 805 | */ |
||
| 806 | public function isEuMember() |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Get the VAT rates. |
||
| 813 | * |
||
| 814 | * @return array|null |
||
| 815 | */ |
||
| 816 | public function getVatRates() |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Get the emoji. |
||
| 823 | * |
||
| 824 | * @return array|null |
||
| 825 | */ |
||
| 826 | public function getEmoji() |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Get the geographic data structure. |
||
| 833 | * |
||
| 834 | * @return string|null |
||
| 835 | */ |
||
| 836 | public function getGeoJson() |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Get the flag. |
||
| 845 | * |
||
| 846 | * @return string|null |
||
| 847 | */ |
||
| 848 | public function getFlag() |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Get the divisions. |
||
| 857 | * |
||
| 858 | * @return array|null |
||
| 859 | */ |
||
| 860 | public function getDivisions() |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Get the divisions. |
||
| 869 | * |
||
| 870 | * @param string $division |
||
| 871 | * |
||
| 872 | * @return array|null |
||
| 873 | */ |
||
| 874 | public function getDivision($division) |
||
| 879 | } |
||
| 880 |