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 | 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 = "") { |
||
| 70 | |||
| 71 | if ($from_own == 0) { |
||
| 72 | // allow only http(s) and (s)ftp |
||
| 73 | $protocols = '/^(https?|s?ftp)\:\/\//i'; |
||
| 74 | if (preg_match($protocols, $url)) { |
||
| 75 | $data = Bookmarks::getURLMetadata($url); |
||
| 76 | // if not (allowed) protocol is given, assume http and https (and fetch both) |
||
| 77 | } else { |
||
| 78 | // append https to url and fetch it |
||
| 79 | $url_https = 'https://' . $url; |
||
| 80 | $data_https = Bookmarks::getURLMetadata($url_https); |
||
| 81 | // append http to url and fetch it |
||
| 82 | $url_http = 'http://' . $url; |
||
| 83 | $data_http = Bookmarks::getURLMetadata($url_http); |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($title === '' && isset($data['title'])) { // prefer original url if working |
||
| 87 | $title = $data['title']; |
||
| 88 | //url remains unchanged |
||
| 89 | } elseif (isset($data_https['title'])) { // test if https works |
||
| 90 | $title = $title === '' ? $data_https['title'] : $title; |
||
| 91 | $url = $url_https; |
||
|
|
|||
| 92 | } elseif (isset($data_http['title'])) { // otherwise test http for results |
||
| 93 | $title = $title === '' ? $data_http['title'] : $title; |
||
| 94 | $url = $url_http; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // Check if it is a valid URL (after adding http(s) prefix) |
||
| 99 | $urlData = parse_url($url); |
||
| 100 | View Code Duplication | if ($urlData === false || !isset($urlData['scheme']) || !isset($urlData['host'])) { |
|
| 101 | return new JSONResponse(array('status' => 'error'), Http::STATUS_BAD_REQUEST); |
||
| 102 | } |
||
| 103 | |||
| 104 | $tags = isset($item['tags']) ? $item['tags'] : array(); |
||
| 105 | |||
| 106 | $id = Bookmarks::addBookmark($this->userId, $this->db, $url, $title, $tags, $description, $is_public); |
||
| 107 | $bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db); |
||
| 108 | return new JSONResponse(array('item' => $bm, 'status' => 'success')); |
||
| 109 | } |
||
| 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 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: