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 |
||
| 42 | class Local extends \OC\Files\Storage\Common { |
||
| 43 | protected $datadir; |
||
| 44 | |||
| 45 | protected $dataDirLength; |
||
| 46 | |||
| 47 | protected $allowSymlinks = false; |
||
| 48 | |||
| 49 | protected $realDataDir; |
||
| 50 | |||
| 51 | public function __construct($arguments) { |
||
| 52 | if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { |
||
| 53 | throw new \InvalidArgumentException('No data directory set for local storage'); |
||
| 54 | } |
||
| 55 | $this->datadir = $arguments['datadir']; |
||
| 56 | $this->realDataDir = rtrim(realpath($this->datadir), '/') . '/'; |
||
| 57 | if (substr($this->datadir, -1) !== '/') { |
||
| 58 | $this->datadir .= '/'; |
||
| 59 | } |
||
| 60 | $this->dataDirLength = strlen($this->realDataDir); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function __destruct() { |
||
| 65 | |||
| 66 | public function getId() { |
||
| 67 | return 'local::' . $this->datadir; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function mkdir($path) { |
||
| 71 | return @mkdir($this->getSourcePath($path), 0777, true); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function rmdir($path) { |
||
| 109 | |||
| 110 | public function opendir($path) { |
||
| 113 | |||
| 114 | public function is_dir($path) { |
||
| 120 | |||
| 121 | public function is_file($path) { |
||
| 124 | |||
| 125 | public function stat($path) { |
||
| 136 | |||
| 137 | public function filetype($path) { |
||
| 144 | |||
| 145 | public function filesize($path) { |
||
| 156 | |||
| 157 | public function isReadable($path) { |
||
| 160 | |||
| 161 | public function isUpdatable($path) { |
||
| 164 | |||
| 165 | public function file_exists($path) { |
||
| 168 | |||
| 169 | public function filemtime($path) { |
||
| 170 | clearstatcache($this->getSourcePath($path)); |
||
| 171 | return $this->file_exists($path) ? filemtime($this->getSourcePath($path)) : false; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function touch($path, $mtime = null) { |
||
| 175 | // sets the modification time of the file to the given value. |
||
| 176 | // If mtime is nil the current time is set. |
||
| 177 | // note that the access time of the file always changes to the current time. |
||
| 178 | if ($this->file_exists($path) and !$this->isUpdatable($path)) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | if (!is_null($mtime)) { |
||
| 182 | $result = touch($this->getSourcePath($path), $mtime); |
||
| 183 | } else { |
||
| 184 | $result = touch($this->getSourcePath($path)); |
||
| 185 | } |
||
| 186 | if ($result) { |
||
| 187 | clearstatcache(true, $this->getSourcePath($path)); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $result; |
||
| 191 | } |
||
| 192 | |||
| 193 | public function file_get_contents($path) { |
||
| 194 | // file_get_contents() has a memory leak: https://bugs.php.net/bug.php?id=61961 |
||
| 195 | $fileName = $this->getSourcePath($path); |
||
| 196 | |||
| 197 | $fileSize = filesize($fileName); |
||
| 198 | if ($fileSize === 0) { |
||
| 199 | return ''; |
||
| 200 | } |
||
| 201 | |||
| 202 | $handle = fopen($fileName, 'rb'); |
||
| 203 | $content = fread($handle, $fileSize); |
||
| 204 | fclose($handle); |
||
| 205 | return $content; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function file_put_contents($path, $data) { |
||
| 211 | |||
| 212 | View Code Duplication | public function unlink($path) { |
|
| 213 | if ($this->is_dir($path)) { |
||
| 214 | return $this->rmdir($path); |
||
| 215 | } else if ($this->is_file($path)) { |
||
| 216 | return unlink($this->getSourcePath($path)); |
||
| 217 | } else { |
||
| 218 | return false; |
||
| 219 | } |
||
| 220 | |||
| 221 | } |
||
| 222 | |||
| 223 | public function rename($path1, $path2) { |
||
| 263 | |||
| 264 | public function copy($path1, $path2) { |
||
| 271 | |||
| 272 | public function fopen($path, $mode) { |
||
| 275 | |||
| 276 | public function hash($type, $path, $raw = false) { |
||
| 279 | |||
| 280 | public function free_space($path) { |
||
| 295 | |||
| 296 | public function search($query) { |
||
| 299 | |||
| 300 | public function getLocalFile($path) { |
||
| 303 | |||
| 304 | public function getLocalFolder($path) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $query |
||
| 310 | * @param string $dir |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | protected function searchInDir($query, $dir = '') { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * check if a file or folder has been updated since $time |
||
| 333 | * |
||
| 334 | * @param string $path |
||
| 335 | * @param int $time |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public function hasUpdated($path, $time) { |
||
| 339 | if ($this->file_exists($path)) { |
||
| 340 | return $this->filemtime($path) > $time; |
||
| 341 | } else { |
||
| 342 | return true; |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get the source path (on disk) of a given path |
||
| 348 | * |
||
| 349 | * @param string $path |
||
| 350 | * @return string |
||
| 351 | * @throws ForbiddenException |
||
| 352 | */ |
||
| 353 | public function getSourcePath($path) { |
||
| 354 | $fullPath = $this->datadir . $path; |
||
| 355 | if ($this->allowSymlinks || $path === '') { |
||
| 356 | return $fullPath; |
||
| 357 | } |
||
| 358 | $pathToResolve = $fullPath; |
||
| 359 | $realPath = realpath($pathToResolve); |
||
| 360 | while ($realPath === false) { // for non existing files check the parent directory |
||
| 361 | $pathToResolve = dirname($pathToResolve); |
||
| 362 | $realPath = realpath($pathToResolve); |
||
| 363 | } |
||
| 364 | if ($realPath) { |
||
| 365 | $realPath = $realPath . '/'; |
||
| 366 | } |
||
| 367 | if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) { |
||
| 368 | return $fullPath; |
||
| 369 | } else { |
||
| 370 | throw new ForbiddenException("Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", false); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * {@inheritdoc} |
||
| 376 | */ |
||
| 377 | public function isLocal() { |
||
| 378 | return true; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * get the ETag for a file or folder |
||
| 383 | * |
||
| 384 | * @param string $path |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function getETag($path) { |
||
| 388 | if ($this->is_file($path)) { |
||
| 389 | $stat = $this->stat($path); |
||
| 390 | return md5( |
||
| 391 | $stat['mtime'] . |
||
| 392 | $stat['ino'] . |
||
| 393 | $stat['dev'] . |
||
| 394 | $stat['size'] |
||
| 395 | ); |
||
| 396 | } else { |
||
| 397 | return parent::getETag($path); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param \OCP\Files\Storage $sourceStorage |
||
| 403 | * @param string $sourceInternalPath |
||
| 404 | * @param string $targetInternalPath |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | View Code Duplication | public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 408 | if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) { |
||
| 409 | /** |
||
| 410 | * @var \OC\Files\Storage\Local $sourceStorage |
||
| 411 | */ |
||
| 412 | $rootStorage = new Local(['datadir' => '/']); |
||
| 413 | return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); |
||
| 414 | } else { |
||
| 415 | return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
||
| 416 | } |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param \OCP\Files\Storage $sourceStorage |
||
| 421 | * @param string $sourceInternalPath |
||
| 422 | * @param string $targetInternalPath |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | View Code Duplication | public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 436 | } |
||
| 437 |