Complex classes like ObjectTree 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 ObjectTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ObjectTree extends \Sabre\DAV\Tree { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \OC\Files\View |
||
| 40 | */ |
||
| 41 | protected $fileView; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \OCP\Files\Mount\IMountManager |
||
| 45 | */ |
||
| 46 | protected $mountManager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Creates the object |
||
| 50 | */ |
||
| 51 | 33 | public function __construct() { |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param \Sabre\DAV\INode $rootNode |
||
| 56 | * @param \OC\Files\View $view |
||
| 57 | * @param \OCP\Files\Mount\IMountManager $mountManager |
||
| 58 | */ |
||
| 59 | 33 | public function init(\Sabre\DAV\INode $rootNode, \OC\Files\View $view, \OCP\Files\Mount\IMountManager $mountManager) { |
|
| 64 | |||
| 65 | /** |
||
| 66 | * If the given path is a chunked file name, converts it |
||
| 67 | * to the real file name. Only applies if the OC-CHUNKED header |
||
| 68 | * is present. |
||
| 69 | * |
||
| 70 | * @param string $path chunk file path to convert |
||
| 71 | * |
||
| 72 | * @return string path to real file |
||
| 73 | */ |
||
| 74 | 21 | private function resolveChunkFile($path) { |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Returns the INode object for the requested path |
||
| 96 | * |
||
| 97 | * @param string $path |
||
| 98 | * @throws \Sabre\DAV\Exception\ServiceUnavailable |
||
| 99 | * @throws \Sabre\DAV\Exception\NotFound |
||
| 100 | * @return \Sabre\DAV\INode |
||
| 101 | */ |
||
| 102 | 23 | public function getNodeForPath($path) { |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Moves a file from one location to another |
||
| 175 | * |
||
| 176 | * @param string $sourcePath The path to the file which should be moved |
||
| 177 | * @param string $destinationPath The full destination path, so not just the destination parent node |
||
| 178 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 179 | * @throws \Sabre\DAV\Exception\ServiceUnavailable |
||
| 180 | * @throws \Sabre\DAV\Exception\Forbidden |
||
| 181 | * @return int |
||
| 182 | */ |
||
| 183 | 10 | public function move($sourcePath, $destinationPath) { |
|
| 184 | 10 | if (!$this->fileView) { |
|
| 185 | throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup'); |
||
| 186 | } |
||
| 187 | |||
| 188 | 10 | $targetNodeExists = $this->nodeExists($destinationPath); |
|
| 189 | 10 | $sourceNode = $this->getNodeForPath($sourcePath); |
|
| 190 | 10 | if ($sourceNode instanceof \Sabre\DAV\ICollection && $targetNodeExists) { |
|
| 191 | 1 | throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists'); |
|
| 192 | } |
||
| 193 | 9 | list($sourceDir,) = \Sabre\HTTP\URLUtil::splitPath($sourcePath); |
|
| 194 | 9 | list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destinationPath); |
|
| 195 | |||
| 196 | 9 | $isMovableMount = false; |
|
| 197 | 9 | $sourceMount = $this->mountManager->find($this->fileView->getAbsolutePath($sourcePath)); |
|
| 198 | 9 | $internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath)); |
|
| 199 | 9 | if ($sourceMount instanceof MoveableMount && $internalPath === '') { |
|
| 200 | $isMovableMount = true; |
||
| 201 | } |
||
| 202 | |||
| 203 | try { |
||
| 204 | 9 | $sameFolder = ($sourceDir === $destinationDir); |
|
| 205 | // if we're overwriting or same folder |
||
| 206 | 9 | if ($targetNodeExists || $sameFolder) { |
|
| 207 | // note that renaming a share mount point is always allowed |
||
| 208 | 3 | if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) { |
|
| 209 | 2 | throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 210 | } |
||
| 211 | 1 | } else { |
|
| 212 | 6 | if (!$this->fileView->isCreatable($destinationDir)) { |
|
| 213 | 3 | throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | 4 | if (!$sameFolder) { |
|
| 218 | // moving to a different folder, source will be gone, like a deletion |
||
| 219 | // note that moving a share mount point is always allowed |
||
| 220 | 3 | if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) { |
|
| 221 | 1 | throw new \Sabre\DAV\Exception\Forbidden(); |
|
| 222 | } |
||
| 223 | 2 | } |
|
| 224 | |||
| 225 | 3 | $fileName = basename($destinationPath); |
|
| 226 | try { |
||
| 227 | 3 | $this->fileView->verifyPath($destinationDir, $fileName); |
|
| 228 | 3 | } catch (\OCP\Files\InvalidPathException $ex) { |
|
| 229 | 1 | throw new InvalidPath($ex->getMessage()); |
|
| 230 | } |
||
| 231 | |||
| 232 | 2 | $renameOkay = $this->fileView->rename($sourcePath, $destinationPath); |
|
| 233 | 2 | if (!$renameOkay) { |
|
| 234 | throw new \Sabre\DAV\Exception\Forbidden(''); |
||
| 235 | } |
||
| 236 | 9 | } catch (StorageNotAvailableException $e) { |
|
| 237 | throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
||
| 238 | 7 | } catch (LockedException $e) { |
|
| 239 | throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
||
| 240 | } |
||
| 241 | |||
| 242 | 2 | $this->markDirty($sourceDir); |
|
| 243 | 2 | $this->markDirty($destinationDir); |
|
| 244 | |||
| 245 | 2 | } |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Copies a file or directory. |
||
| 249 | * |
||
| 250 | * This method must work recursively and delete the destination |
||
| 251 | * if it exists |
||
| 252 | * |
||
| 253 | * @param string $source |
||
| 254 | * @param string $destination |
||
| 255 | * @throws \Sabre\DAV\Exception\ServiceUnavailable |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | public function copy($source, $destination) { |
||
| 284 | } |
||
| 285 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.