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 Download extends Base |
|
|
|||
16 | { |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $id; |
||
21 | /** |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $articleId; |
||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $name; |
||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $file; |
||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $size; |
||
37 | |||
38 | /** |
||
39 | * @return int |
||
40 | */ |
||
41 | public function getId() |
||
45 | |||
46 | /** |
||
47 | * @param int $id |
||
48 | * |
||
49 | * @return Download |
||
50 | */ |
||
51 | public function setId($id) |
||
57 | |||
58 | /** |
||
59 | * @return int |
||
60 | */ |
||
61 | public function getArticleId() |
||
65 | |||
66 | /** |
||
67 | * @param int $articleId |
||
68 | * |
||
69 | * @return Download |
||
70 | */ |
||
71 | public function setArticleId($articleId) |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getName() |
||
85 | |||
86 | /** |
||
87 | * @param string $name |
||
88 | * |
||
89 | * @return Download |
||
90 | */ |
||
91 | public function setName($name) |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getFile() |
||
105 | |||
106 | /** |
||
107 | * @param string $file |
||
108 | * |
||
109 | * @return Download |
||
110 | */ |
||
111 | public function setFile($file) |
||
117 | |||
118 | /** |
||
119 | * @return int |
||
120 | */ |
||
121 | public function getSize() |
||
125 | |||
126 | /** |
||
127 | * @param int $size |
||
128 | * |
||
129 | * @return Download |
||
130 | */ |
||
131 | public function setSize($size) |
||
137 | } |
||
138 |
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.