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 |
||
11 | View Code Duplication | class Song extends BaseModel |
|
|
|||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $id; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $title; |
||
23 | |||
24 | /** |
||
25 | * @var SongArtist[] |
||
26 | */ |
||
27 | protected $artist; |
||
28 | |||
29 | /** |
||
30 | * Duration in seconds |
||
31 | * |
||
32 | * @var integer |
||
33 | */ |
||
34 | protected $duration; |
||
35 | |||
36 | /** |
||
37 | * International Standard Recording Code |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $isrc; |
||
42 | |||
43 | public function __construct() |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getId() |
||
55 | |||
56 | /** |
||
57 | * @param string $id |
||
58 | * @return Song |
||
59 | */ |
||
60 | public function setId($id) |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getTitle() |
||
73 | |||
74 | /** |
||
75 | * @param string $title |
||
76 | * @return Song |
||
77 | */ |
||
78 | public function setTitle($title) |
||
83 | |||
84 | /** |
||
85 | * @return ArrayCollection |
||
86 | */ |
||
87 | public function getArtist() |
||
91 | |||
92 | /** |
||
93 | * @param ArrayCollection $artist |
||
94 | * @return Song |
||
95 | */ |
||
96 | public function setArtist($artist) |
||
101 | |||
102 | /** |
||
103 | * @return int |
||
104 | */ |
||
105 | public function getDuration() |
||
109 | |||
110 | /** |
||
111 | * @param int $duration |
||
112 | * @return Song |
||
113 | */ |
||
114 | public function setDuration($duration) |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getIsrc() |
||
127 | |||
128 | /** |
||
129 | * @param string $isrc |
||
130 | * @return Song |
||
131 | */ |
||
132 | public function setIsrc($isrc) |
||
137 | |||
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.