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 |
||
| 57 | class SMB extends Common implements INotifyStorage { |
||
| 58 | /** |
||
| 59 | * @var \Icewind\SMB\Server |
||
| 60 | */ |
||
| 61 | protected $server; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \Icewind\SMB\Share |
||
| 65 | */ |
||
| 66 | protected $share; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $root; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var \Icewind\SMB\FileInfo[] |
||
| 75 | */ |
||
| 76 | protected $statCache; |
||
| 77 | |||
| 78 | public function __construct($params) { |
||
| 79 | if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
||
| 80 | if (Server::NativeAvailable()) { |
||
| 81 | $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
||
|
|
|||
| 82 | } else { |
||
| 83 | $this->server = new Server($params['host'], $params['user'], $params['password']); |
||
| 84 | } |
||
| 85 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
||
| 86 | |||
| 87 | $this->root = $params['root'] ?? '/'; |
||
| 88 | $this->root = '/' . ltrim($this->root, '/'); |
||
| 89 | $this->root = rtrim($this->root, '/') . '/'; |
||
| 90 | } else { |
||
| 91 | throw new \Exception('Invalid configuration'); |
||
| 92 | } |
||
| 93 | $this->statCache = new CappedMemoryCache(); |
||
| 94 | parent::__construct($params); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getId() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param string $path |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | protected function buildPath($path) { |
||
| 114 | |||
| 115 | View Code Duplication | protected function relativePath($fullPath) { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $path |
||
| 127 | * @return \Icewind\SMB\IFileInfo |
||
| 128 | * @throws StorageNotAvailableException |
||
| 129 | */ |
||
| 130 | protected function getFileInfo($path) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $path |
||
| 144 | * @return \Icewind\SMB\IFileInfo[] |
||
| 145 | * @throws StorageNotAvailableException |
||
| 146 | */ |
||
| 147 | protected function getFolderContents($path) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param \Icewind\SMB\IFileInfo $info |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function formatInfo($info) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Rename the files. If the source or the target is the root, the rename won't happen. |
||
| 181 | * |
||
| 182 | * @param string $source the old name of the path |
||
| 183 | * @param string $target the new name of the path |
||
| 184 | * @return bool true if the rename is successful, false otherwise |
||
| 185 | */ |
||
| 186 | public function rename($source, $target) { |
||
| 187 | if ($this->isRootDir($source) || $this->isRootDir($target)) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | $absoluteSource = $this->buildPath($source); |
||
| 192 | $absoluteTarget = $this->buildPath($target); |
||
| 193 | try { |
||
| 194 | $result = $this->share->rename($absoluteSource, $absoluteTarget); |
||
| 195 | } catch (AlreadyExistsException $e) { |
||
| 196 | $this->remove($target); |
||
| 197 | $result = $this->share->rename($absoluteSource, $absoluteTarget); |
||
| 198 | } catch (\Exception $e) { |
||
| 199 | \OC::$server->getLogger()->logException($e, ['level' => Util::WARN]); |
||
| 200 | return false; |
||
| 201 | } |
||
| 202 | unset($this->statCache[$absoluteSource], $this->statCache[$absoluteTarget]); |
||
| 203 | return $result; |
||
| 204 | } |
||
| 205 | |||
| 206 | public function stat($path) { |
||
| 207 | try { |
||
| 208 | $result = $this->formatInfo($this->getFileInfo($path)); |
||
| 209 | } catch (ForbiddenException $e) { |
||
| 210 | return false; |
||
| 211 | } catch (NotFoundException $e) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | if ($this->remoteIsShare() && $this->isRootDir($path)) { |
||
| 215 | $result['mtime'] = $this->shareMTime(); |
||
| 216 | } |
||
| 217 | return $result; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * get the best guess for the modification time of the share |
||
| 222 | * |
||
| 223 | * @return int |
||
| 224 | */ |
||
| 225 | private function shareMTime() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Check if the path is our root dir (not the smb one) |
||
| 238 | * |
||
| 239 | * @param string $path the path |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | private function isRootDir($path) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Check if our root points to a smb share |
||
| 248 | * |
||
| 249 | * @return bool true if our root points to a share false otherwise |
||
| 250 | */ |
||
| 251 | private function remoteIsShare() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param string $path |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function unlink($path) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * check if a file or folder has been updated since $time |
||
| 284 | * |
||
| 285 | * @param string $path |
||
| 286 | * @param int $time |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | public function hasUpdated($path, $time) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $path |
||
| 302 | * @param string $mode |
||
| 303 | * @return resource|false |
||
| 304 | */ |
||
| 305 | public function fopen($path, $mode) { |
||
| 365 | |||
| 366 | public function rmdir($path) { |
||
| 391 | |||
| 392 | View Code Duplication | public function touch($path, $time = null) { |
|
| 404 | |||
| 405 | public function opendir($path) { |
||
| 419 | |||
| 420 | public function filetype($path) { |
||
| 429 | |||
| 430 | View Code Duplication | public function mkdir($path) { |
|
| 441 | |||
| 442 | public function file_exists($path) { |
||
| 454 | |||
| 455 | View Code Duplication | public function isReadable($path) { |
|
| 465 | |||
| 466 | View Code Duplication | public function isUpdatable($path) { |
|
| 478 | |||
| 479 | View Code Duplication | public function isDeletable($path) { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * check if smbclient is installed |
||
| 492 | */ |
||
| 493 | public static function checkDependencies() { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Test a storage for availability |
||
| 502 | * |
||
| 503 | * @return bool |
||
| 504 | */ |
||
| 505 | public function test() { |
||
| 512 | |||
| 513 | public function listen($path, callable $callback) { |
||
| 522 | |||
| 523 | public function notify($path) { |
||
| 528 | } |
||
| 529 |
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..