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 |
||
11 | class ContactUs |
||
12 | { |
||
13 | private $contact_uuid; |
||
14 | private $contact_id; |
||
15 | private $name; |
||
16 | private $email; |
||
17 | private $phone; |
||
18 | private $subject; |
||
19 | private $body; |
||
20 | private $created_at; |
||
21 | |||
22 | /** |
||
23 | * @return mixed |
||
24 | */ |
||
25 | public function getContactUuid() |
||
29 | |||
30 | /** |
||
31 | * @param $uuid |
||
32 | * |
||
33 | * @return \ContactUs\Entity\ContactUs |
||
34 | */ |
||
35 | public function setContactUuid($uuid) |
||
40 | |||
41 | /** |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function getContactId() |
||
48 | |||
49 | /** |
||
50 | * @param $id |
||
51 | * |
||
52 | * @return \ContactUs\Entity\ContactUs |
||
53 | */ |
||
54 | public function setContactId($id) |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getName() |
||
67 | |||
68 | /** |
||
69 | * @param $name |
||
70 | * |
||
71 | * @return \ContactUs\Entity\ContactUs |
||
72 | */ |
||
73 | public function setName($name) |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getEmail() |
||
86 | |||
87 | /** |
||
88 | * @param $email |
||
89 | * |
||
90 | * @return \ContactUs\Entity\ContactUs |
||
91 | */ |
||
92 | public function setEmail($email) |
||
97 | |||
98 | /** |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function getPhone() |
||
105 | |||
106 | /** |
||
107 | * @param $phone |
||
108 | * |
||
109 | * @return \ContactUs\Entity\ContactUs |
||
110 | */ |
||
111 | public function setPhone($phone) |
||
116 | |||
117 | /** |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function getSubject() |
||
124 | |||
125 | /** |
||
126 | * @param $subject |
||
127 | * |
||
128 | * @return \ContactUs\Entity\ContactUs |
||
129 | */ |
||
130 | public function setSubject($subject) |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getBody() |
||
143 | |||
144 | /** |
||
145 | * @param $body |
||
146 | * |
||
147 | * @return \ContactUs\Entity\ContactUs |
||
148 | */ |
||
149 | public function setBody($body) |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getCreatedAt() |
||
162 | |||
163 | /** |
||
164 | * @param string $createdAt |
||
165 | * |
||
166 | * @return \ContactUs\Entity\ContactUs |
||
167 | */ |
||
168 | public function setCreatedAt($createdAt) |
||
172 | |||
173 | /** |
||
174 | * Hydration of object. |
||
175 | * |
||
176 | * @param array $data |
||
177 | */ |
||
178 | View Code Duplication | public function exchangeArray($data = []) |
|
189 | |||
190 | /** |
||
191 | * Dehydrate/Extract object to array. |
||
192 | * |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getArrayCopy() |
||
208 | } |
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.