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 |
||
10 | class Customer implements \JsonSerializable |
||
11 | { |
||
12 | const DOCUMENT_TYPE_CPF = "CPF"; |
||
13 | const DOCUMENT_TYPE_CNPJ = "CNPJ"; |
||
14 | |||
15 | /** @var */ |
||
16 | private $customer_id; |
||
17 | |||
18 | /** @var */ |
||
19 | private $first_name; |
||
20 | |||
21 | /** @var */ |
||
22 | private $last_name; |
||
23 | |||
24 | /** @var */ |
||
25 | private $name; |
||
26 | |||
27 | /** @var */ |
||
28 | private $email; |
||
29 | |||
30 | /** @var */ |
||
31 | private $document_type; |
||
32 | |||
33 | /** @var */ |
||
34 | private $document_number; |
||
35 | |||
36 | /** @var */ |
||
37 | private $phone_number; |
||
38 | |||
39 | /** @var */ |
||
40 | private $billing_address; |
||
41 | |||
42 | /** |
||
43 | * Customer constructor. |
||
44 | * @param $customer_id |
||
45 | */ |
||
46 | public function __construct($customer_id) |
||
50 | |||
51 | /** |
||
52 | * @return array|mixed |
||
53 | */ |
||
54 | View Code Duplication | public function jsonSerialize() |
|
64 | |||
65 | /** |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function getCustomerId() |
||
72 | |||
73 | /** |
||
74 | * @param $customer_id |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function setCustomerId($customer_id) |
||
83 | |||
84 | /** |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function getFirstName() |
||
91 | |||
92 | /** |
||
93 | * @param $first_name |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function setFirstName($first_name) |
||
102 | |||
103 | /** |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function getLastName() |
||
110 | |||
111 | /** |
||
112 | * @param $last_name |
||
113 | * @return $this |
||
114 | */ |
||
115 | public function setLastName($last_name) |
||
121 | |||
122 | /** |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function getName() |
||
129 | |||
130 | /** |
||
131 | * @param $name |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function setName($name) |
||
140 | |||
141 | /** |
||
142 | * @return mixed |
||
143 | */ |
||
144 | public function getEmail() |
||
148 | |||
149 | /** |
||
150 | * @param $email |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setEmail($email) |
||
159 | |||
160 | /** |
||
161 | * @return mixed |
||
162 | */ |
||
163 | public function getDocumentType() |
||
167 | |||
168 | /** |
||
169 | * @param $document_type |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function setDocumentType($document_type) |
||
178 | |||
179 | /** |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function getDocumentNumber() |
||
186 | |||
187 | /** |
||
188 | * @param $document_number |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function setDocumentNumber($document_number) |
||
197 | |||
198 | /** |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function getPhoneNumber() |
||
205 | |||
206 | /** |
||
207 | * @param $phone_number |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setPhoneNumber($phone_number) |
||
216 | |||
217 | /** |
||
218 | * @return Address |
||
219 | */ |
||
220 | public function billingAddress() |
||
228 | |||
229 | /** |
||
230 | * @return mixed |
||
231 | */ |
||
232 | public function getBillingAddress() |
||
236 | |||
237 | /** |
||
238 | * @param $billing_address |
||
239 | * @return $this |
||
240 | */ |
||
241 | public function setBillingAddress($billing_address) |
||
247 | } |
||
248 |
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.