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 Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Helper { |
||
| 37 | |||
| 38 | public static function registerHooks() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Sets up the filesystem and user for public sharing |
||
| 48 | * @param string $token string share token |
||
| 49 | * @param string $relativePath optional path relative to the share |
||
| 50 | * @param string $password optional password |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public static function setupFromToken($token, $relativePath = null, $password = null) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Authenticate link item with the given password |
||
| 116 | * or with the session if no password was given. |
||
| 117 | * @param array $linkItem link item array |
||
| 118 | * @param string $password optional password |
||
| 119 | * |
||
| 120 | * @return boolean true if authorized, false otherwise |
||
| 121 | */ |
||
| 122 | public static function authenticate($linkItem, $password = null) { |
||
| 123 | if ($password !== null) { |
||
| 124 | if ($linkItem['share_type'] == \OCP\Share::SHARE_TYPE_LINK) { |
||
| 125 | // Check Password |
||
| 126 | $newHash = ''; |
||
| 127 | if(\OC::$server->getHasher()->verify($password, $linkItem['share_with'], $newHash)) { |
||
| 128 | // Save item id in session for future requests |
||
| 129 | \OC::$server->getSession()->set('public_link_authenticated', (string) $linkItem['id']); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * FIXME: Migrate old hashes to new hash format |
||
| 133 | * Due to the fact that there is no reasonable functionality to update the password |
||
| 134 | * of an existing share no migration is yet performed there. |
||
| 135 | * The only possibility is to update the existing share which will result in a new |
||
| 136 | * share ID and is a major hack. |
||
| 137 | * |
||
| 138 | * In the future the migration should be performed once there is a proper method |
||
| 139 | * to update the share's password. (for example `$share->updatePassword($password)` |
||
| 140 | * |
||
| 141 | * @link https://github.com/owncloud/core/issues/10671 |
||
| 142 | */ |
||
| 143 | if(!empty($newHash)) { |
||
| 144 | |||
| 145 | } |
||
| 146 | } else { |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | \OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type'] |
||
| 151 | .' for share id '.$linkItem['id'], \OCP\Util::ERROR); |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | } |
||
| 156 | View Code Duplication | else { |
|
| 157 | // not authenticated ? |
||
| 158 | if ( ! \OC::$server->getSession()->exists('public_link_authenticated') |
||
| 159 | || \OC::$server->getSession()->get('public_link_authenticated') !== (string)$linkItem['id']) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | return true; |
||
| 164 | } |
||
| 165 | |||
| 166 | public static function getSharesFromItem($target) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * get the UID of the owner of the file and the path to the file relative to |
||
| 207 | * owners files folder |
||
| 208 | * |
||
| 209 | * @param $filename |
||
| 210 | * @return array |
||
| 211 | * @throws \OC\User\NoUserException |
||
| 212 | */ |
||
| 213 | View Code Duplication | public static function getUidAndFilename($filename) { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Format a path to be relative to the /user/files/ directory |
||
| 237 | * @param string $path the absolute path |
||
| 238 | * @return string e.g. turns '/admin/files/test.txt' into 'test.txt' |
||
| 239 | */ |
||
| 240 | public static function stripUserFilesPath($path) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * check if file name already exists and generate unique target |
||
| 257 | * |
||
| 258 | * @param string $path |
||
| 259 | * @param array $excludeList |
||
| 260 | * @param View $view |
||
| 261 | * @return string $path |
||
| 262 | */ |
||
| 263 | public static function generateUniqueTarget($path, $excludeList, $view) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * get default share folder |
||
| 279 | * |
||
| 280 | * @param \OC\Files\View |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public static function getShareFolder($view = null) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * set default share folder |
||
| 307 | * |
||
| 308 | * @param string $shareFolder |
||
| 309 | */ |
||
| 310 | public static function setShareFolder($shareFolder) { |
||
| 313 | |||
| 314 | } |
||
| 315 |
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.