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 SMB 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 SMB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class SMB extends Common implements INotifyStorage { |
||
| 60 | /** |
||
| 61 | * @var \Icewind\SMB\Server |
||
| 62 | */ |
||
| 63 | protected $server; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \Icewind\SMB\Share |
||
| 67 | */ |
||
| 68 | protected $share; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $root; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var \Icewind\SMB\FileInfo[] |
||
| 77 | */ |
||
| 78 | protected $statCache; |
||
| 79 | |||
| 80 | public function __construct($params) { |
||
| 81 | if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
||
| 82 | if (Server::NativeAvailable()) { |
||
| 83 | $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
||
|
|
|||
| 84 | } else { |
||
| 85 | $this->server = new Server($params['host'], $params['user'], $params['password']); |
||
| 86 | } |
||
| 87 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
||
| 88 | |||
| 89 | $this->root = isset($params['root']) ? $params['root'] : '/'; |
||
| 90 | View Code Duplication | if (!$this->root || $this->root[0] !== '/') { |
|
| 91 | $this->root = '/' . $this->root; |
||
| 92 | } |
||
| 93 | if (substr($this->root, -1, 1) !== '/') { |
||
| 94 | $this->root .= '/'; |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | throw new \Exception('Invalid configuration'); |
||
| 98 | } |
||
| 99 | $this->statCache = new CappedMemoryCache(); |
||
| 100 | parent::__construct($params); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getId() { |
||
| 107 | // FIXME: double slash to keep compatible with the old storage ids, |
||
| 108 | // failure to do so will lead to creation of a new storage id and |
||
| 109 | // loss of shares from the storage |
||
| 110 | return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $path |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | protected function buildPath($path) { |
||
| 118 | return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
||
| 119 | } |
||
| 120 | |||
| 121 | View Code Duplication | protected function relativePath($fullPath) { |
|
| 122 | if ($fullPath === $this->root) { |
||
| 123 | return ''; |
||
| 124 | } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
||
| 125 | return substr($fullPath, strlen($this->root)); |
||
| 126 | } else { |
||
| 127 | return null; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $path |
||
| 133 | * @return \Icewind\SMB\IFileInfo |
||
| 134 | * @throws StorageNotAvailableException |
||
| 135 | */ |
||
| 136 | protected function getFileInfo($path) { |
||
| 137 | try { |
||
| 138 | $path = $this->buildPath($path); |
||
| 139 | if (!isset($this->statCache[$path])) { |
||
| 140 | $this->statCache[$path] = $this->share->stat($path); |
||
| 141 | } |
||
| 142 | return $this->statCache[$path]; |
||
| 143 | } catch (ConnectException $e) { |
||
| 144 | \OC::$server->getLogger()->logException($e, ['message' => 'Error while getting file info']); |
||
| 145 | throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $path |
||
| 151 | * @return \Icewind\SMB\IFileInfo[] |
||
| 152 | * @throws StorageNotAvailableException |
||
| 153 | */ |
||
| 154 | protected function getFolderContents($path) { |
||
| 155 | try { |
||
| 156 | $path = $this->buildPath($path); |
||
| 157 | $files = $this->share->dir($path); |
||
| 158 | foreach ($files as $file) { |
||
| 159 | $this->statCache[$path . '/' . $file->getName()] = $file; |
||
| 160 | } |
||
| 161 | return array_filter($files, function (IFileInfo $file) { |
||
| 162 | try { |
||
| 163 | return !$file->isHidden(); |
||
| 164 | } catch (ForbiddenException $e) { |
||
| 165 | return false; |
||
| 166 | } catch (NotFoundException $e) { |
||
| 167 | return false; |
||
| 168 | } |
||
| 169 | }); |
||
| 170 | } catch (ConnectException $e) { |
||
| 171 | \OC::$server->getLogger()->logException($e, ['message' => 'Error while getting folder content']); |
||
| 172 | throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param \Icewind\SMB\IFileInfo $info |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | protected function formatInfo($info) { |
||
| 181 | $result = [ |
||
| 182 | 'size' => $info->getSize(), |
||
| 183 | 'mtime' => $info->getMTime(), |
||
| 184 | ]; |
||
| 185 | if ($info->isDirectory()) { |
||
| 186 | $result['type'] = 'dir'; |
||
| 187 | } else { |
||
| 188 | $result['type'] = 'file'; |
||
| 189 | } |
||
| 190 | return $result; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Rename the files. If the source or the target is the root, the rename won't happen. |
||
| 195 | * |
||
| 196 | * @param string $source the old name of the path |
||
| 197 | * @param string $target the new name of the path |
||
| 198 | * @return bool true if the rename is successful, false otherwise |
||
| 199 | */ |
||
| 200 | public function rename($source, $target, $retry = true) { |
||
| 201 | if ($this->isRootDir($source) || $this->isRootDir($target)) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | |||
| 205 | $absoluteSource = $this->buildPath($source); |
||
| 206 | $absoluteTarget = $this->buildPath($target); |
||
| 207 | try { |
||
| 208 | $result = $this->share->rename($absoluteSource, $absoluteTarget); |
||
| 209 | } catch (AlreadyExistsException $e) { |
||
| 210 | View Code Duplication | if ($retry) { |
|
| 211 | $this->remove($target); |
||
| 212 | $result = $this->share->rename($absoluteSource, $absoluteTarget, false); |
||
| 213 | } else { |
||
| 214 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | } catch (InvalidArgumentException $e) { |
||
| 218 | View Code Duplication | if ($retry) { |
|
| 219 | $this->remove($target); |
||
| 220 | $result = $this->share->rename($absoluteSource, $absoluteTarget, false); |
||
| 221 | } else { |
||
| 222 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
||
| 223 | return false; |
||
| 224 | } |
||
| 225 | } catch (\Exception $e) { |
||
| 226 | \OC::$server->getLogger()->logException($e, ['level' => Util::WARN]); |
||
| 227 | return false; |
||
| 228 | } |
||
| 229 | unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
||
| 230 | return $result; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function stat($path) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * get the best guess for the modification time of the share |
||
| 249 | * |
||
| 250 | * @return int |
||
| 251 | */ |
||
| 252 | private function shareMTime() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Check if the path is our root dir (not the smb one) |
||
| 269 | * |
||
| 270 | * @param string $path the path |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | private function isRootDir($path) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Check if our root points to a smb share |
||
| 279 | * |
||
| 280 | * @return bool true if our root points to a share false otherwise |
||
| 281 | */ |
||
| 282 | private function remoteIsShare() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $path |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function unlink($path) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * check if a file or folder has been updated since $time |
||
| 316 | * |
||
| 317 | * @param string $path |
||
| 318 | * @param int $time |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function hasUpdated($path, $time) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $path |
||
| 334 | * @param string $mode |
||
| 335 | * @return resource|false |
||
| 336 | */ |
||
| 337 | public function fopen($path, $mode) { |
||
| 338 | $fullPath = $this->buildPath($path); |
||
| 339 | try { |
||
| 340 | switch ($mode) { |
||
| 341 | case 'r': |
||
| 342 | case 'rb': |
||
| 343 | if (!$this->file_exists($path)) { |
||
| 344 | return false; |
||
| 345 | } |
||
| 346 | return $this->share->read($fullPath); |
||
| 347 | case 'w': |
||
| 348 | case 'wb': |
||
| 349 | $source = $this->share->write($fullPath); |
||
| 350 | return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) { |
||
| 351 | unset($this->statCache[$fullPath]); |
||
| 352 | }); |
||
| 353 | case 'a': |
||
| 354 | case 'ab': |
||
| 355 | case 'r+': |
||
| 356 | case 'w+': |
||
| 357 | case 'wb+': |
||
| 358 | case 'a+': |
||
| 359 | case 'x': |
||
| 360 | case 'x+': |
||
| 361 | case 'c': |
||
| 362 | case 'c+': |
||
| 363 | //emulate these |
||
| 364 | if (strrpos($path, '.') !== false) { |
||
| 365 | $ext = substr($path, strrpos($path, '.')); |
||
| 366 | } else { |
||
| 367 | $ext = ''; |
||
| 368 | } |
||
| 369 | View Code Duplication | if ($this->file_exists($path)) { |
|
| 370 | if (!$this->isUpdatable($path)) { |
||
| 371 | return false; |
||
| 372 | } |
||
| 373 | $tmpFile = $this->getCachedFile($path); |
||
| 374 | } else { |
||
| 375 | if (!$this->isCreatable(dirname($path))) { |
||
| 376 | return false; |
||
| 377 | } |
||
| 378 | $tmpFile = \OCP\Files::tmpFile($ext); |
||
| 379 | } |
||
| 380 | $source = fopen($tmpFile, $mode); |
||
| 381 | $share = $this->share; |
||
| 382 | return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) { |
||
| 383 | unset($this->statCache[$fullPath]); |
||
| 384 | $share->put($tmpFile, $fullPath); |
||
| 385 | unlink($tmpFile); |
||
| 386 | }); |
||
| 387 | } |
||
| 388 | return false; |
||
| 389 | } catch (NotFoundException $e) { |
||
| 390 | return false; |
||
| 391 | } catch (ForbiddenException $e) { |
||
| 392 | return false; |
||
| 393 | } catch (ConnectException $e) { |
||
| 394 | \OC::$server->getLogger()->logException($e, ['message' => 'Error while opening file']); |
||
| 395 | throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | public function rmdir($path) { |
||
| 425 | |||
| 426 | View Code Duplication | public function touch($path, $time = null) { |
|
| 439 | |||
| 440 | public function opendir($path) { |
||
| 454 | |||
| 455 | public function filetype($path) { |
||
| 464 | |||
| 465 | View Code Duplication | public function mkdir($path) { |
|
| 477 | |||
| 478 | public function file_exists($path) { |
||
| 490 | |||
| 491 | View Code Duplication | public function isReadable($path) { |
|
| 492 | try { |
||
| 493 | $info = $this->getFileInfo($path); |
||
| 494 | return !$info->isHidden(); |
||
| 495 | } catch (NotFoundException $e) { |
||
| 496 | return false; |
||
| 497 | } catch (ForbiddenException $e) { |
||
| 498 | return false; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | |||
| 502 | View Code Duplication | public function isUpdatable($path) { |
|
| 514 | |||
| 515 | View Code Duplication | public function isDeletable($path) { |
|
| 525 | |||
| 526 | /** |
||
| 527 | * check if smbclient is installed |
||
| 528 | */ |
||
| 529 | public static function checkDependencies() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Test a storage for availability |
||
| 538 | * |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | public function test() { |
||
| 549 | |||
| 550 | public function listen($path, callable $callback) { |
||
| 559 | |||
| 560 | public function notify($path) { |
||
| 565 | } |
||
| 566 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..