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 |
||
16 | View Code Duplication | class Link extends Base |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $id; |
||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $articleId; |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $name; |
||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $link; |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $target; |
||
38 | |||
39 | /** |
||
40 | * @return int |
||
41 | */ |
||
42 | public function getId() |
||
46 | |||
47 | /** |
||
48 | * @param int $id |
||
49 | * |
||
50 | * @return Link |
||
51 | */ |
||
52 | public function setId($id) |
||
58 | |||
59 | /** |
||
60 | * @return int |
||
61 | */ |
||
62 | public function getArticleId() |
||
66 | |||
67 | /** |
||
68 | * @param int $articleId |
||
69 | * |
||
70 | * @return Link |
||
71 | */ |
||
72 | public function setArticleId($articleId) |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getName() |
||
86 | |||
87 | /** |
||
88 | * @param string $name |
||
89 | * |
||
90 | * @return Link |
||
91 | */ |
||
92 | public function setName($name) |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getLink() |
||
106 | |||
107 | /** |
||
108 | * @param string $link |
||
109 | * |
||
110 | * @return Link |
||
111 | */ |
||
112 | public function setLink($link) |
||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getTarget() |
||
126 | |||
127 | /** |
||
128 | * @param string $target |
||
129 | * |
||
130 | * @return Link |
||
131 | */ |
||
132 | public function setTarget($target) |
||
138 | } |
||
139 |
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.