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 |
||
| 60 | class SMB extends Common implements INotifyStorage { |
||
| 61 | /** |
||
| 62 | * @var \Icewind\SMB\Server |
||
| 63 | */ |
||
| 64 | protected $server; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \Icewind\SMB\Share |
||
| 68 | */ |
||
| 69 | protected $share; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $root; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \Icewind\SMB\FileInfo[] |
||
| 78 | */ |
||
| 79 | protected $statCache; |
||
| 80 | |||
| 81 | public function __construct($params) { |
||
| 82 | if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
||
| 83 | if (Server::NativeAvailable()) { |
||
| 84 | $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
||
|
|
|||
| 85 | } else { |
||
| 86 | $this->server = new Server($params['host'], $params['user'], $params['password']); |
||
| 87 | } |
||
| 88 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
||
| 89 | |||
| 90 | $this->root = isset($params['root']) ? $params['root'] : '/'; |
||
| 91 | View Code Duplication | if (!$this->root || $this->root[0] !== '/') { |
|
| 92 | $this->root = '/' . $this->root; |
||
| 93 | } |
||
| 94 | if (substr($this->root, -1, 1) !== '/') { |
||
| 95 | $this->root .= '/'; |
||
| 96 | } |
||
| 97 | } else { |
||
| 98 | throw new \Exception('Invalid configuration'); |
||
| 99 | } |
||
| 100 | $this->statCache = new CappedMemoryCache(); |
||
| 101 | parent::__construct($params); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getId() { |
||
| 108 | // FIXME: double slash to keep compatible with the old storage ids, |
||
| 109 | // failure to do so will lead to creation of a new storage id and |
||
| 110 | // loss of shares from the storage |
||
| 111 | return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $path |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | protected function buildPath($path) { |
||
| 119 | return Filesystem::normalizePath($this->root . '/' . $path, true, false, true); |
||
| 120 | } |
||
| 121 | |||
| 122 | View Code Duplication | protected function relativePath($fullPath) { |
|
| 123 | if ($fullPath === $this->root) { |
||
| 124 | return ''; |
||
| 125 | } else if (substr($fullPath, 0, strlen($this->root)) === $this->root) { |
||
| 126 | return substr($fullPath, strlen($this->root)); |
||
| 127 | } else { |
||
| 128 | return null; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $path |
||
| 134 | * @return \Icewind\SMB\IFileInfo |
||
| 135 | * @throws StorageNotAvailableException |
||
| 136 | */ |
||
| 137 | protected function getFileInfo($path) { |
||
| 138 | try { |
||
| 139 | $path = $this->buildPath($path); |
||
| 140 | if (!isset($this->statCache[$path])) { |
||
| 141 | $this->statCache[$path] = $this->share->stat($path); |
||
| 142 | } |
||
| 143 | return $this->statCache[$path]; |
||
| 144 | } catch (ConnectException $e) { |
||
| 145 | \OC::$server->getLogger()->logException($e, ['message' => 'Error while getting file info']); |
||
| 146 | throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $path |
||
| 152 | * @return \Icewind\SMB\IFileInfo[] |
||
| 153 | * @throws StorageNotAvailableException |
||
| 154 | */ |
||
| 155 | protected function getFolderContents($path) { |
||
| 156 | try { |
||
| 157 | $path = $this->buildPath($path); |
||
| 158 | $files = $this->share->dir($path); |
||
| 159 | foreach ($files as $file) { |
||
| 160 | $this->statCache[$path . '/' . $file->getName()] = $file; |
||
| 161 | } |
||
| 162 | return array_filter($files, function (IFileInfo $file) { |
||
| 163 | try { |
||
| 164 | if ($file->isHidden()) { |
||
| 165 | \OC::$server->getLogger()->debug('hiding hidden file ' . $file->getName()); |
||
| 166 | } |
||
| 167 | return !$file->isHidden(); |
||
| 168 | } catch (ForbiddenException $e) { |
||
| 169 | \OC::$server->getLogger()->logException($e, ['level' => Util::DEBUG, 'message' => 'Hiding forbidden entry ' . $file->getName()]); |
||
| 170 | return false; |
||
| 171 | } catch (NotFoundException $e) { |
||
| 172 | \OC::$server->getLogger()->logException($e, ['level' => Util::DEBUG, 'message' => 'Hiding not found entry ' . $file->getName()]); |
||
| 173 | return false; |
||
| 174 | } |
||
| 175 | }); |
||
| 176 | } catch (ConnectException $e) { |
||
| 177 | \OC::$server->getLogger()->logException($e, ['message' => 'Error while getting folder content']); |
||
| 178 | throw new StorageNotAvailableException($e->getMessage(), $e->getCode(), $e); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param \Icewind\SMB\IFileInfo $info |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | protected function formatInfo($info) { |
||
| 187 | $result = [ |
||
| 188 | 'size' => $info->getSize(), |
||
| 189 | 'mtime' => $info->getMTime(), |
||
| 190 | ]; |
||
| 191 | if ($info->isDirectory()) { |
||
| 192 | $result['type'] = 'dir'; |
||
| 193 | } else { |
||
| 194 | $result['type'] = 'file'; |
||
| 195 | } |
||
| 196 | return $result; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Rename the files. If the source or the target is the root, the rename won't happen. |
||
| 201 | * |
||
| 202 | * @param string $source the old name of the path |
||
| 203 | * @param string $target the new name of the path |
||
| 204 | * @return bool true if the rename is successful, false otherwise |
||
| 205 | */ |
||
| 206 | public function rename($source, $target, $retry = true) { |
||
| 207 | if ($this->isRootDir($source) || $this->isRootDir($target)) { |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | |||
| 211 | $absoluteSource = $this->buildPath($source); |
||
| 212 | $absoluteTarget = $this->buildPath($target); |
||
| 213 | try { |
||
| 214 | $result = $this->share->rename($absoluteSource, $absoluteTarget); |
||
| 215 | } catch (AlreadyExistsException $e) { |
||
| 216 | View Code Duplication | if ($retry) { |
|
| 217 | $this->remove($target); |
||
| 218 | $result = $this->share->rename($absoluteSource, $absoluteTarget, false); |
||
| 219 | } else { |
||
| 220 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
||
| 221 | return false; |
||
| 222 | } |
||
| 223 | } catch (InvalidArgumentException $e) { |
||
| 224 | View Code Duplication | if ($retry) { |
|
| 225 | $this->remove($target); |
||
| 226 | $result = $this->share->rename($absoluteSource, $absoluteTarget, false); |
||
| 227 | } else { |
||
| 228 | \OC::$server->getLogger()->logException($e, ['level' => ILogger::WARN]); |
||
| 229 | return false; |
||
| 230 | } |
||
| 231 | } catch (\Exception $e) { |
||
| 232 | \OC::$server->getLogger()->logException($e, ['level' => Util::WARN]); |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
||
| 236 | return $result; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function stat($path, $retry = true) { |
||
| 240 | try { |
||
| 241 | $result = $this->formatInfo($this->getFileInfo($path)); |
||
| 242 | } catch (ForbiddenException $e) { |
||
| 243 | return false; |
||
| 244 | } catch (NotFoundException $e) { |
||
| 245 | return false; |
||
| 246 | } catch (TimedOutException $e) { |
||
| 247 | if ($retry) { |
||
| 248 | return $this->stat($path, false); |
||
| 249 | } else { |
||
| 250 | throw $e; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | if ($this->remoteIsShare() && $this->isRootDir($path)) { |
||
| 254 | $result['mtime'] = $this->shareMTime(); |
||
| 255 | } |
||
| 256 | return $result; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * get the best guess for the modification time of the share |
||
| 261 | * |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | private function shareMTime() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Check if the path is our root dir (not the smb one) |
||
| 281 | * |
||
| 282 | * @param string $path the path |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | private function isRootDir($path) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Check if our root points to a smb share |
||
| 291 | * |
||
| 292 | * @return bool true if our root points to a share false otherwise |
||
| 293 | */ |
||
| 294 | private function remoteIsShare() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $path |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public function unlink($path) { |
||
| 303 | if ($this->isRootDir($path)) { |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * check if a file or folder has been updated since $time |
||
| 328 | * |
||
| 329 | * @param string $path |
||
| 330 | * @param int $time |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function hasUpdated($path, $time) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $path |
||
| 346 | * @param string $mode |
||
| 347 | * @return resource|false |
||
| 348 | */ |
||
| 349 | public function fopen($path, $mode) { |
||
| 410 | |||
| 411 | public function rmdir($path) { |
||
| 437 | |||
| 438 | View Code Duplication | public function touch($path, $time = null) { |
|
| 451 | |||
| 452 | public function opendir($path) { |
||
| 466 | |||
| 467 | public function filetype($path) { |
||
| 476 | |||
| 477 | View Code Duplication | public function mkdir($path) { |
|
| 489 | |||
| 490 | public function file_exists($path) { |
||
| 502 | |||
| 503 | View Code Duplication | public function isReadable($path) { |
|
| 513 | |||
| 514 | View Code Duplication | public function isUpdatable($path) { |
|
| 526 | |||
| 527 | View Code Duplication | public function isDeletable($path) { |
|
| 537 | |||
| 538 | /** |
||
| 539 | * check if smbclient is installed |
||
| 540 | */ |
||
| 541 | public static function checkDependencies() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Test a storage for availability |
||
| 550 | * |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | public function test() { |
||
| 561 | |||
| 562 | public function listen($path, callable $callback) { |
||
| 571 | |||
| 572 | public function notify($path) { |
||
| 577 | } |
||
| 578 |
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..