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 |
||
17 | class TrackMapper extends BaseMapper { |
||
18 | |||
19 | public function __construct(IDBConnection $db){ |
||
22 | |||
23 | /** |
||
24 | * @param string $condition |
||
25 | */ |
||
26 | private function makeSelectQueryWithoutUserId($condition){ |
||
30 | |||
31 | /** |
||
32 | * @param string $condition |
||
33 | */ |
||
34 | private function makeSelectQuery($condition=null){ |
||
37 | |||
38 | /** |
||
39 | * @param string $userId |
||
40 | * @param integer $limit |
||
41 | * @param integer $offset |
||
42 | * @return Track[] |
||
43 | */ |
||
44 | public function findAll($userId, $limit=null, $offset=null){ |
||
49 | |||
50 | /** |
||
51 | * @param integer $artistId |
||
52 | * @param string $userId |
||
53 | * @return Track[] |
||
54 | */ |
||
55 | public function findAllByArtist($artistId, $userId){ |
||
61 | |||
62 | /** |
||
63 | * @param integer $albumId |
||
64 | * @param string $userId |
||
65 | * @param integer $artistId |
||
66 | * @return Track[] |
||
67 | */ |
||
68 | public function findAllByAlbum($albumId, $userId, $artistId = null){ |
||
78 | |||
79 | /** |
||
80 | * @param string $userId |
||
81 | * @return int[] |
||
82 | */ |
||
83 | public function findAllFileIds($userId){ |
||
89 | |||
90 | /** |
||
91 | * Find a track of user matching a file ID |
||
92 | * @param integer $fileId |
||
93 | * @param string $userId |
||
94 | * @return Track |
||
95 | * @throws \OCP\AppFramework\Db\DoesNotExistException if not found |
||
96 | */ |
||
97 | public function findByFileId($fileId, $userId){ |
||
102 | |||
103 | /** |
||
104 | * Find tracks of user with multiple file IDs |
||
105 | * @param integer[] $fileIds |
||
106 | * @param string $userId |
||
107 | * @return Track[] |
||
108 | */ |
||
109 | public function findByFileIds($fileIds, $userId){ |
||
115 | |||
116 | /** |
||
117 | * Finds tracks of all users matching one or multiple file IDs |
||
118 | * @param integer[] $fileIds |
||
119 | * @return Track[] |
||
120 | */ |
||
121 | public function findAllByFileIds($fileIds){ |
||
126 | |||
127 | /** |
||
128 | * @param integer $artistId |
||
129 | * @return integer |
||
130 | */ |
||
131 | View Code Duplication | public function countByArtist($artistId){ |
|
138 | |||
139 | /** |
||
140 | * @param integer $albumId |
||
141 | * @return integer |
||
142 | */ |
||
143 | View Code Duplication | public function countByAlbum($albumId){ |
|
150 | |||
151 | /** |
||
152 | * @param string $name |
||
153 | * @param string $userId |
||
154 | * @param bool $fuzzy |
||
155 | * @return Track[] |
||
156 | */ |
||
157 | View Code Duplication | public function findAllByName($name, $userId, $fuzzy = false){ |
|
168 | |||
169 | /** |
||
170 | * @param string $name |
||
171 | * @param string $userId |
||
172 | * @return Track[] |
||
173 | */ |
||
174 | public function findAllByNameRecursive($name, $userId){ |
||
183 | |||
184 | public function findUniqueEntity(Track $track) { |
||
187 | } |
||
188 |
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.