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 |
||
18 | class TrackMapper extends Mapper { |
||
19 | |||
20 | public function __construct(IDb $db){ |
||
23 | |||
24 | /** |
||
25 | * @param string $condition |
||
26 | */ |
||
27 | private function makeSelectQueryWithoutUserId($condition){ |
||
35 | |||
36 | /** |
||
37 | * @param string $condition |
||
38 | */ |
||
39 | private function makeSelectQuery($condition=null){ |
||
42 | |||
43 | /** |
||
44 | * @param string $userId |
||
45 | * @param integer $limit |
||
46 | * @param integer $offset |
||
47 | * @return Track[] |
||
48 | */ |
||
49 | public function findAll($userId, $limit=null, $offset=null){ |
||
54 | |||
55 | /** |
||
56 | * @param integer $artistId |
||
57 | * @param string $userId |
||
58 | * @return Track[] |
||
59 | */ |
||
60 | public function findAllByArtist($artistId, $userId){ |
||
66 | |||
67 | /** |
||
68 | * @param integer $albumId |
||
69 | * @param string $userId |
||
70 | * @param integer $artistId |
||
71 | * @return Track[] |
||
72 | */ |
||
73 | public function findAllByAlbum($albumId, $userId, $artistId = null){ |
||
83 | |||
84 | /** |
||
85 | * @param integer $id |
||
86 | * @param string $userId |
||
87 | * @return Track |
||
88 | */ |
||
89 | public function find($id, $userId){ |
||
94 | |||
95 | /** |
||
96 | * @param integer $fileId |
||
97 | * @param string $userId |
||
98 | * @return Track |
||
99 | */ |
||
100 | public function findByFileId($fileId, $userId){ |
||
105 | |||
106 | /** |
||
107 | * @param integer $fileId |
||
108 | * @return Track[] |
||
109 | */ |
||
110 | public function findAllByFileId($fileId){ |
||
116 | |||
117 | /** |
||
118 | * @param integer $artistId |
||
119 | * @param string $userId |
||
120 | * @return integer |
||
121 | */ |
||
122 | View Code Duplication | public function countByArtist($artistId, $userId){ |
|
130 | |||
131 | /** |
||
132 | * @param integer $albumId |
||
133 | * @param string $userId |
||
134 | * @return integer |
||
135 | */ |
||
136 | View Code Duplication | public function countByAlbum($albumId, $userId){ |
|
144 | |||
145 | /** |
||
146 | * @param string $userId |
||
147 | * @return integer |
||
148 | */ |
||
149 | View Code Duplication | public function count($userId){ |
|
157 | |||
158 | /** |
||
159 | * @param string $name |
||
160 | * @param string $userId |
||
161 | * @param bool $fuzzy |
||
162 | * @return Track[] |
||
163 | */ |
||
164 | View Code Duplication | public function findAllByName($name, $userId, $fuzzy = false){ |
|
175 | |||
176 | /** |
||
177 | * @param string $name |
||
178 | * @param string $userId |
||
179 | * @return Track[] |
||
180 | */ |
||
181 | public function findAllByNameRecursive($name, $userId){ |
||
190 | |||
191 | /** |
||
192 | * @param string $userId |
||
193 | * @return string[] |
||
194 | */ |
||
195 | View Code Duplication | public function lastChange($userId){ |
|
203 | } |
||
204 |
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.