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 AlbumMapper extends BaseMapper { |
||
19 | |||
20 | public function __construct(IDBConnection $db){ |
||
21 | parent::__construct($db, 'music_albums', '\OCA\Music\Db\Album'); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * @param string $condition |
||
26 | * @return string |
||
27 | */ |
||
28 | private function makeSelectQuery($condition=null){ |
||
29 | return 'SELECT `album`.`name`, `album`.`year`, `album`.`disk`, `album`.`id`, '. |
||
30 | '`album`.`cover_file_id`, `album`.`mbid`, `album`.`disk`, '. |
||
31 | '`album`.`mbid_group`, `album`.`mbid_group`, '. |
||
32 | '`album`.`album_artist_id` FROM `*PREFIX*music_albums` `album`'. |
||
33 | 'WHERE `album`.`user_id` = ? ' . $condition; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * returns all albums of a user |
||
38 | * |
||
39 | * @param string $userId the user ID |
||
40 | * @return Album[] |
||
41 | */ |
||
42 | public function findAll($userId){ |
||
47 | |||
48 | /** |
||
49 | * finds an album by ID |
||
50 | * |
||
51 | * @param integer $albumId ID of the album |
||
52 | * @param string $userId the user ID |
||
53 | * @return Album |
||
54 | */ |
||
55 | public function find($albumId, $userId){ |
||
60 | |||
61 | /** |
||
62 | * returns artist IDs mapped to album IDs |
||
63 | * does not include album_artist_id |
||
64 | * |
||
65 | * @param integer[] $albumIds IDs of the albums |
||
66 | * @return array the artist IDs of an album are accessible |
||
67 | * by the album ID inside of this array |
||
68 | */ |
||
69 | public function getAlbumArtistsByAlbumId($albumIds){ |
||
83 | |||
84 | /** |
||
85 | * returns albums of a specified artist |
||
86 | * The artist may be an album_artist or the artist of a track |
||
87 | * |
||
88 | * @param integer $artistId ID of the artist |
||
89 | * @param string $userId the user ID |
||
90 | * @return Album[] |
||
91 | */ |
||
92 | public function findAllByArtist($artistId, $userId){ |
||
106 | |||
107 | /** |
||
108 | * returns album that matches a name and year |
||
109 | * |
||
110 | * @param string $albumName name of the album |
||
111 | * @param string|integer $albumYear year of the album release |
||
112 | * @param string $userId the user ID |
||
113 | * @return Album |
||
114 | */ |
||
115 | public function findByNameAndYear($albumName, $albumYear, $userId){ |
||
131 | |||
132 | /** |
||
133 | * returns album that matches a name, a year and an album artist ID |
||
134 | * |
||
135 | * @param string|null $albumName name of the album |
||
136 | * @param string|integer|null $albumYear year of the album release |
||
137 | * @param string|integer|null $discNumber disk number of this album's disk |
||
138 | * @param integer|null $albumArtistId ID of the album artist |
||
139 | * @param string $userId the user ID |
||
140 | * @return Album[] |
||
141 | */ |
||
142 | public function findAlbum($albumName, $albumYear, $discNumber, $albumArtistId, $userId) { |
||
185 | |||
186 | /** |
||
187 | * @param integer $coverFileId |
||
188 | * @param integer $folderId |
||
189 | */ |
||
190 | public function updateFolderCover($coverFileId, $folderId){ |
||
202 | |||
203 | /** |
||
204 | * @param integer $coverFileId |
||
205 | * @param integer $albumId |
||
206 | */ |
||
207 | public function setCover($coverFileId, $albumId){ |
||
214 | |||
215 | /** |
||
216 | * @param integer $coverFileId |
||
217 | */ |
||
218 | public function removeCover($coverFileId){ |
||
225 | |||
226 | /** |
||
227 | * @return array of [albumId, parentFolderId] pairs |
||
228 | */ |
||
229 | public function getAlbumsWithoutCover(){ |
||
242 | |||
243 | /** |
||
244 | * @param integer $albumId |
||
245 | * @param integer $parentFolderId |
||
246 | */ |
||
247 | public function findAlbumCover($albumId, $parentFolderId){ |
||
279 | |||
280 | /** |
||
281 | * Returns the count of albums an Artist is featured in |
||
282 | * @param integer $artistId |
||
283 | * @param string $userId |
||
284 | * @return integer |
||
285 | */ |
||
286 | public function countByArtist($artistId, $userId){ |
||
298 | |||
299 | /** |
||
300 | * @param string $name |
||
301 | * @param string $userId |
||
302 | * @param bool $fuzzy |
||
303 | * @return Album[] |
||
304 | */ |
||
305 | View Code Duplication | public function findAllByName($name, $userId, $fuzzy = false){ |
|
316 | } |
||
317 |