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 Local 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 Local, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Local extends \OC\Files\Storage\Common { |
||
| 50 | protected $datadir; |
||
| 51 | |||
| 52 | protected $dataDirLength; |
||
| 53 | |||
| 54 | protected $allowSymlinks = false; |
||
| 55 | |||
| 56 | protected $realDataDir; |
||
| 57 | |||
| 58 | public function __construct($arguments) { |
||
| 59 | if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { |
||
| 60 | throw new \InvalidArgumentException('No data directory set for local storage'); |
||
| 61 | } |
||
| 62 | $this->datadir = str_replace('//', '/', $arguments['datadir']); |
||
| 63 | // some crazy code uses a local storage on root... |
||
| 64 | if ($this->datadir === '/') { |
||
| 65 | $this->realDataDir = $this->datadir; |
||
| 66 | } else { |
||
| 67 | $realPath = realpath($this->datadir) ?: $this->datadir; |
||
| 68 | $this->realDataDir = rtrim($realPath, '/') . '/'; |
||
| 69 | } |
||
| 70 | if (substr($this->datadir, -1) !== '/') { |
||
| 71 | $this->datadir .= '/'; |
||
| 72 | } |
||
| 73 | $this->dataDirLength = strlen($this->realDataDir); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function __destruct() { |
||
| 78 | |||
| 79 | public function getId() { |
||
| 82 | |||
| 83 | public function mkdir($path) { |
||
| 86 | |||
| 87 | public function rmdir($path) { |
||
| 122 | |||
| 123 | public function opendir($path) { |
||
| 126 | |||
| 127 | public function is_dir($path) { |
||
| 133 | |||
| 134 | public function is_file($path) { |
||
| 137 | |||
| 138 | public function stat($path) { |
||
| 149 | |||
| 150 | public function filetype($path) { |
||
| 157 | |||
| 158 | public function filesize($path) { |
||
| 169 | |||
| 170 | public function isReadable($path) { |
||
| 173 | |||
| 174 | public function isUpdatable($path) { |
||
| 177 | |||
| 178 | public function file_exists($path) { |
||
| 181 | |||
| 182 | public function filemtime($path) { |
||
| 194 | |||
| 195 | public function touch($path, $mtime = null) { |
||
| 213 | |||
| 214 | public function file_get_contents($path) { |
||
| 217 | |||
| 218 | public function file_put_contents($path, $data) { |
||
| 221 | |||
| 222 | View Code Duplication | public function unlink($path) { |
|
| 232 | |||
| 233 | public function rename($path1, $path2) { |
||
| 273 | |||
| 274 | public function copy($path1, $path2) { |
||
| 281 | |||
| 282 | public function fopen($path, $mode) { |
||
| 285 | |||
| 286 | public function hash($type, $path, $raw = false) { |
||
| 289 | |||
| 290 | public function free_space($path) { |
||
| 305 | |||
| 306 | public function search($query) { |
||
| 309 | |||
| 310 | public function getLocalFile($path) { |
||
| 313 | |||
| 314 | public function getLocalFolder($path) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $query |
||
| 320 | * @param string $dir |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | protected function searchInDir($query, $dir = '') { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * check if a file or folder has been updated since $time |
||
| 343 | * |
||
| 344 | * @param string $path |
||
| 345 | * @param int $time |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | public function hasUpdated($path, $time) { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the source path (on disk) of a given path |
||
| 358 | * |
||
| 359 | * @param string $path |
||
| 360 | * @return string |
||
| 361 | * @throws ForbiddenException |
||
| 362 | */ |
||
| 363 | public function getSourcePath($path) { |
||
| 364 | $fullPath = $this->datadir . $path; |
||
| 365 | $currentPath = $path; |
||
| 366 | if ($this->allowSymlinks || $currentPath === '') { |
||
| 367 | return $fullPath; |
||
| 368 | } |
||
| 369 | $pathToResolve = $fullPath; |
||
| 370 | $realPath = realpath($pathToResolve); |
||
| 371 | while ($realPath === false) { // for non existing files check the parent directory |
||
| 372 | $currentPath = dirname($currentPath); |
||
| 373 | if ($currentPath === '' || $currentPath === '.') { |
||
| 374 | return $fullPath; |
||
| 375 | } |
||
| 376 | $realPath = realpath($this->datadir . $currentPath); |
||
| 377 | } |
||
| 378 | if ($realPath) { |
||
| 379 | $realPath = $realPath . '/'; |
||
| 380 | } |
||
| 381 | if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) { |
||
| 382 | return $fullPath; |
||
| 383 | } |
||
| 384 | |||
| 385 | \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", \OCP\Util::ERROR); |
||
| 386 | throw new ForbiddenException('Following symlinks is not allowed', false); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * {@inheritdoc} |
||
| 391 | */ |
||
| 392 | public function isLocal() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * get the ETag for a file or folder |
||
| 398 | * |
||
| 399 | * @param string $path |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getETag($path) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param IStorage $sourceStorage |
||
| 418 | * @param string $sourceInternalPath |
||
| 419 | * @param string $targetInternalPath |
||
| 420 | * @param bool $preserveMtime |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | View Code Duplication | public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath, $preserveMtime = false) { |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @param IStorage $sourceStorage |
||
| 443 | * @param string $sourceInternalPath |
||
| 444 | * @param string $targetInternalPath |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | View Code Duplication | public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 464 | } |
||
| 465 |