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 |
||
15 | View Code Duplication | class Credential extends DataObject |
|
|
|||
16 | { |
||
17 | /** @var int */ |
||
18 | private $user; |
||
19 | /** @var int */ |
||
20 | private $factor; |
||
21 | /** @var string */ |
||
22 | private $type; |
||
23 | /** @var string */ |
||
24 | private $data; |
||
25 | /** @var int */ |
||
26 | private $version; |
||
27 | |||
28 | /** |
||
29 | * @return int |
||
30 | */ |
||
31 | public function getUserId() |
||
35 | |||
36 | /** |
||
37 | * @param int $user |
||
38 | */ |
||
39 | public function setUserId($user) |
||
43 | |||
44 | /** |
||
45 | * @return int |
||
46 | */ |
||
47 | public function getFactor() |
||
51 | |||
52 | /** |
||
53 | * @param int $factor |
||
54 | */ |
||
55 | public function setFactor($factor) |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getType() |
||
67 | |||
68 | /** |
||
69 | * @param string $type |
||
70 | */ |
||
71 | public function setType($type) |
||
75 | |||
76 | /** |
||
77 | * @return string |
||
78 | */ |
||
79 | public function getData() |
||
83 | |||
84 | /** |
||
85 | * @param string $data |
||
86 | */ |
||
87 | public function setData($data) |
||
91 | |||
92 | /** |
||
93 | * @return int |
||
94 | */ |
||
95 | public function getVersion() |
||
99 | |||
100 | /** |
||
101 | * @param int $version |
||
102 | */ |
||
103 | public function setVersion($version) |
||
107 | |||
108 | public function save() |
||
160 | } |
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.