| Conditions | 15 |
| Paths | 31 |
| Total Lines | 72 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 110 | public function getNodeForPath($path) { |
||
| 111 | if (!$this->fileView) { |
||
| 112 | throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $path = trim($path, '/'); |
||
| 116 | |||
| 117 | if (isset($this->cache[$path])) { |
||
| 118 | return $this->cache[$path]; |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($path) { |
||
| 122 | try { |
||
| 123 | $this->fileView->verifyPath($path, basename($path)); |
||
| 124 | } catch (\OCP\Files\InvalidPathException $ex) { |
||
| 125 | throw new InvalidPath($ex->getMessage()); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // Is it the root node? |
||
| 130 | if (!strlen($path)) { |
||
| 131 | return $this->rootNode; |
||
| 132 | } |
||
| 133 | |||
| 134 | if (pathinfo($path, PATHINFO_EXTENSION) === 'part') { |
||
| 135 | // read from storage |
||
| 136 | $absPath = $this->fileView->getAbsolutePath($path); |
||
| 137 | $mount = $this->fileView->getMount($path); |
||
| 138 | $storage = $mount->getStorage(); |
||
| 139 | $internalPath = $mount->getInternalPath($absPath); |
||
| 140 | if ($storage && $storage->file_exists($internalPath)) { |
||
| 141 | /** |
||
| 142 | * @var \OC\Files\Storage\Storage $storage |
||
| 143 | */ |
||
| 144 | // get data directly |
||
| 145 | $data = $storage->getMetaData($internalPath); |
||
| 146 | $info = new FileInfo($absPath, $storage, $internalPath, $data, $mount); |
||
| 147 | } else { |
||
| 148 | $info = null; |
||
| 149 | } |
||
| 150 | } else { |
||
| 151 | // resolve chunk file name to real name, if applicable |
||
| 152 | $path = $this->resolveChunkFile($path); |
||
| 153 | |||
| 154 | // read from cache |
||
| 155 | try { |
||
| 156 | $info = $this->fileView->getFileInfo($path); |
||
| 157 | } catch (StorageNotAvailableException $e) { |
||
| 158 | throw new \Sabre\DAV\Exception\ServiceUnavailable('Storage is temporarily not available'); |
||
| 159 | } catch (StorageInvalidException $e) { |
||
| 160 | throw new \Sabre\DAV\Exception\NotFound('Storage ' . $path . ' is invalid'); |
||
| 161 | } catch (LockedException $e) { |
||
| 162 | throw new \Sabre\DAV\Exception\Locked(); |
||
| 163 | } catch (ForbiddenException $e) { |
||
| 164 | throw new \Sabre\DAV\Exception\Forbidden(); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | if (!$info) { |
||
| 169 | throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located'); |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($info->getType() === 'dir') { |
||
| 173 | $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this); |
||
| 174 | } else { |
||
| 175 | $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info); |
||
| 176 | } |
||
| 177 | |||
| 178 | $this->cache[$path] = $node; |
||
| 179 | return $node; |
||
| 180 | |||
| 181 | } |
||
| 182 | |||
| 241 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: