Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
7 | class Contact extends Model |
||
8 | { |
||
9 | /** |
||
10 | * @var integer |
||
11 | */ |
||
12 | protected $id; |
||
13 | |||
14 | /** |
||
15 | * @var integer |
||
16 | */ |
||
17 | protected $projectId; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $name; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $profession; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $site; |
||
33 | |||
34 | /** |
||
35 | * @var ContactDetail[] |
||
36 | */ |
||
37 | protected $details = []; |
||
38 | |||
39 | /** |
||
40 | * @var ContactField[] |
||
41 | */ |
||
42 | protected $fields = []; |
||
43 | |||
44 | public function __construct(array $contactData = []) |
||
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | * @throws LPTrackerSDKException |
||
80 | */ |
||
81 | public function validate() |
||
98 | |||
99 | /** |
||
100 | * @return array |
||
101 | */ |
||
102 | public function toArray() |
||
128 | |||
129 | /** |
||
130 | * @return int |
||
131 | */ |
||
132 | public function getId() |
||
136 | |||
137 | /** |
||
138 | * @return int |
||
139 | */ |
||
140 | public function getProjectId() |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getName() |
||
152 | |||
153 | /** |
||
154 | * @param string $name |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function setName($name) |
||
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getProfession() |
||
170 | |||
171 | /** |
||
172 | * @param string $profession |
||
173 | * @return $this |
||
174 | */ |
||
175 | public function setProfession($profession) |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getSite() |
||
188 | |||
189 | /** |
||
190 | * @param string $site |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function setSite($site) |
||
198 | |||
199 | /** |
||
200 | * @return ContactDetail[] |
||
201 | */ |
||
202 | public function getDetails() |
||
206 | |||
207 | /** |
||
208 | * @param ContactDetail[] $details |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function setDetails(array $details) |
||
219 | |||
220 | /** |
||
221 | * @param ContactDetail $detail |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function addDetail(ContactDetail $detail) |
||
230 | |||
231 | /** |
||
232 | * @return ContactField[] |
||
233 | */ |
||
234 | public function getFields() |
||
238 | |||
239 | /** |
||
240 | * @param ContactField[] $fields |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function setFields(array $fields) |
||
251 | |||
252 | /** |
||
253 | * @param ContactField $field |
||
254 | * @return $this |
||
255 | */ |
||
256 | public function addField(ContactField $field) |
||
262 | } |
||
263 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.