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 |
||
5 | class Address extends Model |
||
6 | { |
||
7 | public $country; |
||
8 | public $contact_name; |
||
9 | public $phone; |
||
10 | public $fax; |
||
11 | public $email; |
||
12 | public $company_name; |
||
13 | public $street1; |
||
14 | public $street2; |
||
15 | public $street3; |
||
16 | public $city; |
||
17 | public $state; |
||
18 | public $postal_code; |
||
19 | public $type = 'residential'; |
||
20 | public $tax_id; |
||
21 | |||
22 | /** |
||
23 | * @param string $value |
||
24 | * |
||
25 | * @throws \Exception |
||
26 | * |
||
27 | * @return $this |
||
28 | */ |
||
29 | View Code Duplication | public function country($value) |
|
40 | |||
41 | /** |
||
42 | * @param string $value |
||
43 | * |
||
44 | * @return $this |
||
45 | */ |
||
46 | public function contact_name($value) |
||
52 | |||
53 | /** |
||
54 | * @param string $value |
||
55 | * |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function phone($value) |
||
64 | |||
65 | /** |
||
66 | * @param string $value |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function fax($value) |
||
76 | |||
77 | /** |
||
78 | * @param string $value |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function email($value) |
||
88 | |||
89 | /** |
||
90 | * @param string $value |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function company_name($value) |
||
100 | |||
101 | /** |
||
102 | * @param string $value |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | public function street1($value) |
||
112 | |||
113 | /** |
||
114 | * @param string $value |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function street2($value) |
||
128 | |||
129 | /** |
||
130 | * @param string $value |
||
131 | * |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function street3($value) |
||
144 | |||
145 | /** |
||
146 | * @param string $value |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function city($value) |
||
156 | |||
157 | /** |
||
158 | * @param string $value |
||
159 | * |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function state($value) |
||
168 | |||
169 | /** |
||
170 | * @param string $value |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function postal_code($value) |
||
180 | |||
181 | /** |
||
182 | * @param string $value |
||
183 | * |
||
184 | * @throws \Exception |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function type($value) |
||
198 | |||
199 | /** |
||
200 | * @param string $value |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function tax_id($value) |
||
210 | } |
||
211 |
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.