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 |
||
31 | class PlaylistApiController extends Controller { |
||
32 | |||
33 | private $playlistBusinessLayer; |
||
34 | private $userId; |
||
35 | private $userFolder; |
||
36 | private $artistBusinessLayer; |
||
37 | private $albumBusinessLayer; |
||
38 | private $trackBusinessLayer; |
||
39 | private $urlGenerator; |
||
40 | private $l10n; |
||
41 | |||
42 | View Code Duplication | public function __construct($appname, |
|
62 | |||
63 | /** |
||
64 | * lists all playlists |
||
65 | * |
||
66 | * @NoAdminRequired |
||
67 | * @NoCSRFRequired |
||
68 | */ |
||
69 | public function getAll() { |
||
75 | |||
76 | /** |
||
77 | * creates a playlist |
||
78 | * |
||
79 | * @NoAdminRequired |
||
80 | * @NoCSRFRequired |
||
81 | */ |
||
82 | public function create() { |
||
93 | |||
94 | /** |
||
95 | * deletes a playlist |
||
96 | * @param int $id playlist ID |
||
97 | * |
||
98 | * @NoAdminRequired |
||
99 | * @NoCSRFRequired |
||
100 | */ |
||
101 | public function delete($id) { |
||
105 | |||
106 | /** |
||
107 | * lists a single playlist |
||
108 | * @param int $id playlist ID |
||
109 | * |
||
110 | * @NoAdminRequired |
||
111 | * @NoCSRFRequired |
||
112 | */ |
||
113 | public function get($id) { |
||
129 | |||
130 | private function toFullTree($playlist) { |
||
147 | |||
148 | /** |
||
149 | * update a playlist |
||
150 | * @param int $id playlist ID |
||
151 | * |
||
152 | * @NoAdminRequired |
||
153 | * @NoCSRFRequired |
||
154 | */ |
||
155 | public function update($id) { |
||
158 | |||
159 | /** |
||
160 | * add tracks to a playlist |
||
161 | * @param int $id playlist ID |
||
162 | * |
||
163 | * @NoAdminRequired |
||
164 | * @NoCSRFRequired |
||
165 | */ |
||
166 | public function addTracks($id) { |
||
169 | |||
170 | /** |
||
171 | * removes tracks from a playlist |
||
172 | * @param int $id playlist ID |
||
173 | * |
||
174 | * @NoAdminRequired |
||
175 | * @NoCSRFRequired |
||
176 | */ |
||
177 | public function removeTracks($id) { |
||
180 | |||
181 | /** |
||
182 | * moves single track on playlist to a new position |
||
183 | * @param int $id playlist ID |
||
184 | * |
||
185 | * @NoAdminRequired |
||
186 | * @NoCSRFRequired |
||
187 | */ |
||
188 | public function reorder($id) { |
||
192 | |||
193 | /** |
||
194 | * Modify playlist by calling a supplied method from PlaylistBusinessLayer |
||
195 | * @param string funcName Name of a function to call from PlaylistBusinessLayer |
||
196 | * @param array $funcParams Parameters to pass to the function 'funcName' |
||
197 | * @return \OCP\AppFramework\Http\JSONResponse JSON representation of the modified playlist |
||
198 | */ |
||
199 | private function modifyPlaylist($funcName, $funcParams) { |
||
208 | |||
209 | /** |
||
210 | * Get integer array passed as parameter to the Playlist API |
||
211 | * @param string $name Name of the parameter |
||
212 | * @return int[] |
||
213 | */ |
||
214 | private function paramArray($name) { |
||
221 | } |
||
222 |
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.