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 | /** |
||
| 55 | * @param string $storage |
||
| 56 | * @param SharedMount[] $mountpoints |
||
| 57 | * @param array|null $arguments |
||
| 58 | * @param \OCP\Files\Storage\IStorageFactory $loader |
||
| 59 | */ |
||
| 60 | public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) { |
||
| 61 | $this->user = $arguments['user']; |
||
| 62 | $this->recipientView = new View('/' . $this->user . '/files'); |
||
| 63 | $newMountPoint = $this->verifyMountPoint($arguments['share'], $mountpoints); |
||
| 64 | $absMountPoint = '/' . $this->user . '/files' . $newMountPoint; |
||
| 65 | $arguments['ownerView'] = new View('/' . $arguments['share']['uid_owner'] . '/files'); |
||
| 66 | parent::__construct($storage, $absMountPoint, $arguments, $loader); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * check if the parent folder exists otherwise move the mount point up |
||
| 71 | * |
||
| 72 | * @param array $share |
||
| 73 | * @param SharedMount[] $mountpoints |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | private function verifyMountPoint(&$share, array $mountpoints) { |
||
| 77 | |||
| 78 | $mountPoint = basename($share['file_target']); |
||
| 79 | $parent = dirname($share['file_target']); |
||
| 80 | |||
| 81 | if (!$this->recipientView->is_dir($parent)) { |
||
| 82 | $parent = Helper::getShareFolder($this->recipientView); |
||
| 83 | } |
||
| 84 | |||
| 85 | $newMountPoint = $this->generateUniqueTarget( |
||
| 86 | \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), |
||
| 87 | $this->recipientView, |
||
| 88 | $mountpoints |
||
| 89 | ); |
||
| 90 | |||
| 91 | if ($newMountPoint !== $share['file_target']) { |
||
| 92 | $this->updateFileTarget($newMountPoint, $share); |
||
| 93 | $share['file_target'] = $newMountPoint; |
||
| 94 | $share['unique_name'] = true; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $newMountPoint; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $path |
||
| 102 | * @param View $view |
||
| 103 | * @param SharedMount[] $mountpoints |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | private function generateUniqueTarget($path, $view, array $mountpoints) { |
||
| 107 | $pathinfo = pathinfo($path); |
||
| 108 | $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : ''; |
||
| 109 | $name = $pathinfo['filename']; |
||
| 110 | $dir = $pathinfo['dirname']; |
||
| 111 | |||
| 112 | // Helper function to find existing mount points |
||
| 113 | $mountpointExists = function($path) use ($mountpoints) { |
||
| 114 | foreach ($mountpoints as $mountpoint) { |
||
| 115 | if ($mountpoint->getShare()['file_target'] === $path) { |
||
| 116 | return true; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | return false; |
||
| 120 | }; |
||
| 121 | |||
| 122 | $i = 2; |
||
| 123 | View Code Duplication | while ($view->file_exists($path) || $mountpointExists($path)) { |
|
| 124 | $path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext); |
||
| 125 | $i++; |
||
| 126 | } |
||
| 127 | |||
| 128 | return $path; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * update fileTarget in the database if the mount point changed |
||
| 133 | * |
||
| 134 | * @param string $newPath |
||
| 135 | * @param array $share reference to the share which should be modified |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | private function updateFileTarget($newPath, &$share) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Format a path to be relative to the /user/files/ directory |
||
| 165 | * |
||
| 166 | * @param string $path the absolute path |
||
| 167 | * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' |
||
| 168 | * @throws \OCA\Files_Sharing\Exceptions\BrokenPath |
||
| 169 | */ |
||
| 170 | protected function stripUserFilesPath($path) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Move the mount point to $target |
||
| 191 | * |
||
| 192 | * @param string $target the target mount point |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function moveMount($target) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Remove the mount points |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | public function removeMount() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function getShare() { |
||
| 247 | } |
||
| 248 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.