Complex classes like Person 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 Person, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Person extends PopoloObject |
||
| 9 | { |
||
| 10 | use \EveryPolitician\EveryPoliticianPopolo\Traits\ArrayGetterTrait; |
||
| 11 | |||
| 12 | protected $properties = [ |
||
| 13 | 'id', |
||
| 14 | 'email', |
||
| 15 | 'gender', |
||
| 16 | 'honorificPrefix', |
||
| 17 | 'honorificSuffix', |
||
| 18 | 'image', |
||
| 19 | 'name', |
||
| 20 | 'sortName', |
||
| 21 | 'nationalIdentity', |
||
| 22 | 'summary', |
||
| 23 | 'biography', |
||
| 24 | 'birthDate', |
||
| 25 | 'deathDate', |
||
| 26 | 'familyName', |
||
| 27 | 'givenName', |
||
| 28 | 'wikidata', |
||
| 29 | 'twitter', |
||
| 30 | 'twitterAll', |
||
| 31 | 'phone', |
||
| 32 | 'phoneAll', |
||
| 33 | 'facebook', |
||
| 34 | 'facebookAll', |
||
| 35 | 'fax', |
||
| 36 | 'faxAll', |
||
| 37 | |||
| 38 | 'links', |
||
| 39 | 'contactDetails', |
||
| 40 | 'identifiers', |
||
| 41 | 'images', |
||
| 42 | 'otherNames', |
||
| 43 | 'sources', |
||
| 44 | 'memberships', |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * String representation of {@link Person} |
||
| 49 | * |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | 6 | public function __toString() |
|
| 56 | |||
| 57 | 90 | protected function getId() |
|
| 61 | |||
| 62 | 3 | protected function getEmail() |
|
| 66 | |||
| 67 | 3 | protected function getGender() |
|
| 71 | |||
| 72 | 3 | protected function getHonorificPrefix() |
|
| 76 | |||
| 77 | 3 | protected function getHonorificSuffix() |
|
| 81 | |||
| 82 | 6 | protected function getImage() |
|
| 86 | |||
| 87 | 33 | protected function getName() |
|
| 91 | |||
| 92 | 3 | protected function getSortName() |
|
| 96 | |||
| 97 | 3 | protected function getNationalIdentity() |
|
| 101 | |||
| 102 | 3 | protected function getSummary() |
|
| 106 | |||
| 107 | 3 | protected function getBiography() |
|
| 111 | |||
| 112 | 6 | protected function getBirthDate() |
|
| 116 | |||
| 117 | 6 | protected function getDeathDate() |
|
| 121 | |||
| 122 | 3 | protected function getFamilyName() |
|
| 126 | |||
| 127 | 3 | protected function getGivenName() |
|
| 131 | |||
| 132 | 3 | protected function getWikidata() |
|
| 136 | |||
| 137 | 9 | protected function getTwitter() |
|
| 145 | |||
| 146 | 6 | protected function getTwitterAll() |
|
| 160 | |||
| 161 | 3 | protected function getPhone() |
|
| 165 | |||
| 166 | 3 | protected function getPhoneAll() |
|
| 170 | |||
| 171 | 3 | protected function getFacebook() |
|
| 175 | |||
| 176 | 3 | protected function getFacebookAll() |
|
| 180 | |||
| 181 | 3 | protected function getFax() |
|
| 185 | |||
| 186 | 3 | protected function getFaxAll() |
|
| 190 | |||
| 191 | 3 | protected function getLinks() |
|
| 195 | |||
| 196 | 3 | protected function getContactDetails() |
|
| 200 | |||
| 201 | 3 | protected function getIdentifiers() |
|
| 205 | |||
| 206 | 3 | protected function getImages() |
|
| 210 | |||
| 211 | 21 | protected function getOtherNames() |
|
| 215 | |||
| 216 | 3 | protected function getSources() |
|
| 220 | |||
| 221 | 3 | protected function getMemberships() |
|
| 231 | |||
| 232 | 9 | private function isNameCurrentAt($name, $dateStr) |
|
| 238 | |||
| 239 | 15 | public function nameAt($particularDate) |
|
| 266 | } |
||
| 267 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.