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 Item extends Model |
||
6 | { |
||
7 | public $description; |
||
8 | public $quantity; |
||
9 | public $price; |
||
10 | public $weight; |
||
11 | public $item_id; |
||
12 | public $origin_country; |
||
13 | public $sku; |
||
14 | public $hs_code; |
||
15 | |||
16 | /** |
||
17 | * @param string $value |
||
18 | * |
||
19 | * @return $this |
||
20 | */ |
||
21 | public function description($value) |
||
27 | |||
28 | /** |
||
29 | * @param int $value |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | public function quantity($value) |
||
39 | |||
40 | /** |
||
41 | * @param Money $price |
||
42 | * |
||
43 | * @return $this |
||
44 | */ |
||
45 | public function price(Money $price) |
||
51 | |||
52 | /** |
||
53 | * @param Weight $weight |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function weight(Weight $weight) |
||
63 | |||
64 | /** |
||
65 | * @param string $value |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function item_id($value) |
||
75 | |||
76 | /** |
||
77 | * @param $value |
||
78 | * |
||
79 | * @throws \Exception |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | View Code Duplication | public function origin_country($value) |
|
94 | |||
95 | /** |
||
96 | * @param $value |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function sku($value) |
||
106 | |||
107 | /** |
||
108 | * @param $value |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function hs_code($value) |
||
118 | } |
||
119 |
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.