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 |
||
8 | class Entry |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | protected $value; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $color = null; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $label; |
||
24 | |||
25 | 1 | public function __construct() |
|
30 | |||
31 | /** |
||
32 | * @param $value |
||
33 | * |
||
34 | * @return $this |
||
35 | */ |
||
36 | 1 | public function setValue($value) |
|
42 | |||
43 | /** |
||
44 | * @param $label |
||
45 | * |
||
46 | * @return $this |
||
47 | */ |
||
48 | 1 | public function setLabel($label) |
|
54 | |||
55 | /** |
||
56 | * @return string |
||
57 | */ |
||
58 | 1 | public function getLabel() |
|
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | 1 | public function getValue() |
|
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | 1 | public function getColor() |
|
78 | |||
79 | /** |
||
80 | * @param string $color |
||
81 | */ |
||
82 | 1 | public function setColor($color) |
|
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | 1 | View Code Duplication | public function toArray() |
111 | } |
||
112 |
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.