| Conditions | 17 |
| Paths | 66 |
| Total Lines | 65 |
| Code Lines | 39 |
| 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 |
||
| 124 | public function move($sourcePath, $destinationPath) { |
||
| 125 | if (!$this->fileView) { |
||
| 126 | throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup'); |
||
| 127 | } |
||
| 128 | |||
| 129 | $targetNodeExists = $this->nodeExists($destinationPath); |
||
| 130 | $sourceNode = $this->getNodeForPath($sourcePath); |
||
| 131 | if ($sourceNode instanceof \Sabre\DAV\ICollection && $targetNodeExists) { |
||
| 132 | throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode . ', target exists'); |
||
| 133 | } |
||
| 134 | list($sourceDir,) = \Sabre\DAV\URLUtil::splitPath($sourcePath); |
||
| 135 | list($destinationDir,) = \Sabre\DAV\URLUtil::splitPath($destinationPath); |
||
| 136 | |||
| 137 | $isMovableMount = false; |
||
| 138 | $sourceMount = $this->mountManager->find($this->fileView->getAbsolutePath($sourcePath)); |
||
| 139 | $internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath)); |
||
| 140 | if ($sourceMount instanceof MoveableMount && $internalPath === '') { |
||
| 141 | $isMovableMount = true; |
||
| 142 | } |
||
| 143 | |||
| 144 | try { |
||
| 145 | $sameFolder = ($sourceDir === $destinationDir); |
||
| 146 | // if we're overwriting or same folder |
||
| 147 | if ($targetNodeExists || $sameFolder) { |
||
| 148 | // note that renaming a share mount point is always allowed |
||
| 149 | if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) { |
||
| 150 | throw new \Sabre\DAV\Exception\Forbidden(); |
||
| 151 | } |
||
| 152 | } else { |
||
| 153 | if (!$this->fileView->isCreatable($destinationDir)) { |
||
| 154 | throw new \Sabre\DAV\Exception\Forbidden(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if (!$sameFolder) { |
||
| 159 | // moving to a different folder, source will be gone, like a deletion |
||
| 160 | // note that moving a share mount point is always allowed |
||
| 161 | if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) { |
||
| 162 | throw new \Sabre\DAV\Exception\Forbidden(); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | $fileName = basename($destinationPath); |
||
| 167 | if (!\OCP\Util::isValidFileName($fileName)) { |
||
| 168 | throw new \Sabre\DAV\Exception\BadRequest(); |
||
| 169 | } |
||
| 170 | |||
| 171 | $renameOkay = $this->fileView->rename($sourcePath, $destinationPath); |
||
| 172 | if (!$renameOkay) { |
||
| 173 | throw new \Sabre\DAV\Exception\Forbidden(''); |
||
| 174 | } |
||
| 175 | } catch (\OCP\Files\StorageNotAvailableException $e) { |
||
| 176 | throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); |
||
| 177 | } |
||
| 178 | |||
| 179 | // update properties |
||
| 180 | $query = \OC_DB::prepare('UPDATE `*PREFIX*properties` SET `propertypath` = ?' |
||
| 181 | . ' WHERE `userid` = ? AND `propertypath` = ?'); |
||
| 182 | $query->execute(array(\OC\Files\Filesystem::normalizePath($destinationPath), \OC_User::getUser(), |
||
| 183 | \OC\Files\Filesystem::normalizePath($sourcePath))); |
||
| 184 | |||
| 185 | $this->markDirty($sourceDir); |
||
| 186 | $this->markDirty($destinationDir); |
||
| 187 | |||
| 188 | } |
||
| 189 | |||
| 240 |
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.