| Total Complexity | 63 |
| Total Lines | 383 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Node 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.
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 Node, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | abstract class Node implements \Sabre\DAV\INode { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \OC\Files\View |
||
| 54 | */ |
||
| 55 | protected $fileView; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The path to the current node |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $path; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * node properties cache |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $property_cache = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \OCP\Files\FileInfo |
||
| 73 | */ |
||
| 74 | protected $info; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var IManager |
||
| 78 | */ |
||
| 79 | protected $shareManager; |
||
| 80 | |||
| 81 | protected \OCP\Files\Node $node; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Sets up the node, expects a full path name |
||
| 85 | * |
||
| 86 | * @param \OC\Files\View $view |
||
| 87 | * @param \OCP\Files\FileInfo $info |
||
| 88 | * @param IManager $shareManager |
||
| 89 | */ |
||
| 90 | public function __construct(View $view, FileInfo $info, IManager $shareManager = null) { |
||
| 91 | $this->fileView = $view; |
||
| 92 | $this->path = $this->fileView->getRelativePath($info->getPath()); |
||
| 93 | $this->info = $info; |
||
| 94 | if ($shareManager) { |
||
| 95 | $this->shareManager = $shareManager; |
||
| 96 | } else { |
||
| 97 | $this->shareManager = \OC::$server->getShareManager(); |
||
| 98 | } |
||
| 99 | if ($info instanceof Folder || $info instanceof File) { |
||
| 100 | $this->node = $info; |
||
| 101 | } else { |
||
| 102 | $root = \OC::$server->get(IRootFolder::class); |
||
| 103 | if ($info->getType() === FileInfo::TYPE_FOLDER) { |
||
| 104 | $this->node = new Folder($root, $view, $this->path, $info); |
||
| 105 | } else { |
||
| 106 | $this->node = new File($root, $view, $this->path, $info); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function refreshInfo() { |
||
| 112 | $this->info = $this->fileView->getFileInfo($this->path); |
||
| 113 | $root = \OC::$server->get(IRootFolder::class); |
||
| 114 | if ($this->info->getType() === FileInfo::TYPE_FOLDER) { |
||
| 115 | $this->node = new Folder($root, $this->fileView, $this->path, $this->info); |
||
| 116 | } else { |
||
| 117 | $this->node = new File($root, $this->fileView, $this->path, $this->info); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns the name of the node |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getName() { |
||
| 127 | return $this->info->getName(); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns the full path |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function getPath() { |
||
| 136 | return $this->path; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Renames the node |
||
| 141 | * |
||
| 142 | * @param string $name The new name |
||
| 143 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 144 | * @throws \Sabre\DAV\Exception\Forbidden |
||
| 145 | */ |
||
| 146 | public function setName($name) { |
||
| 147 | |||
| 148 | // rename is only allowed if the update privilege is granted |
||
| 149 | if (!($this->info->isUpdateable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) { |
||
| 150 | throw new \Sabre\DAV\Exception\Forbidden(); |
||
| 151 | } |
||
| 152 | |||
| 153 | [$parentPath,] = \Sabre\Uri\split($this->path); |
||
| 154 | [, $newName] = \Sabre\Uri\split($name); |
||
| 155 | |||
| 156 | // verify path of the target |
||
| 157 | $this->verifyPath(); |
||
| 158 | |||
| 159 | $newPath = $parentPath . '/' . $newName; |
||
| 160 | |||
| 161 | if (!$this->fileView->rename($this->path, $newPath)) { |
||
| 162 | throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath); |
||
| 163 | } |
||
| 164 | |||
| 165 | $this->path = $newPath; |
||
| 166 | |||
| 167 | $this->refreshInfo(); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function setPropertyCache($property_cache) { |
||
| 171 | $this->property_cache = $property_cache; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the last modification time, as a unix timestamp |
||
| 176 | * |
||
| 177 | * @return int timestamp as integer |
||
| 178 | */ |
||
| 179 | public function getLastModified() { |
||
| 180 | $timestamp = $this->info->getMtime(); |
||
| 181 | if (!empty($timestamp)) { |
||
| 182 | return (int)$timestamp; |
||
| 183 | } |
||
| 184 | return $timestamp; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * sets the last modification time of the file (mtime) to the value given |
||
| 189 | * in the second parameter or to now if the second param is empty. |
||
| 190 | * Even if the modification time is set to a custom value the access time is set to now. |
||
| 191 | */ |
||
| 192 | public function touch($mtime) { |
||
| 193 | $mtime = $this->sanitizeMtime($mtime); |
||
| 194 | $this->fileView->touch($this->path, $mtime); |
||
| 195 | $this->refreshInfo(); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the ETag for a file |
||
| 200 | * |
||
| 201 | * An ETag is a unique identifier representing the current version of the |
||
| 202 | * file. If the file changes, the ETag MUST change. The ETag is an |
||
| 203 | * arbitrary string, but MUST be surrounded by double-quotes. |
||
| 204 | * |
||
| 205 | * Return null if the ETag can not effectively be determined |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function getETag() { |
||
| 210 | return '"' . $this->info->getEtag() . '"'; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Sets the ETag |
||
| 215 | * |
||
| 216 | * @param string $etag |
||
| 217 | * |
||
| 218 | * @return int file id of updated file or -1 on failure |
||
| 219 | */ |
||
| 220 | public function setETag($etag) { |
||
| 221 | return $this->fileView->putFileInfo($this->path, ['etag' => $etag]); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function setCreationTime(int $time) { |
||
| 225 | return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]); |
||
| 226 | } |
||
| 227 | |||
| 228 | public function setUploadTime(int $time) { |
||
| 229 | return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns the size of the node, in bytes |
||
| 234 | * |
||
| 235 | * @return integer |
||
| 236 | */ |
||
| 237 | public function getSize() { |
||
| 238 | return $this->info->getSize(); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the cache's file id |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function getId() { |
||
| 247 | return $this->info->getId(); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string|null |
||
| 252 | */ |
||
| 253 | public function getFileId() { |
||
| 254 | if ($this->info->getId()) { |
||
| 255 | $instanceId = \OC_Util::getInstanceId(); |
||
| 256 | $id = sprintf('%08d', $this->info->getId()); |
||
| 257 | return $id . $instanceId; |
||
| 258 | } |
||
| 259 | |||
| 260 | return null; |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return integer |
||
| 265 | */ |
||
| 266 | public function getInternalFileId() { |
||
| 267 | return $this->info->getId(); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $user |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | public function getSharePermissions($user) { |
||
| 275 | |||
| 276 | // check of we access a federated share |
||
| 277 | if ($user !== null) { |
||
| 278 | try { |
||
| 279 | $share = $this->shareManager->getShareByToken($user); |
||
| 280 | return $share->getPermissions(); |
||
| 281 | } catch (ShareNotFound $e) { |
||
| 282 | // ignore |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | try { |
||
| 287 | $storage = $this->info->getStorage(); |
||
| 288 | } catch (StorageNotAvailableException $e) { |
||
| 289 | $storage = null; |
||
| 290 | } |
||
| 291 | |||
| 292 | if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
||
| 293 | /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
||
| 294 | $permissions = (int)$storage->getShare()->getPermissions(); |
||
| 295 | } else { |
||
| 296 | $permissions = $this->info->getPermissions(); |
||
| 297 | } |
||
| 298 | |||
| 299 | /* |
||
| 300 | * We can always share non moveable mount points with DELETE and UPDATE |
||
| 301 | * Eventually we need to do this properly |
||
| 302 | */ |
||
| 303 | $mountpoint = $this->info->getMountPoint(); |
||
| 304 | if (!($mountpoint instanceof MoveableMount)) { |
||
| 305 | $mountpointpath = $mountpoint->getMountPoint(); |
||
| 306 | if (substr($mountpointpath, -1) === '/') { |
||
| 307 | $mountpointpath = substr($mountpointpath, 0, -1); |
||
| 308 | } |
||
| 309 | |||
| 310 | if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) { |
||
| 311 | $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /* |
||
| 316 | * Files can't have create or delete permissions |
||
| 317 | */ |
||
| 318 | if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) { |
||
| 319 | $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE); |
||
| 320 | } |
||
| 321 | |||
| 322 | return $permissions; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $user |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getNoteFromShare($user) { |
||
| 330 | if ($user === null) { |
||
| 331 | return ''; |
||
| 332 | } |
||
| 333 | |||
| 334 | $types = [ |
||
| 335 | IShare::TYPE_USER, |
||
| 336 | IShare::TYPE_GROUP, |
||
| 337 | IShare::TYPE_CIRCLE, |
||
| 338 | IShare::TYPE_ROOM |
||
| 339 | ]; |
||
| 340 | |||
| 341 | foreach ($types as $shareType) { |
||
| 342 | $shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1); |
||
| 343 | foreach ($shares as $share) { |
||
| 344 | $note = $share->getNote(); |
||
| 345 | if ($share->getShareOwner() !== $user && !empty($note)) { |
||
| 346 | return $note; |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | return ''; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function getDavPermissions() { |
||
| 387 | } |
||
| 388 | |||
| 389 | public function getOwner() { |
||
| 390 | return $this->info->getOwner(); |
||
| 391 | } |
||
| 392 | |||
| 393 | protected function verifyPath() { |
||
| 394 | try { |
||
| 395 | $fileName = basename($this->info->getPath()); |
||
| 396 | $this->fileView->verifyPath($this->path, $fileName); |
||
| 397 | } catch (\OCP\Files\InvalidPathException $ex) { |
||
| 398 | throw new InvalidPath($ex->getMessage()); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 404 | */ |
||
| 405 | public function acquireLock($type) { |
||
| 406 | $this->fileView->lockFile($this->path, $type); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 411 | */ |
||
| 412 | public function releaseLock($type) { |
||
| 413 | $this->fileView->unlockFile($this->path, $type); |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 418 | */ |
||
| 419 | public function changeLock($type) { |
||
| 421 | } |
||
| 422 | |||
| 423 | public function getFileInfo() { |
||
| 424 | return $this->info; |
||
| 425 | } |
||
| 426 | |||
| 427 | public function getNode(): \OCP\Files\Node { |
||
| 429 | } |
||
| 430 | |||
| 431 | protected function sanitizeMtime($mtimeFromRequest) { |
||
| 433 | } |
||
| 434 | } |
||
| 435 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: