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 |
||
| 41 | class Manager { |
||
| 42 | const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $uid; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var IDBConnection |
||
| 51 | */ |
||
| 52 | private $connection; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \OC\Files\Mount\Manager |
||
| 56 | */ |
||
| 57 | private $mountManager; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var IStorageFactory |
||
| 61 | */ |
||
| 62 | private $storageLoader; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var IClientService |
||
| 66 | */ |
||
| 67 | private $clientService; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var IManager |
||
| 71 | */ |
||
| 72 | private $notificationManager; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var IDiscoveryService |
||
| 76 | */ |
||
| 77 | private $discoveryService; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param IDBConnection $connection |
||
| 81 | * @param \OC\Files\Mount\Manager $mountManager |
||
| 82 | * @param IStorageFactory $storageLoader |
||
| 83 | * @param IClientService $clientService |
||
| 84 | * @param IManager $notificationManager |
||
| 85 | * @param IDiscoveryService $discoveryService |
||
| 86 | * @param string $uid |
||
| 87 | */ |
||
| 88 | public function __construct(IDBConnection $connection, |
||
| 103 | |||
| 104 | /** |
||
| 105 | * add new server-to-server share |
||
| 106 | * |
||
| 107 | * @param string $remote |
||
| 108 | * @param string $token |
||
| 109 | * @param string $password |
||
| 110 | * @param string $name |
||
| 111 | * @param string $owner |
||
| 112 | * @param boolean $accepted |
||
| 113 | * @param string $user |
||
| 114 | * @param int $remoteId |
||
| 115 | * @return Mount|null |
||
| 116 | */ |
||
| 117 | public function addShare($remote, $token, $password, $name, $owner, $accepted=false, $user = null, $remoteId = -1) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * get share |
||
| 177 | * |
||
| 178 | * @param int $id share id |
||
| 179 | * @return mixed share of false |
||
| 180 | */ |
||
| 181 | public function getShare($id) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * accept server-to-server share |
||
| 193 | * |
||
| 194 | * @param int $id |
||
| 195 | * @return bool True if the share could be accepted, false otherwise |
||
| 196 | */ |
||
| 197 | public function acceptShare($id) { |
||
| 198 | |||
| 199 | $share = $this->getShare($id); |
||
| 200 | |||
| 201 | if ($share) { |
||
| 202 | \OC_Util::setupFS($this->uid); |
||
| 203 | $shareFolder = Helper::getShareFolder(); |
||
| 204 | $mountPoint = Files::buildNotExistingFileName($shareFolder, $share['name']); |
||
| 205 | $mountPoint = Filesystem::normalizePath($mountPoint); |
||
| 206 | $hash = md5($mountPoint); |
||
| 207 | |||
| 208 | $acceptShare = $this->connection->prepare(' |
||
| 209 | UPDATE `*PREFIX*share_external` |
||
| 210 | SET `accepted` = ?, |
||
| 211 | `mountpoint` = ?, |
||
| 212 | `mountpoint_hash` = ? |
||
| 213 | WHERE `id` = ? AND `user` = ?'); |
||
| 214 | $acceptShare->execute(array(1, $mountPoint, $hash, $id, $this->uid)); |
||
| 215 | $this->sendFeedbackToRemote($share['remote'], $share['share_token'], $share['remote_id'], 'accept'); |
||
| 216 | |||
| 217 | \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $share['remote']]); |
||
| 218 | |||
| 219 | $this->processNotification($id); |
||
| 220 | return true; |
||
| 221 | } |
||
| 222 | |||
| 223 | return false; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * decline server-to-server share |
||
| 228 | * |
||
| 229 | * @param int $id |
||
| 230 | * @return bool True if the share could be declined, false otherwise |
||
| 231 | */ |
||
| 232 | public function declineShare($id) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param int $remoteShare |
||
| 251 | */ |
||
| 252 | public function processNotification($remoteShare) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * inform remote server whether server-to-server share was accepted/declined |
||
| 262 | * |
||
| 263 | * @param string $remote |
||
| 264 | * @param string $token |
||
| 265 | * @param int $remoteId Share id on the remote host |
||
| 266 | * @param string $feedback |
||
| 267 | * @return boolean |
||
| 268 | */ |
||
| 269 | private function sendFeedbackToRemote($remote, $token, $remoteId, $feedback) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * remove '/user/files' from the path and trailing slashes |
||
| 298 | * |
||
| 299 | * @param string $path |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | protected function stripPath($path) { |
||
| 306 | |||
| 307 | public function getMount($data) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $data |
||
| 317 | * @return Mount |
||
| 318 | */ |
||
| 319 | protected function mountShare($data) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return \OC\Files\Mount\Manager |
||
| 327 | */ |
||
| 328 | public function getMountManager() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $source |
||
| 334 | * @param string $target |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function setMountPoint($source, $target) { |
||
| 353 | |||
| 354 | public function removeShare($mountPoint) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * remove re-shares from share table and mapping in the federated_reshares table |
||
| 390 | * |
||
| 391 | * @param $mountPointId |
||
| 392 | */ |
||
| 393 | protected function removeReShares($mountPointId) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * remove all shares for user $uid if the user was deleted |
||
| 413 | * |
||
| 414 | * @param string $uid |
||
| 415 | * @return bool |
||
| 416 | */ |
||
| 417 | public function removeUserShares($uid) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * return a list of shares which are not yet accepted by the user |
||
| 440 | * |
||
| 441 | * @return array list of open server-to-server shares |
||
| 442 | */ |
||
| 443 | public function getOpenShares() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * return a list of shares which are accepted by the user |
||
| 449 | * |
||
| 450 | * @return array list of accepted server-to-server shares |
||
| 451 | */ |
||
| 452 | public function getAcceptedShares() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * return a list of shares for the user |
||
| 458 | * |
||
| 459 | * @param bool|null $accepted True for accepted only, |
||
| 460 | * false for not accepted, |
||
| 461 | * null for all shares of the user |
||
| 462 | * @return array list of open server-to-server shares |
||
| 463 | */ |
||
| 464 | private function getShares($accepted) { |
||
| 480 | } |
||
| 481 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: