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 |
||
| 30 | class Shared_Updater { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Walk up the users file tree and update the etags. |
||
| 34 | * |
||
| 35 | * @param string $user user id |
||
| 36 | * @param string $path share mount point path, relative to the user's "files" folder |
||
| 37 | */ |
||
| 38 | 30 | static private function correctUsersFolder($user, $path) { |
|
| 39 | // $path points to the mount point which is a virtual folder, so we start with |
||
| 40 | // the parent |
||
| 41 | 30 | $path = '/' . ltrim($path, '/'); |
|
| 42 | 30 | $path = '/files' . dirname($path); |
|
| 43 | 30 | \OC\Files\Filesystem::initMountPoints($user); |
|
| 44 | 30 | $view = new \OC\Files\View('/' . $user); |
|
| 45 | 30 | if ($view->file_exists($path)) { |
|
| 46 | 27 | while ($path !== dirname($path)) { |
|
| 47 | 27 | $etag = $view->getETag($path); |
|
| 48 | 27 | $view->putFileInfo($path, array('etag' => $etag)); |
|
| 49 | 27 | $path = dirname($path); |
|
| 50 | 27 | } |
|
| 51 | 27 | } else { |
|
| 52 | 3 | \OCP\Util::writeLog('files_sharing', 'can not update etags on ' . $path . ' for user ' . $user . '. Path does not exists', \OCP\Util::DEBUG); |
|
| 53 | } |
||
| 54 | 30 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @param array $params |
||
| 58 | */ |
||
| 59 | 3 | static public function renameHook($params) { |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Fix for https://github.com/owncloud/core/issues/20769 |
||
| 66 | * |
||
| 67 | * The owner is allowed to move their files (if they are shared) into a receiving folder |
||
| 68 | * In this case we need to update the parent of the moved share. Since they are |
||
| 69 | * effectively handing over ownership of the file the rest of the code needs to know |
||
| 70 | * they need to build up the reshare tree. |
||
| 71 | * |
||
| 72 | * @param string $path |
||
| 73 | */ |
||
| 74 | 3 | static private function moveShareToShare($path) { |
|
| 109 | 9 | ||
| 110 | /** |
||
| 111 | * @param array $params |
||
| 112 | */ |
||
| 113 | static public function deleteHook($params) { |
||
| 116 | |||
| 117 | 30 | /** |
|
| 118 | * update etags if a file was shared |
||
| 119 | 30 | * @param array $params |
|
| 120 | 30 | */ |
|
| 121 | static public function postShareHook($params) { |
||
| 137 | 4 | ||
| 138 | /** |
||
| 139 | * update etags if a file was unshared |
||
| 140 | 4 | * |
|
| 141 | 4 | * @param array $params |
|
| 142 | 4 | */ |
|
| 143 | static public function postUnshareHook($params) { |
||
| 163 | 2 | ||
| 164 | 2 | /** |
|
| 165 | 2 | * update etags if file was unshared from self |
|
| 166 | * @param array $params |
||
| 167 | */ |
||
| 168 | static public function postUnshareFromSelfHook($params) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * clean up oc_share table from files which are no longer exists |
||
| 184 | * |
||
| 185 | * This fixes issues from updates from files_sharing < 0.3.5.6 (ownCloud 4.5) |
||
| 186 | * It will just be called during the update of the app |
||
| 187 | */ |
||
| 188 | static public function fixBrokenSharesOnAppUpdate() { |
||
| 196 | |||
| 197 | 3 | /** |
|
| 198 | * rename mount point from the children if the parent was renamed |
||
| 199 | 3 | * |
|
| 200 | 3 | * @param string $oldPath old path relative to data/user/files |
|
| 201 | * @param string $newPath new path relative to data/user/files |
||
| 202 | 3 | */ |
|
| 203 | 3 | static private function renameChildren($oldPath, $newPath) { |
|
| 218 | |||
| 219 | } |
||
| 220 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.