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:
Complex classes like BookmarkController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BookmarkController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class BookmarkController extends ApiController { |
||
24 | |||
25 | private $userId; |
||
26 | private $db; |
||
27 | |||
28 | View Code Duplication | public function __construct($appName, IRequest $request, $userId, IDb $db) { |
|
|
|||
29 | parent::__construct($appName, $request); |
||
30 | $this->userId = $userId; |
||
31 | $this->db = $db; |
||
32 | $this->request = $request; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @NoAdminRequired |
||
37 | */ |
||
38 | public function legacyGetBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") { |
||
41 | |||
42 | /** |
||
43 | * @NoAdminRequired |
||
44 | */ |
||
45 | public function getBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") { |
||
65 | |||
66 | /** |
||
67 | * @NoAdminRequired |
||
68 | */ |
||
69 | public function newBookmark($url = "", $item = array(), $from_own = 0, $title = "", $is_public = false, $description = "") { |
||
110 | |||
111 | /** |
||
112 | @NoAdminRequired |
||
113 | * |
||
114 | * @param int $id |
||
115 | * @param bool $is_public Description |
||
116 | * @return \OCP\AppFramework\Http\TemplateResponse |
||
117 | */ |
||
118 | //TODO id vs record_id? |
||
119 | public function legacyEditBookmark($id = null, $url = "", $item = array(), $title = "", $is_public = false, $record_id = null, $description = "") { |
||
126 | |||
127 | /** |
||
128 | @NoAdminRequired |
||
129 | * |
||
130 | * @param int $id |
||
131 | * @param bool $is_public Description |
||
132 | * @return \OCP\AppFramework\Http\TemplateResponse |
||
133 | */ |
||
134 | public function editBookmark($id = null, $url = "", $item = array(), $title = "", $is_public = false, $record_id = null, $description = "") { |
||
155 | |||
156 | /** |
||
157 | @NoAdminRequired |
||
158 | * |
||
159 | * @param int $id |
||
160 | * @param bool $is_public Description |
||
161 | * @return \OCP\AppFramework\Http\JSONResponse |
||
162 | */ |
||
163 | public function legacyDeleteBookmark($id = -1) { |
||
166 | |||
167 | /** |
||
168 | @NoAdminRequired |
||
169 | * |
||
170 | * @param int $id |
||
171 | * @param bool $is_public Description |
||
172 | * @return \OCP\AppFramework\Http\JSONResponse |
||
173 | */ |
||
174 | public function deleteBookmark($id = -1) { |
||
185 | |||
186 | /** |
||
187 | @NoAdminRequired |
||
188 | * |
||
189 | * @param string $url |
||
190 | * @return \OCP\AppFramework\Http\JSONResponse |
||
191 | */ |
||
192 | public function clickBookmark($url = "") { |
||
212 | |||
213 | /** |
||
214 | @NoAdminRequired |
||
215 | * |
||
216 | * @return \OCP\AppFramework\Http\JSONResponse |
||
217 | */ |
||
218 | public function importBookmark() { |
||
243 | |||
244 | /** |
||
245 | @NoAdminRequired |
||
246 | * |
||
247 | * @return \OCP\AppFramework\Http\JSONResponse |
||
248 | */ |
||
249 | public function exportBookmark() { |
||
277 | |||
278 | } |
||
279 |
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.