Complex classes like Contact 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 Contact, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Contact extends Model |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var integer |
||
16 | */ |
||
17 | protected $id; |
||
18 | |||
19 | /** |
||
20 | * @var integer |
||
21 | */ |
||
22 | protected $projectId; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $name; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $profession; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $site; |
||
38 | |||
39 | /** |
||
40 | * @var ContactDetail[] |
||
41 | */ |
||
42 | protected $details = []; |
||
43 | |||
44 | /** |
||
45 | * @var ContactField[] |
||
46 | */ |
||
47 | protected $fields = []; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Contact constructor. |
||
52 | * |
||
53 | * @param array $contactData |
||
54 | */ |
||
55 | public function __construct(array $contactData = []) |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @return bool |
||
90 | * @throws LPTrackerSDKException |
||
91 | */ |
||
92 | public function validate() |
||
108 | |||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function toArray() |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @return int |
||
144 | */ |
||
145 | public function getId() |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @return int |
||
153 | */ |
||
154 | public function getProjectId() |
||
158 | |||
159 | |||
160 | /** |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getName() |
||
167 | |||
168 | |||
169 | /** |
||
170 | * @param string $name |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function setName($name) |
||
180 | |||
181 | |||
182 | /** |
||
183 | * @return string |
||
184 | */ |
||
185 | public function getProfession() |
||
189 | |||
190 | |||
191 | /** |
||
192 | * @param string $profession |
||
193 | * |
||
194 | * @return $this |
||
195 | */ |
||
196 | public function setProfession($profession) |
||
202 | |||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getSite() |
||
211 | |||
212 | |||
213 | /** |
||
214 | * @param string $site |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setSite($site) |
||
224 | |||
225 | |||
226 | /** |
||
227 | * @return ContactDetail[] |
||
228 | */ |
||
229 | public function getDetails() |
||
233 | |||
234 | |||
235 | /** |
||
236 | * @param ContactDetail[] $details |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function setDetails(array $details) |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @param ContactDetail $detail |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function addDetail(ContactDetail $detail) |
||
265 | |||
266 | |||
267 | /** |
||
268 | * @return ContactField[] |
||
269 | */ |
||
270 | public function getFields() |
||
274 | |||
275 | |||
276 | /** |
||
277 | * @param ContactField[] $fields |
||
278 | * |
||
279 | * @return $this |
||
280 | */ |
||
281 | public function setFields(array $fields) |
||
292 | |||
293 | |||
294 | /** |
||
295 | * @param ContactField $field |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | public function addField(ContactField $field) |
||
306 | } |