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 |
||
| 25 | class Country |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * The attributes array. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $attributes; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new Country instance. |
||
| 36 | * |
||
| 37 | * @param array $attributes |
||
| 38 | * |
||
| 39 | * @throws \Exception |
||
| 40 | */ |
||
| 41 | public function __construct($attributes) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Set the attributes. |
||
| 56 | * |
||
| 57 | * @param array $attributes |
||
| 58 | * |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | public function setAttributes($attributes) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get the attributes. |
||
| 70 | * |
||
| 71 | * @return array|null |
||
| 72 | */ |
||
| 73 | public function getAttributes() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set single attribute. |
||
| 80 | * |
||
| 81 | * @param string $key |
||
| 82 | * @param mixed $value |
||
| 83 | * |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | public function set($key, $value) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get an item from attributes array using "dot" notation. |
||
| 95 | * |
||
| 96 | * @param string $key |
||
| 97 | * @param mixed $default |
||
| 98 | * |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | public function get($key, $default = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the common name. |
||
| 126 | * |
||
| 127 | * @return string|null |
||
| 128 | */ |
||
| 129 | public function getName() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the official name. |
||
| 136 | * |
||
| 137 | * @return string|null |
||
| 138 | */ |
||
| 139 | public function getOfficialName() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the given native name or fallback to first native name. |
||
| 146 | * |
||
| 147 | * @param string|null $language |
||
| 148 | * |
||
| 149 | * @return string|null |
||
| 150 | */ |
||
| 151 | public function getNativeName($language = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the given native official name or fallback to first native official name. |
||
| 161 | * |
||
| 162 | * @param string|null $language |
||
| 163 | * |
||
| 164 | * @return string|null |
||
| 165 | */ |
||
| 166 | public function getNativeOfficialName($language = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the native names. |
||
| 176 | * |
||
| 177 | * @return array|null |
||
| 178 | */ |
||
| 179 | public function getNativeNames() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the demonym. |
||
| 186 | * |
||
| 187 | * @return string|null |
||
| 188 | */ |
||
| 189 | public function getDemonym() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get the capital. |
||
| 196 | * |
||
| 197 | * @return string|null |
||
| 198 | */ |
||
| 199 | public function getCapital() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the ISO 3166-1 alpha2. |
||
| 206 | * |
||
| 207 | * @return string|null |
||
| 208 | */ |
||
| 209 | public function getIsoAlpha2() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get the ISO 3166-1 alpha3. |
||
| 216 | * |
||
| 217 | * @return string|null |
||
| 218 | */ |
||
| 219 | public function getIsoAlpha3() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the ISO 3166-1 numeric. |
||
| 226 | * |
||
| 227 | * @return string|null |
||
| 228 | */ |
||
| 229 | public function getIsoNumeric() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get the given currency or fallback to first currency. |
||
| 236 | * |
||
| 237 | * @param string|null $currency |
||
| 238 | * |
||
| 239 | * @return string|null |
||
| 240 | */ |
||
| 241 | public function getCurrency($currency = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get the currencies. |
||
| 250 | * |
||
| 251 | * @return array|null |
||
| 252 | */ |
||
| 253 | public function getCurrencies() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get the TLD. |
||
| 260 | * |
||
| 261 | * @return string|null |
||
| 262 | */ |
||
| 263 | public function getTld() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the TLDs. |
||
| 270 | * |
||
| 271 | * @return array|null |
||
| 272 | */ |
||
| 273 | public function getTlds() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the alternative spellings. |
||
| 280 | * |
||
| 281 | * @return array|null |
||
| 282 | */ |
||
| 283 | public function getAltSpellings() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get the given language or fallback to first language. |
||
| 290 | * |
||
| 291 | * @param string|null $language |
||
| 292 | * |
||
| 293 | * @return string|null |
||
| 294 | */ |
||
| 295 | public function getLanguage($language = null) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the languages. |
||
| 304 | * |
||
| 305 | * @return array|null |
||
| 306 | */ |
||
| 307 | public function getLanguages() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the translations. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function getTranslations() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get the translation. |
||
| 345 | * |
||
| 346 | * @param string|null $language |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getTranslation($language = null) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the geodata. |
||
| 358 | * |
||
| 359 | * @return array|null |
||
| 360 | */ |
||
| 361 | public function getGeodata() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the continent. |
||
| 368 | * |
||
| 369 | * @return string|null |
||
| 370 | */ |
||
| 371 | public function getContinent() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Determine whether the country uses postal code. |
||
| 378 | * |
||
| 379 | * @return bool|null |
||
| 380 | */ |
||
| 381 | public function usesPostalCode() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the latitude. |
||
| 388 | * |
||
| 389 | * @return string|null |
||
| 390 | */ |
||
| 391 | public function getLatitude() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the longitude. |
||
| 398 | * |
||
| 399 | * @return string|null |
||
| 400 | */ |
||
| 401 | public function getLongitude() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the described latitude. |
||
| 408 | * |
||
| 409 | * @return string|null |
||
| 410 | */ |
||
| 411 | public function getLatitudeDesc() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Get the described longitude. |
||
| 418 | * |
||
| 419 | * @return string|null |
||
| 420 | */ |
||
| 421 | public function getLongitudeDesc() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Get the maximum latitude. |
||
| 428 | * |
||
| 429 | * @return string|null |
||
| 430 | */ |
||
| 431 | public function getMaxLatitude() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Get the maximum longitude. |
||
| 438 | * |
||
| 439 | * @return string|null |
||
| 440 | */ |
||
| 441 | public function getMaxLongitude() |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get the minimum latitude. |
||
| 448 | * |
||
| 449 | * @return string|null |
||
| 450 | */ |
||
| 451 | public function getMinLatitude() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get the minimum longitude. |
||
| 458 | * |
||
| 459 | * @return string|null |
||
| 460 | */ |
||
| 461 | public function getMinLongitude() |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Get the area. |
||
| 468 | * |
||
| 469 | * @return int|null |
||
| 470 | */ |
||
| 471 | public function getArea() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the region. |
||
| 478 | * |
||
| 479 | * @return string|null |
||
| 480 | */ |
||
| 481 | public function getRegion() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the subregion. |
||
| 488 | * |
||
| 489 | * @return string|null |
||
| 490 | */ |
||
| 491 | public function getSubregion() |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get the world region. |
||
| 498 | * |
||
| 499 | * @return string|null |
||
| 500 | */ |
||
| 501 | public function getWorldRegion() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Get the region code. |
||
| 508 | * |
||
| 509 | * @return string|null |
||
| 510 | */ |
||
| 511 | public function getRegionCode() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get the subregion code. |
||
| 518 | * |
||
| 519 | * @return string|null |
||
| 520 | */ |
||
| 521 | public function getSubregionCode() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Check the landlock status. |
||
| 528 | * |
||
| 529 | * @return bool|null |
||
| 530 | */ |
||
| 531 | public function isLandlocked() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Get the borders. |
||
| 538 | * |
||
| 539 | * @return array|null |
||
| 540 | */ |
||
| 541 | public function getBorders() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Determine whether the country is independent. |
||
| 548 | * |
||
| 549 | * @return string|null |
||
| 550 | */ |
||
| 551 | public function isIndependent() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get the given calling code or fallback to first calling code. |
||
| 558 | * |
||
| 559 | * @return string|null |
||
| 560 | */ |
||
| 561 | public function getCallingCode() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Get the calling codes. |
||
| 568 | * |
||
| 569 | * @return array|null |
||
| 570 | */ |
||
| 571 | public function getCallingCodes() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get the national prefix. |
||
| 578 | * |
||
| 579 | * @return string|null |
||
| 580 | */ |
||
| 581 | public function getNationalPrefix() |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Get the national number length. |
||
| 588 | * |
||
| 589 | * @return int|null |
||
| 590 | */ |
||
| 591 | public function getNationalNumberLength() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Get the national number lengths. |
||
| 598 | * |
||
| 599 | * @return array|null |
||
| 600 | */ |
||
| 601 | public function getNationalNumberLengths() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get the national destination code length. |
||
| 608 | * |
||
| 609 | * @return int|null |
||
| 610 | */ |
||
| 611 | public function getNationalDestinationCodeLength() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get the national destination code lengths. |
||
| 618 | * |
||
| 619 | * @return array|null |
||
| 620 | */ |
||
| 621 | public function getnationaldestinationcodelengths() |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Get the international prefix. |
||
| 628 | * |
||
| 629 | * @return string|null |
||
| 630 | */ |
||
| 631 | public function getInternationalPrefix() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the extras. |
||
| 638 | * |
||
| 639 | * @return array|null |
||
| 640 | */ |
||
| 641 | public function getExtra() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get the geonameid. |
||
| 648 | * |
||
| 649 | * @return int|null |
||
| 650 | */ |
||
| 651 | public function getGeonameid() |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get the edgar code. |
||
| 658 | * |
||
| 659 | * @return string|null |
||
| 660 | */ |
||
| 661 | public function getEdgar() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get the itu code. |
||
| 668 | * |
||
| 669 | * @return string|null |
||
| 670 | */ |
||
| 671 | public function getItu() |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Get the marc code. |
||
| 678 | * |
||
| 679 | * @return string|null |
||
| 680 | */ |
||
| 681 | public function getMarc() |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Get the wmo code. |
||
| 688 | * |
||
| 689 | * @return string|null |
||
| 690 | */ |
||
| 691 | public function getWmo() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Get the ds code. |
||
| 698 | * |
||
| 699 | * @return string|null |
||
| 700 | */ |
||
| 701 | public function getDs() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Get the fifa code. |
||
| 708 | * |
||
| 709 | * @return string|null |
||
| 710 | */ |
||
| 711 | public function getFifa() |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Get the fips code. |
||
| 718 | * |
||
| 719 | * @return string|null |
||
| 720 | */ |
||
| 721 | public function getFips() |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Get the gaul code. |
||
| 728 | * |
||
| 729 | * @return int|null |
||
| 730 | */ |
||
| 731 | public function getGaul() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Get the ioc code. |
||
| 738 | * |
||
| 739 | * @return string|null |
||
| 740 | */ |
||
| 741 | public function getIoc() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Get the cowc code. |
||
| 748 | * |
||
| 749 | * @return string|null |
||
| 750 | */ |
||
| 751 | public function getCowc() |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Get the cown code. |
||
| 758 | * |
||
| 759 | * @return int|null |
||
| 760 | */ |
||
| 761 | public function getCown() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Get the fao code. |
||
| 768 | * |
||
| 769 | * @return int|null |
||
| 770 | */ |
||
| 771 | public function getFao() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Get the imf code. |
||
| 778 | * |
||
| 779 | * @return int|null |
||
| 780 | */ |
||
| 781 | public function getImf() |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Get the ar5 code. |
||
| 788 | * |
||
| 789 | * @return string|null |
||
| 790 | */ |
||
| 791 | public function getAr5() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Get the address format. |
||
| 798 | * |
||
| 799 | * @return string|null |
||
| 800 | */ |
||
| 801 | public function getAddressFormat() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Determine whether the country is EU member. |
||
| 808 | * |
||
| 809 | * @return bool|null |
||
| 810 | */ |
||
| 811 | public function isEuMember() |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Get the VAT rates. |
||
| 818 | * |
||
| 819 | * @return array|null |
||
| 820 | */ |
||
| 821 | public function getVatRates() |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Get the emoji. |
||
| 828 | * |
||
| 829 | * @return array|null |
||
| 830 | */ |
||
| 831 | public function getEmoji() |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Get the geographic data structure. |
||
| 838 | * |
||
| 839 | * @return string|null |
||
| 840 | */ |
||
| 841 | public function getGeoJson() |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Get the flag. |
||
| 852 | * |
||
| 853 | * @return string|null |
||
| 854 | */ |
||
| 855 | public function getFlag() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Get the divisions. |
||
| 866 | * |
||
| 867 | * @return array|null |
||
| 868 | */ |
||
| 869 | public function getDivisions() |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Get the divisions. |
||
| 880 | * |
||
| 881 | * @param string $division |
||
| 882 | * |
||
| 883 | * @return array|null |
||
| 884 | */ |
||
| 885 | public function getDivision($division) |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Get the timezones. |
||
| 893 | * |
||
| 894 | * @return array|null |
||
| 895 | */ |
||
| 896 | public function getTimezones() |
||
| 904 | |||
| 905 | /** |
||
| 906 | * Get the locales. |
||
| 907 | * |
||
| 908 | * @return array|null |
||
| 909 | */ |
||
| 910 | public function getLocales() |
||
| 925 | } |
||
| 926 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.