| Conditions | 10 |
| Paths | 20 |
| Total Lines | 98 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 225 | private function legacyMountPublicLink($token, $remote, $password, $name, $owner, $ownerDisplayName) { |
||
| 226 | |||
| 227 | // Check for invalid name |
||
| 228 | if (!\OCP\Util::isValidFileName($name)) { |
||
| 229 | return new JSONResponse(['message' => $this->l->t('The mountpoint name contains invalid characters.')], Http::STATUS_BAD_REQUEST); |
||
| 230 | } |
||
| 231 | $currentUser = $this->userSession->getUser()->getUID(); |
||
| 232 | $currentServer = $this->addressHandler->generateRemoteURL(); |
||
| 233 | if (\OC\Share\Helper::isSameUserOnSameServer($owner, $remote, $currentUser, $currentServer)) { |
||
| 234 | return new JSONResponse(['message' => $this->l->t('Not allowed to create a federated share with the owner.')], Http::STATUS_BAD_REQUEST); |
||
| 235 | } |
||
| 236 | $discoveryManager = new \OCA\FederatedFileSharing\DiscoveryManager( |
||
| 237 | \OC::$server->getMemCacheFactory(), |
||
| 238 | \OC::$server->getHTTPClientService() |
||
| 239 | ); |
||
| 240 | $externalManager = new \OCA\Files_Sharing\External\Manager( |
||
| 241 | \OC::$server->getDatabaseConnection(), |
||
| 242 | \OC\Files\Filesystem::getMountManager(), |
||
| 243 | \OC\Files\Filesystem::getLoader(), |
||
| 244 | \OC::$server->getHTTPHelper(), |
||
| 245 | \OC::$server->getNotificationManager(), |
||
| 246 | $discoveryManager, |
||
| 247 | \OC::$server->getUserSession()->getUser()->getUID() |
||
| 248 | ); |
||
| 249 | |||
| 250 | // check for ssl cert |
||
| 251 | if (substr($remote, 0, 5) === 'https') { |
||
| 252 | try { |
||
| 253 | $client = $this->clientService->newClient(); |
||
| 254 | $client->get($remote, [ |
||
| 255 | 'timeout' => 10, |
||
| 256 | 'connect_timeout' => 10, |
||
| 257 | ])->getBody(); |
||
| 258 | } catch (\Exception $e) { |
||
| 259 | return new JSONResponse(['message' => $this->l->t('Invalid or untrusted SSL certificate')], Http::STATUS_BAD_REQUEST); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | $mount = $externalManager->addShare($remote, $token, $password, $name, $ownerDisplayName, true); |
||
| 263 | /** |
||
| 264 | * @var \OCA\Files_Sharing\External\Storage $storage |
||
| 265 | */ |
||
| 266 | $storage = $mount->getStorage(); |
||
| 267 | try { |
||
| 268 | // check if storage exists |
||
| 269 | $storage->checkStorageAvailability(); |
||
| 270 | } catch (\OCP\Files\StorageInvalidException $e) { |
||
| 271 | // note: checkStorageAvailability will already remove the invalid share |
||
| 272 | \OCP\Util::writeLog( |
||
| 273 | 'federatedfilesharing', |
||
| 274 | 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), |
||
| 275 | \OCP\Util::DEBUG |
||
| 276 | ); |
||
| 277 | return new JSONResponse(['message' => $this->l->t('Could not authenticate to remote share, password might be wrong')], Http::STATUS_BAD_REQUEST); |
||
| 278 | } catch (\Exception $e) { |
||
| 279 | \OCP\Util::writeLog( |
||
| 280 | 'federatedfilesharing', |
||
| 281 | 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), |
||
| 282 | \OCP\Util::DEBUG |
||
| 283 | ); |
||
| 284 | $externalManager->removeShare($mount->getMountPoint()); |
||
| 285 | return new JSONResponse(['message' => $this->l->t('Storage not valid')], Http::STATUS_BAD_REQUEST); |
||
| 286 | } |
||
| 287 | $result = $storage->file_exists(''); |
||
| 288 | if ($result) { |
||
| 289 | try { |
||
| 290 | $storage->getScanner()->scanAll(); |
||
| 291 | return new JSONResponse( |
||
| 292 | [ |
||
| 293 | 'message' => $this->l->t('Federated Share successfully added'), |
||
| 294 | 'legacyMount' => '1' |
||
| 295 | ] |
||
| 296 | ); |
||
| 297 | } catch (\OCP\Files\StorageInvalidException $e) { |
||
| 298 | \OCP\Util::writeLog( |
||
| 299 | 'federatedfilesharing', |
||
| 300 | 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), |
||
| 301 | \OCP\Util::DEBUG |
||
| 302 | ); |
||
| 303 | return new JSONResponse(['message' => $this->l->t('Storage not valid')], Http::STATUS_BAD_REQUEST); |
||
| 304 | } catch (\Exception $e) { |
||
| 305 | \OCP\Util::writeLog( |
||
| 306 | 'federatedfilesharing', |
||
| 307 | 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(), |
||
| 308 | \OCP\Util::DEBUG |
||
| 309 | ); |
||
| 310 | return new JSONResponse(['message' => $this->l->t('Couldn\'t add remote share')], Http::STATUS_BAD_REQUEST); |
||
| 311 | } |
||
| 312 | } else { |
||
| 313 | $externalManager->removeShare($mount->getMountPoint()); |
||
| 314 | \OCP\Util::writeLog( |
||
| 315 | 'federatedfilesharing', |
||
| 316 | 'Couldn\'t add remote share', |
||
| 317 | \OCP\Util::DEBUG |
||
| 318 | ); |
||
| 319 | return new JSONResponse(['message' => $this->l->t('Couldn\'t add remote share')], Http::STATUS_BAD_REQUEST); |
||
| 320 | } |
||
| 321 | |||
| 322 | } |
||
| 323 | |||
| 325 |
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.