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 TchatBotIdentity 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 TchatBotIdentity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class TchatBotIdentity |
||
14 | { |
||
15 | |||
16 | private static $logger = NULL; |
||
17 | |||
18 | private $id; |
||
19 | |||
20 | private $lang; |
||
21 | |||
22 | private $name; |
||
23 | |||
24 | private $pseudo; |
||
25 | |||
26 | private $avatar; |
||
27 | |||
28 | private $conceptorName; |
||
29 | |||
30 | /** |
||
31 | * |
||
32 | * @var \DateTime |
||
33 | */ |
||
34 | private $birthday; |
||
35 | |||
36 | /** |
||
37 | * |
||
38 | * @var \DateTimeZone |
||
39 | */ |
||
40 | private $timezone; |
||
41 | |||
42 | private $charset; |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | * @see \Posibrain\OutputDecorationEnum |
||
47 | */ |
||
48 | private $outputDecoration; |
||
49 | |||
50 | private $triggers; |
||
51 | |||
52 | private $positrons; |
||
53 | |||
54 | private $instinctPath; |
||
55 | private $memoryPath; |
||
56 | |||
57 | public function __construct($id = '', $lang = '', $params = array()) |
||
133 | |||
134 | public function getIdentityPath() |
||
138 | |||
139 | private function loadIdentity($identityPath) |
||
179 | |||
180 | public function getSynonymsPath() |
||
184 | |||
185 | public function getKnowledgePath() |
||
189 | |||
190 | public function getComputedKnowledgePath() |
||
194 | |||
195 | public function getNoKnowledgePath() |
||
199 | |||
200 | /* Getter / Setter */ |
||
201 | public function getId() |
||
205 | |||
206 | public function setId($id) |
||
210 | |||
211 | public function getLang() |
||
215 | |||
216 | public function setLang($lang) |
||
220 | |||
221 | public function getInstinctPath() |
||
225 | |||
226 | public function setInstinctPath($instinctPath) |
||
230 | |||
231 | public function getCharset() |
||
235 | |||
236 | public function setCharset($charset) |
||
245 | |||
246 | public function getName() |
||
250 | |||
251 | public function setName($name) |
||
256 | |||
257 | public function getPseudo() |
||
261 | |||
262 | public function setPseudo($pseudo) |
||
267 | |||
268 | public function getAvatar() |
||
272 | |||
273 | public function setAvatar($avatar) |
||
278 | |||
279 | public function getConceptorName() |
||
283 | |||
284 | public function setConceptorName($conceptorName) |
||
289 | |||
290 | /** |
||
291 | * |
||
292 | * @return DateTime |
||
293 | */ |
||
294 | public function getBirthday() |
||
298 | |||
299 | public function setBirthday($birthday) |
||
307 | |||
308 | /** |
||
309 | * |
||
310 | * @return DateTimeZone |
||
311 | */ |
||
312 | public function getTimezone() |
||
316 | |||
317 | public function setTimezone($timezone) |
||
329 | |||
330 | public function getOutputDecoration() |
||
334 | |||
335 | public function setOutputDecoration($outputDecoration) |
||
340 | |||
341 | public function getTriggers() |
||
345 | |||
346 | public function setTriggers($triggers) |
||
351 | |||
352 | public function getPositrons() |
||
356 | |||
357 | public function setPositrons($positrons) |
||
362 | |||
363 | public function getMemoryPath() |
||
367 | |||
368 | public function setMemoryPath($memoryPath) |
||
373 | |||
374 | } |
||
375 | |||
378 |
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.