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 |
||
7 | class Person extends PopoloObject |
||
8 | { |
||
9 | use \EveryPolitician\EveryPoliticianPopolo\Traits\ArrayGetterTrait; |
||
10 | |||
11 | protected $properties = [ |
||
12 | 'id', |
||
13 | 'email', |
||
14 | 'gender', |
||
15 | 'honorificPrefix', |
||
16 | 'honorificSuffix', |
||
17 | 'image', |
||
18 | 'name', |
||
19 | 'sortName', |
||
20 | 'nationalIdentity', |
||
21 | 'summary', |
||
22 | 'biography', |
||
23 | 'birthDate', |
||
24 | 'deathDate', |
||
25 | 'familyName', |
||
26 | 'givenName', |
||
27 | 'wikidata', |
||
28 | 'twitter', |
||
29 | 'twitterAll', |
||
30 | 'phone', |
||
31 | 'phoneAll', |
||
32 | 'facebook', |
||
33 | 'facebookAll', |
||
34 | 'fax', |
||
35 | 'faxAll', |
||
36 | |||
37 | 'links', |
||
38 | 'contactDetails', |
||
39 | 'identifiers', |
||
40 | 'images', |
||
41 | 'otherNames', |
||
42 | 'sources', |
||
43 | 'memberships', |
||
44 | ]; |
||
45 | |||
46 | 6 | public function __toString() |
|
50 | |||
51 | 90 | protected function getId() |
|
55 | |||
56 | 3 | protected function getEmail() |
|
60 | |||
61 | 3 | protected function getGender() |
|
65 | |||
66 | 3 | protected function getHonorificPrefix() |
|
70 | |||
71 | 3 | protected function getHonorificSuffix() |
|
75 | |||
76 | 6 | protected function getImage() |
|
80 | |||
81 | 33 | protected function getName() |
|
85 | |||
86 | 3 | protected function getSortName() |
|
90 | |||
91 | 3 | protected function getNationalIdentity() |
|
95 | |||
96 | 3 | protected function getSummary() |
|
100 | |||
101 | 3 | protected function getBiography() |
|
105 | |||
106 | 6 | protected function getBirthDate() |
|
110 | |||
111 | 6 | protected function getDeathDate() |
|
115 | |||
116 | 3 | protected function getFamilyName() |
|
120 | |||
121 | 3 | protected function getGivenName() |
|
125 | |||
126 | 3 | protected function getWikidata() |
|
130 | |||
131 | 9 | protected function getTwitter() |
|
139 | |||
140 | 6 | protected function getTwitterAll() |
|
154 | |||
155 | 3 | protected function getPhone() |
|
159 | |||
160 | 3 | protected function getPhoneAll() |
|
164 | |||
165 | 3 | protected function getFacebook() |
|
169 | |||
170 | 3 | protected function getFacebookAll() |
|
174 | |||
175 | 3 | protected function getFax() |
|
179 | |||
180 | 3 | protected function getFaxAll() |
|
184 | |||
185 | 3 | protected function getLinks() |
|
189 | |||
190 | 3 | protected function getContactDetails() |
|
194 | |||
195 | 3 | protected function getIdentifiers() |
|
199 | |||
200 | 3 | protected function getImages() |
|
204 | |||
205 | 21 | protected function getOtherNames() |
|
209 | |||
210 | 3 | protected function getSources() |
|
214 | |||
215 | 3 | protected function getMemberships() |
|
225 | |||
226 | 9 | private function isNameCurrentAt($name, $dateStr) |
|
232 | |||
233 | 15 | public function nameAt($particularDate) |
|
260 | } |
||
261 |
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@property
annotation 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.