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 |
||
| 53 | class LocalFilesService { |
||
| 54 | |||
| 55 | |||
| 56 | /** @var IRootFolder */ |
||
| 57 | private $rootFolder; |
||
| 58 | |||
| 59 | /** @var IGroupManager */ |
||
| 60 | private $groupManager; |
||
| 61 | |||
| 62 | /** @var IUserManager */ |
||
| 63 | private $userManager; |
||
| 64 | |||
| 65 | /** @var IManager */ |
||
| 66 | private $shareManager; |
||
| 67 | |||
| 68 | /** @var SharesRequest */ |
||
| 69 | private $sharesRequest; |
||
| 70 | |||
| 71 | /** @var ConfigService */ |
||
| 72 | private $configService; |
||
| 73 | |||
| 74 | /** @var MiscService */ |
||
| 75 | private $miscService; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * LocalFilesService constructor. |
||
| 80 | * |
||
| 81 | * @param IRootFolder $rootFolder |
||
| 82 | * @param IGroupManager $groupManager |
||
| 83 | * @param IUserManager $userManager |
||
| 84 | * @param IManager $shareManager |
||
| 85 | * @param SharesRequest $sharesRequest |
||
| 86 | * @param ConfigService $configService |
||
| 87 | * @param MiscService $miscService |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function __construct( |
|
|
|
|||
| 90 | IRootFolder $rootFolder, IGroupManager $groupManager, IUserManager $userManager, |
||
| 91 | IManager $shareManager, SharesRequest $sharesRequest, ConfigService $configService, |
||
| 92 | MiscService $miscService |
||
| 93 | ) { |
||
| 94 | $this->rootFolder = $rootFolder; |
||
| 95 | $this->groupManager = $groupManager; |
||
| 96 | $this->userManager = $userManager; |
||
| 97 | $this->shareManager = $shareManager; |
||
| 98 | |||
| 99 | $this->sharesRequest = $sharesRequest; |
||
| 100 | $this->configService = $configService; |
||
| 101 | $this->miscService = $miscService; |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * @param Node $file |
||
| 107 | * @param string $source |
||
| 108 | * |
||
| 109 | * @throws KnownFileSourceException |
||
| 110 | */ |
||
| 111 | public function getFileSource(Node $file, string &$source) { |
||
| 112 | if ($file->getMountPoint() |
||
| 113 | ->getMountType() !== '') { |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | |||
| 117 | $source = ConfigService::FILES_LOCAL; |
||
| 118 | |||
| 119 | throw new KnownFileSourceException(); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * @param FilesDocument $document |
||
| 125 | * @param Node $file |
||
| 126 | */ |
||
| 127 | public function updateDocumentAccess(FilesDocument $document, Node $file) { |
||
| 128 | |||
| 129 | $ownerId = ''; |
||
| 130 | if ($file->getOwner() !== null) { |
||
| 131 | $ownerId = $file->getOwner() |
||
| 132 | ->getUID(); |
||
| 133 | } |
||
| 134 | |||
| 135 | $access = new DocumentAccess($ownerId); |
||
| 136 | |||
| 137 | $fileShares = new FileShares(); |
||
| 138 | $this->getSharesFromFile($file, $fileShares); |
||
| 139 | $access->setUsers($fileShares->getUsers()); |
||
| 140 | $access->setGroups($fileShares->getGroups()); |
||
| 141 | $access->setCircles($fileShares->getCircles()); |
||
| 142 | $access->setLinks($fileShares->getLinks()); |
||
| 143 | |||
| 144 | $document->setAccess($access); |
||
| 145 | } |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @param Node $file |
||
| 150 | * @param array $users |
||
| 151 | */ |
||
| 152 | public function getShareUsersFromFile(Node $file, array &$users) { |
||
| 153 | if ($file->getOwner() === null) { |
||
| 154 | return; |
||
| 155 | } |
||
| 156 | |||
| 157 | try { |
||
| 158 | $shares = $this->shareManager->getAccessList($file, true, true); |
||
| 159 | } catch (Exception $e) { |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | if (!array_key_exists('users', $shares)) { |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | foreach ($shares['users'] as $user => $node) { |
||
| 168 | if (in_array($user, $users) || $this->userManager->get($user) === null) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | array_push($users, $user); |
||
| 173 | } |
||
| 174 | |||
| 175 | } |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * same a getShareUsers, but we do it 'manually' |
||
| 180 | * |
||
| 181 | * @param DocumentAccess $access |
||
| 182 | * @param array $users |
||
| 183 | */ |
||
| 184 | public function getSharedUsersFromAccess(DocumentAccess $access, array &$users) { |
||
| 185 | |||
| 186 | $result = array_merge( |
||
| 187 | $access->getUsers(), |
||
| 188 | $this->getSharedUsersFromAccessGroups($access), |
||
| 189 | $this->getSharedUsersFromAccessCircles($access) |
||
| 190 | ); |
||
| 191 | |||
| 192 | foreach ($result as $user) { |
||
| 193 | if (!in_array($user, $users)) { |
||
| 194 | $users[] = $user; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param DocumentAccess $access |
||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | private function getSharedUsersFromAccessGroups(DocumentAccess $access): array { |
||
| 206 | |||
| 207 | $result = []; |
||
| 208 | $users = []; |
||
| 209 | foreach ($access->getGroups() as $groupName) { |
||
| 210 | $group = $this->groupManager->get($groupName); |
||
| 211 | if ($group === null) { |
||
| 212 | // TODO: set a warning |
||
| 213 | continue; |
||
| 214 | } |
||
| 215 | $users = array_merge($users, $group->getUsers()); |
||
| 216 | } |
||
| 217 | |||
| 218 | foreach ($users as $user) { |
||
| 219 | $result[] = $user->getUID(); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $result; |
||
| 223 | } |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * // TODO: get users from circles. |
||
| 228 | * |
||
| 229 | * @param DocumentAccess $access |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | private function getSharedUsersFromAccessCircles(DocumentAccess $access): array { |
||
| 234 | $result = []; |
||
| 235 | |||
| 236 | return $result; |
||
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * @param Node $file |
||
| 242 | * @param FileShares $fileShares |
||
| 243 | */ |
||
| 244 | private function getSharesFromFile(Node $file, FileShares $fileShares) { |
||
| 245 | |||
| 246 | if (strlen($file->getPath()) <= 1) { |
||
| 247 | return; |
||
| 248 | } |
||
| 249 | |||
| 250 | // we get shares from parent first |
||
| 251 | $this->getSharesFromFile($file->getParent(), $fileShares); |
||
| 252 | |||
| 253 | $shares = $this->sharesRequest->getFromFile($file); |
||
| 254 | foreach ($shares as $share) { |
||
| 255 | if ($share['parent'] !== null) { |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->parseUsersShares($share, $fileShares); |
||
| 260 | $this->parseUsersGroups($share, $fileShares); |
||
| 261 | $this->parseUsersCircles($share, $fileShares); |
||
| 262 | $this->parseUsersLinks($share, $fileShares); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | |||
| 267 | /** |
||
| 268 | * @param array $share |
||
| 269 | * @param FileShares $fileShares |
||
| 270 | */ |
||
| 271 | private function parseUsersShares(array $share, FileShares $fileShares) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * @param array $share |
||
| 282 | * @param FileShares $fileShares |
||
| 283 | */ |
||
| 284 | private function parseUsersGroups(array $share, FileShares $fileShares) { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $share |
||
| 295 | * @param FileShares $fileShares |
||
| 296 | */ |
||
| 297 | private function parseUsersCircles(array $share, FileShares $fileShares) { |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array $share |
||
| 308 | * @param FileShares $fileShares |
||
| 309 | */ |
||
| 310 | private function parseUsersLinks(array $share, FileShares $fileShares) { |
||
| 317 | |||
| 318 | } |
||
| 319 | |||
| 320 |
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.