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 |
||
21 | View Code Duplication | trait GenericIdTrait |
|
|
|||
22 | { |
||
23 | /** |
||
24 | * The answer id. |
||
25 | * |
||
26 | * @var int|null |
||
27 | */ |
||
28 | protected $answerId; |
||
29 | |||
30 | /** |
||
31 | * The body. |
||
32 | * |
||
33 | * @var string|null |
||
34 | */ |
||
35 | protected $body; |
||
36 | |||
37 | /** |
||
38 | * The question id. |
||
39 | * |
||
40 | * @var int|null |
||
41 | */ |
||
42 | protected $questionId; |
||
43 | |||
44 | /** |
||
45 | * Sets the answer id. |
||
46 | * |
||
47 | * @param int $answerId The answer id |
||
48 | * |
||
49 | * @return $this self Object |
||
50 | */ |
||
51 | public function setAnswerId($answerId) |
||
57 | |||
58 | /** |
||
59 | * Gets the answer id. |
||
60 | * |
||
61 | * @return int |
||
62 | */ |
||
63 | public function getAnswerId() |
||
67 | |||
68 | /** |
||
69 | * Sets body. |
||
70 | * |
||
71 | * @param string|null $body The body |
||
72 | * |
||
73 | * @return $this self Object |
||
74 | */ |
||
75 | public function setBody($body) |
||
81 | |||
82 | /** |
||
83 | * Gets body. |
||
84 | * |
||
85 | * @return string|null |
||
86 | */ |
||
87 | public function getBody() |
||
91 | |||
92 | /** |
||
93 | * Sets the question id. |
||
94 | * |
||
95 | * @param int|null $questionId The question id |
||
96 | * |
||
97 | * @return $this self Object |
||
98 | */ |
||
99 | public function setQuestionId($questionId) |
||
105 | |||
106 | /** |
||
107 | * Gets the question id. |
||
108 | * |
||
109 | * @return int|null |
||
110 | */ |
||
111 | public function getQuestionId() |
||
115 | |||
116 | /** |
||
117 | * Loads the variables if the data exist into resource. It works like a constructor. |
||
118 | * |
||
119 | * @param null|mixed[] $resource The resource |
||
120 | */ |
||
121 | protected function loadGenericId($resource) |
||
127 | } |
||
128 |
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.