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:
1 | <?php |
||
23 | View Code Duplication | trait CountTrait |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * Answer count. |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $answerCount; |
||
31 | |||
32 | /** |
||
33 | * The total Badges, segregated by rank. |
||
34 | * |
||
35 | * @var \BenatEspina\StackExchangeApiClient\Model\Interfaces\BadgeCountInterface |
||
36 | */ |
||
37 | protected $badgeCount; |
||
38 | |||
39 | /** |
||
40 | * Number of questions. |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $questionCount; |
||
45 | |||
46 | /** |
||
47 | * Sets number of answers. |
||
48 | * |
||
49 | * @param int $answerCount The number of answers |
||
50 | * |
||
51 | * @return $this self Object |
||
52 | */ |
||
53 | public function setAnswerCount($answerCount) |
||
59 | |||
60 | /** |
||
61 | * Gets number of answers. |
||
62 | * |
||
63 | * @return int |
||
64 | */ |
||
65 | public function getAnswerCount() |
||
69 | |||
70 | /** |
||
71 | * Sets badge count. |
||
72 | * |
||
73 | * @param \BenatEspina\StackExchangeApiClient\Model\Interfaces\BadgeCountInterface $badgeCount The badge count |
||
74 | * |
||
75 | * @return $this self Object |
||
76 | */ |
||
77 | public function setBadgeCount(BadgeCountInterface $badgeCount) |
||
83 | |||
84 | /** |
||
85 | * Gets badge count. |
||
86 | * |
||
87 | * @return \BenatEspina\StackExchangeApiClient\Model\Interfaces\BadgeCountInterface |
||
88 | */ |
||
89 | public function getBadgeCount() |
||
93 | |||
94 | /** |
||
95 | * Sets question id. |
||
96 | * |
||
97 | * @param int $questionCount The question id |
||
98 | * |
||
99 | * @return $this self Object |
||
100 | */ |
||
101 | public function setQuestionCount($questionCount) |
||
107 | |||
108 | /** |
||
109 | * Gets question id. |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | public function getQuestionCount() |
||
117 | |||
118 | /** |
||
119 | * Loads the variables if the data exist into resource. It works like a constructor. |
||
120 | * |
||
121 | * @param null|mixed[] $resource The resource |
||
122 | */ |
||
123 | protected function loadCount($resource) |
||
129 | } |
||
130 |
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.