Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 38 | class SharedMount extends MountPoint implements MoveableMount { |
||
| 39 | /** |
||
| 40 | * @var \OC\Files\Storage\Shared $storage |
||
| 41 | */ |
||
| 42 | protected $storage = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \OC\Files\View |
||
| 46 | */ |
||
| 47 | private $recipientView; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $user; |
||
| 53 | |||
| 54 | /** @var \OCP\Share\IShare */ |
||
| 55 | private $superShare; |
||
| 56 | |||
| 57 | /** @var \OCP\Share\IShare[] */ |
||
| 58 | private $groupedShares; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param string $storage |
||
| 62 | * @param SharedMount[] $mountpoints |
||
| 63 | * @param array|null $arguments |
||
| 64 | * @param \OCP\Files\Storage\IStorageFactory $loader |
||
| 65 | */ |
||
| 66 | public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * check if the parent folder exists otherwise move the mount point up |
||
| 81 | * |
||
| 82 | * @param \OCP\Share\IShare $share |
||
| 83 | * @param SharedMount[] $mountpoints |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * update fileTarget in the database if the mount point changed |
||
| 110 | * |
||
| 111 | * @param string $newPath |
||
| 112 | * @param \OCP\Share\IShare $share |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | private function updateFileTarget($newPath, &$share) { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $path |
||
| 127 | * @param View $view |
||
| 128 | * @param SharedMount[] $mountpoints |
||
| 129 | * @return mixed |
||
| 130 | */ |
||
| 131 | private function generateUniqueTarget($path, $view, array $mountpoints) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Format a path to be relative to the /user/files/ directory |
||
| 158 | * |
||
| 159 | * @param string $path the absolute path |
||
| 160 | * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' |
||
| 161 | * @throws \OCA\Files_Sharing\Exceptions\BrokenPath |
||
| 162 | */ |
||
| 163 | protected function stripUserFilesPath($path) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Move the mount point to $target |
||
| 184 | * |
||
| 185 | * @param string $target the target mount point |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function moveMount($target) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Remove the mount points |
||
| 210 | * |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function removeMount() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return \OCP\Share\IShare |
||
| 225 | */ |
||
| 226 | public function getShare() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Get the file id of the root of the storage |
||
| 232 | * |
||
| 233 | * @return int |
||
| 234 | */ |
||
| 235 | public function getStorageRootId() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return int |
||
| 241 | */ |
||
| 242 | public function getNumericStorageId() { |
||
| 251 | } |
||
| 252 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.