| Conditions | 14 |
| Paths | 291 |
| Total Lines | 60 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 43 | public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDescending = false) { |
||
| 44 | $result = array(); |
||
| 45 | $timestamp = null; |
||
| 46 | |||
| 47 | $view = new \OC\Files\View('/' . $user . '/files_trashbin/files'); |
||
| 48 | |||
| 49 | if (ltrim($dir, '/') !== '' && !$view->is_dir($dir)) { |
||
| 50 | throw new \Exception('Directory does not exists'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $mount = $view->getMount($dir); |
||
| 54 | $storage = $mount->getStorage(); |
||
| 55 | $absoluteDir = $view->getAbsolutePath($dir); |
||
| 56 | $internalPath = $mount->getInternalPath($absoluteDir); |
||
| 57 | |||
| 58 | $originalLocations = \OCA\Files_Trashbin\Trashbin::getLocations($user); |
||
| 59 | $dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir))); |
||
| 60 | foreach ($dirContent as $entry) { |
||
| 61 | $entryName = $entry->getName(); |
||
| 62 | $id = $entry->getId(); |
||
| 63 | $name = $entryName; |
||
| 64 | if ($dir === '' || $dir === '/') { |
||
| 65 | $pathparts = pathinfo($entryName); |
||
| 66 | $timestamp = substr($pathparts['extension'], 1); |
||
| 67 | $name = $pathparts['filename']; |
||
| 68 | |||
| 69 | } else if ($timestamp === null) { |
||
| 70 | // for subfolders we need to calculate the timestamp only once |
||
| 71 | $parts = explode('/', ltrim($dir, '/')); |
||
| 72 | $timestamp = substr(pathinfo($parts[0], PATHINFO_EXTENSION), 1); |
||
| 73 | } |
||
| 74 | $originalPath = ''; |
||
| 75 | if (isset($originalLocations[$id][$timestamp])) { |
||
| 76 | $originalPath = $originalLocations[$id][$timestamp]; |
||
| 77 | if (substr($originalPath, -1) === '/') { |
||
| 78 | $originalPath = substr($originalPath, 0, -1); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | $type = $entry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE ? 'dir' : 'file'; |
||
| 82 | $i = array( |
||
| 83 | 'name' => $name, |
||
| 84 | 'mtime' => $timestamp, |
||
| 85 | 'mimetype' => $type === 'dir' ? 'httpd/unix-directory' : \OC::$server->getMimeTypeDetector()->detectPath($name), |
||
| 86 | 'type' => $type, |
||
| 87 | 'directory' => ($dir === '/') ? '' : $dir, |
||
| 88 | 'size' => $entry->getSize(), |
||
| 89 | 'etag' => '', |
||
| 90 | 'permissions' => Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE |
||
| 91 | ); |
||
| 92 | if ($originalPath) { |
||
| 93 | $i['extraData'] = $originalPath . '/' . $id; |
||
| 94 | } |
||
| 95 | $result[] = new FileInfo($absoluteDir . '/' . $i['name'], $storage, $internalPath . '/' . $i['name'], $i, $mount); |
||
|
|
|||
| 96 | } |
||
| 97 | |||
| 98 | if ($sortAttribute !== '') { |
||
| 99 | return \OCA\Files\Helper::sortFiles($result, $sortAttribute, $sortDescending); |
||
| 100 | } |
||
| 101 | return $result; |
||
| 102 | } |
||
| 103 | |||
| 122 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: