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 |
||
| 39 | class Local extends \OC\Files\Storage\Common { |
||
| 40 | protected $datadir; |
||
| 41 | |||
| 42 | public function __construct($arguments) { |
||
| 43 | if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { |
||
| 44 | throw new \InvalidArgumentException('No data directory set for local storage'); |
||
| 45 | } |
||
| 46 | $this->datadir = $arguments['datadir']; |
||
| 47 | if (substr($this->datadir, -1) !== '/') { |
||
| 48 | $this->datadir .= '/'; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | public function __destruct() { |
||
| 54 | |||
| 55 | public function getId() { |
||
| 56 | return 'local::' . $this->datadir; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function mkdir($path) { |
||
| 60 | return @mkdir($this->getSourcePath($path), 0777, true); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function rmdir($path) { |
||
| 98 | |||
| 99 | public function opendir($path) { |
||
| 102 | |||
| 103 | public function is_dir($path) { |
||
| 109 | |||
| 110 | public function is_file($path) { |
||
| 113 | |||
| 114 | public function stat($path) { |
||
| 125 | |||
| 126 | public function filetype($path) { |
||
| 133 | |||
| 134 | public function filesize($path) { |
||
| 145 | |||
| 146 | public function isReadable($path) { |
||
| 149 | |||
| 150 | public function isUpdatable($path) { |
||
| 153 | |||
| 154 | public function file_exists($path) { |
||
| 157 | |||
| 158 | public function filemtime($path) { |
||
| 159 | clearstatcache($this->getSourcePath($path)); |
||
| 160 | return filemtime($this->getSourcePath($path)); |
||
| 161 | } |
||
| 162 | |||
| 163 | public function touch($path, $mtime = null) { |
||
| 164 | // sets the modification time of the file to the given value. |
||
| 165 | // If mtime is nil the current time is set. |
||
| 166 | // note that the access time of the file always changes to the current time. |
||
| 167 | if ($this->file_exists($path) and !$this->isUpdatable($path)) { |
||
| 168 | return false; |
||
| 169 | } |
||
| 170 | if (!is_null($mtime)) { |
||
| 171 | $result = touch($this->getSourcePath($path), $mtime); |
||
| 172 | } else { |
||
| 173 | $result = touch($this->getSourcePath($path)); |
||
| 174 | } |
||
| 175 | if ($result) { |
||
| 176 | clearstatcache(true, $this->getSourcePath($path)); |
||
| 177 | } |
||
| 178 | |||
| 179 | return $result; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function file_get_contents($path) { |
||
| 183 | // file_get_contents() has a memory leak: https://bugs.php.net/bug.php?id=61961 |
||
| 184 | $fileName = $this->getSourcePath($path); |
||
| 185 | |||
| 186 | $fileSize = filesize($fileName); |
||
| 187 | if ($fileSize === 0) { |
||
| 188 | return ''; |
||
| 189 | } |
||
| 190 | |||
| 191 | $handle = fopen($fileName,'rb'); |
||
| 192 | $content = fread($handle, $fileSize); |
||
| 193 | fclose($handle); |
||
| 194 | return $content; |
||
| 195 | } |
||
| 196 | |||
| 197 | public function file_put_contents($path, $data) { |
||
| 200 | |||
| 201 | View Code Duplication | public function unlink($path) { |
|
| 202 | if ($this->is_dir($path)) { |
||
| 203 | return $this->rmdir($path); |
||
| 204 | } else if ($this->is_file($path)) { |
||
| 205 | return unlink($this->getSourcePath($path)); |
||
| 206 | } else { |
||
| 207 | return false; |
||
| 208 | } |
||
| 209 | |||
| 210 | } |
||
| 211 | |||
| 212 | public function rename($path1, $path2) { |
||
| 252 | |||
| 253 | public function copy($path1, $path2) { |
||
| 260 | |||
| 261 | public function fopen($path, $mode) { |
||
| 264 | |||
| 265 | public function hash($type, $path, $raw = false) { |
||
| 268 | |||
| 269 | public function free_space($path) { |
||
| 284 | |||
| 285 | public function search($query) { |
||
| 288 | |||
| 289 | public function getLocalFile($path) { |
||
| 292 | |||
| 293 | public function getLocalFolder($path) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $query |
||
| 299 | * @param string $dir |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function searchInDir($query, $dir = '') { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * check if a file or folder has been updated since $time |
||
| 322 | * |
||
| 323 | * @param string $path |
||
| 324 | * @param int $time |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function hasUpdated($path, $time) { |
||
| 328 | if ($this->file_exists($path)) { |
||
| 329 | return $this->filemtime($path) > $time; |
||
| 330 | } else { |
||
| 331 | return true; |
||
| 332 | } |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Get the source path (on disk) of a given path |
||
| 337 | * |
||
| 338 | * @param string $path |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public function getSourcePath($path) { |
||
| 342 | $fullPath = $this->datadir . $path; |
||
| 343 | return $fullPath; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * {@inheritdoc} |
||
| 348 | */ |
||
| 349 | public function isLocal() { |
||
| 350 | return true; |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * get the ETag for a file or folder |
||
| 355 | * |
||
| 356 | * @param string $path |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getETag($path) { |
||
| 360 | if ($this->is_file($path)) { |
||
| 361 | $stat = $this->stat($path); |
||
| 362 | return md5( |
||
| 363 | $stat['mtime'] . |
||
| 364 | $stat['ino'] . |
||
| 365 | $stat['dev'] . |
||
| 366 | $stat['size'] |
||
| 367 | ); |
||
| 368 | } else { |
||
| 369 | return parent::getETag($path); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param \OCP\Files\Storage $sourceStorage |
||
| 375 | * @param string $sourceInternalPath |
||
| 376 | * @param string $targetInternalPath |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | View Code Duplication | public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 380 | if($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')){ |
||
| 381 | /** |
||
| 382 | * @var \OC\Files\Storage\Local $sourceStorage |
||
| 383 | */ |
||
| 384 | $rootStorage = new Local(['datadir' => '/']); |
||
| 385 | return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath)); |
||
| 386 | } else { |
||
| 387 | return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param \OCP\Files\Storage $sourceStorage |
||
| 393 | * @param string $sourceInternalPath |
||
| 394 | * @param string $targetInternalPath |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 408 | } |
||
| 409 |