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 SongArtist extends BaseModel |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var Song |
||
20 | */ |
||
21 | protected $song; |
||
22 | |||
23 | /** |
||
24 | * @var Artist |
||
25 | */ |
||
26 | protected $artist; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $role; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $joinphrase; |
||
37 | |||
38 | /** |
||
39 | * @var integer |
||
40 | */ |
||
41 | protected $order; |
||
42 | |||
43 | /** |
||
44 | * @return Song |
||
45 | */ |
||
46 | public function getSong() |
||
50 | |||
51 | /** |
||
52 | * @param Song $song |
||
53 | * @return SongArtist |
||
54 | */ |
||
55 | public function setSong($song) |
||
60 | |||
61 | /** |
||
62 | * @return Artist |
||
63 | */ |
||
64 | public function getArtist() |
||
68 | |||
69 | /** |
||
70 | * @param Artist $artist |
||
71 | * @return SongArtist |
||
72 | */ |
||
73 | public function setArtist($artist) |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getRole() |
||
86 | |||
87 | /** |
||
88 | * @param string $role |
||
89 | * @return SongArtist |
||
90 | */ |
||
91 | public function setRole($role) |
||
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getJoinphrase() |
||
104 | |||
105 | /** |
||
106 | * @param string $joinphrase |
||
107 | * @return SongArtist |
||
108 | */ |
||
109 | public function setJoinphrase($joinphrase) |
||
114 | |||
115 | /** |
||
116 | * @return int |
||
117 | */ |
||
118 | public function getOrder() |
||
122 | |||
123 | /** |
||
124 | * @param int $order |
||
125 | * @return SongArtist |
||
126 | */ |
||
127 | public function setOrder($order) |
||
132 | |||
133 | |||
134 | } |
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.