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 |
||
| 55 | class ApiController extends Controller { |
||
| 56 | /** @var TagService */ |
||
| 57 | private $tagService; |
||
| 58 | /** @var IManager * */ |
||
| 59 | private $shareManager; |
||
| 60 | /** @var IPreview */ |
||
| 61 | private $previewManager; |
||
| 62 | /** IUserSession */ |
||
| 63 | private $userSession; |
||
| 64 | /** IConfig */ |
||
| 65 | private $config; |
||
| 66 | /** @var Folder */ |
||
| 67 | private $userFolder; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string $appName |
||
| 71 | * @param IRequest $request |
||
| 72 | * @param IUserSession $userSession |
||
| 73 | * @param TagService $tagService |
||
| 74 | * @param IPreview $previewManager |
||
| 75 | * @param IManager $shareManager |
||
| 76 | * @param IConfig $config |
||
| 77 | * @param Folder $userFolder |
||
| 78 | */ |
||
| 79 | public function __construct($appName, |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Gets a thumbnail of the specified file |
||
| 98 | * |
||
| 99 | * @since API version 1.0 |
||
| 100 | * |
||
| 101 | * @NoAdminRequired |
||
| 102 | * @NoCSRFRequired |
||
| 103 | * @StrictCookieRequired |
||
| 104 | * |
||
| 105 | * @param int $x |
||
| 106 | * @param int $y |
||
| 107 | * @param string $file URL-encoded filename |
||
| 108 | * @return DataResponse|FileDisplayResponse |
||
| 109 | */ |
||
| 110 | public function getThumbnail($x, $y, $file) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Updates the info of the specified file path |
||
| 134 | * The passed tags are absolute, which means they will |
||
| 135 | * replace the actual tag selection. |
||
| 136 | * |
||
| 137 | * @NoAdminRequired |
||
| 138 | * |
||
| 139 | * @param string $path path |
||
| 140 | * @param array|string $tags array of tags |
||
| 141 | * @return DataResponse |
||
| 142 | */ |
||
| 143 | public function updateFileTags($path, $tags = null) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param \OCP\Files\Node[] $nodes |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | private function formatNodes(array $nodes) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns a list of recently modifed files. |
||
| 191 | * |
||
| 192 | * @NoAdminRequired |
||
| 193 | * |
||
| 194 | * @return DataResponse |
||
| 195 | */ |
||
| 196 | public function getRecentFiles() { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Returns a list of favorites modifed folder. |
||
| 204 | * |
||
| 205 | * @NoAdminRequired |
||
| 206 | * |
||
| 207 | * @return DataResponse |
||
| 208 | */ |
||
| 209 | public function getFavoritesFolder() { |
||
| 210 | $nodes = $this->userFolder->searchByTag('_$!<Favorite>!$_', $this->userSession->getUser()->getUID()); |
||
| 211 | |||
| 212 | $favorites = []; |
||
| 213 | $i = 0; |
||
| 214 | foreach ($nodes as &$node) { |
||
| 215 | |||
| 216 | $favorites[$i]['id'] = $node->getId(); |
||
| 217 | $favorites[$i]['name'] = $node->getName(); |
||
| 218 | $favorites[$i]['path'] = $node->getInternalPath(); |
||
| 219 | $favorites[$i]['mtime'] = $node->getMTime(); |
||
| 220 | $i++; |
||
| 221 | } |
||
| 222 | |||
| 223 | return new DataResponse(['favoriteFolders' => $favorites]); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return a list of share types for outgoing shares |
||
| 228 | * |
||
| 229 | * @param Node $node file node |
||
| 230 | * |
||
| 231 | * @return int[] array of share types |
||
| 232 | */ |
||
| 233 | View Code Duplication | private function getShareTypes(Node $node) { |
|
| 234 | $userId = $this->userSession->getUser()->getUID(); |
||
| 235 | $shareTypes = []; |
||
| 236 | $requestedShareTypes = [ |
||
| 237 | \OCP\Share::SHARE_TYPE_USER, |
||
| 238 | \OCP\Share::SHARE_TYPE_GROUP, |
||
| 239 | \OCP\Share::SHARE_TYPE_LINK, |
||
| 240 | \OCP\Share::SHARE_TYPE_REMOTE, |
||
| 241 | \OCP\Share::SHARE_TYPE_EMAIL |
||
| 242 | ]; |
||
| 243 | foreach ($requestedShareTypes as $requestedShareType) { |
||
| 244 | // one of each type is enough to find out about the types |
||
| 245 | $shares = $this->shareManager->getSharesBy( |
||
| 246 | $userId, |
||
| 247 | $requestedShareType, |
||
| 248 | $node, |
||
| 249 | false, |
||
| 250 | 1 |
||
| 251 | ); |
||
| 252 | if (!empty($shares)) { |
||
| 253 | $shareTypes[] = $requestedShareType; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | return $shareTypes; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Change the default sort mode |
||
| 261 | * |
||
| 262 | * @NoAdminRequired |
||
| 263 | * |
||
| 264 | * @param string $mode |
||
| 265 | * @param string $direction |
||
| 266 | * @return Response |
||
| 267 | */ |
||
| 268 | public function updateFileSorting($mode, $direction) { |
||
| 269 | $allowedMode = ['name', 'size', 'mtime']; |
||
| 270 | $allowedDirection = ['asc', 'desc']; |
||
| 271 | if (!in_array($mode, $allowedMode) || !in_array($direction, $allowedDirection)) { |
||
| 272 | $response = new Response(); |
||
| 273 | $response->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY); |
||
| 274 | return $response; |
||
| 275 | } |
||
| 276 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting', $mode); |
||
| 277 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'file_sorting_direction', $direction); |
||
| 278 | return new Response(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Toggle default for showing/hiding hidden files |
||
| 283 | * |
||
| 284 | * @NoAdminRequired |
||
| 285 | * |
||
| 286 | * @param bool $show |
||
| 287 | */ |
||
| 288 | public function showHiddenFiles($show) { |
||
| 289 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', (int)$show); |
||
| 290 | return new Response(); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Toggle default for showing/hiding QuickAccess folder |
||
| 295 | * |
||
| 296 | * @NoAdminRequired |
||
| 297 | * |
||
| 298 | * @param bool $show |
||
| 299 | * |
||
| 300 | * @return Response |
||
| 301 | */ |
||
| 302 | public function showQuickAccess($show) { |
||
| 303 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'show_Quick_Access', (int)$show); |
||
| 304 | return new Response(); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Toggle default for showing/hiding QuickAccess folder |
||
| 309 | * |
||
| 310 | * @NoAdminRequired |
||
| 311 | * |
||
| 312 | * @return String |
||
| 313 | */ |
||
| 314 | public function getShowQuickAccess() { |
||
| 315 | |||
| 316 | return $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_Quick_Access', 0); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * quickaccess-sorting-strategy |
||
| 321 | * |
||
| 322 | * @NoAdminRequired |
||
| 323 | * |
||
| 324 | * @param string $strategy |
||
| 325 | * @return Response |
||
| 326 | */ |
||
| 327 | public function setSortingStrategy($strategy) { |
||
| 328 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_sorting_strategy', (String)$strategy); |
||
| 329 | return new Response(); |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get reverse-state for quickaccess-list |
||
| 334 | * |
||
| 335 | * @NoAdminRequired |
||
| 336 | * |
||
| 337 | * @return String |
||
| 338 | */ |
||
| 339 | public function getSortingStrategy() { |
||
| 340 | return $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_sorting_strategy', 'alphabet'); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Toggle for reverse quickaccess-list |
||
| 345 | * |
||
| 346 | * @NoAdminRequired |
||
| 347 | * |
||
| 348 | * @param bool $reverse |
||
| 349 | * @return Response |
||
| 350 | */ |
||
| 351 | public function setReverseQuickaccess($reverse) { |
||
| 352 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_reverse_list', (int)$reverse); |
||
| 353 | return new Response(); |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get reverse-state for quickaccess-list |
||
| 358 | * |
||
| 359 | * @NoAdminRequired |
||
| 360 | * |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | public function getReverseQuickaccess() { |
||
| 364 | if ($this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_reverse_list', false)) { |
||
| 365 | return true; |
||
| 366 | } |
||
| 367 | return false; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set state for show sorting menu |
||
| 372 | * |
||
| 373 | * @NoAdminRequired |
||
| 374 | * |
||
| 375 | * @param bool $show |
||
| 376 | * @return Response |
||
| 377 | */ |
||
| 378 | public function setShowQuickaccessSettings($show) { |
||
| 379 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_show_settings', (int)$show); |
||
| 380 | return new Response(); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get state for show sorting menu |
||
| 385 | * |
||
| 386 | * @NoAdminRequired |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | public function getShowQuickaccessSettings() { |
||
| 391 | if ($this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_show_settings', false)) { |
||
| 392 | return true; |
||
| 393 | } |
||
| 394 | return false; |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Set sorting-order for custom sorting |
||
| 399 | * |
||
| 400 | * @NoAdminRequired |
||
| 401 | * |
||
| 402 | * @param String $order |
||
| 403 | * @return Response |
||
| 404 | */ |
||
| 405 | public function setSortingOrder($order) { |
||
| 406 | $this->config->setUserValue($this->userSession->getUser()->getUID(), 'files', 'quickaccess_custom_sorting_order', (String)$order); |
||
| 407 | return new Response(); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get sorting-order for custom sorting |
||
| 412 | * |
||
| 413 | * @NoAdminRequired |
||
| 414 | * |
||
| 415 | * @return String |
||
| 416 | */ |
||
| 417 | public function getSortingOrder() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get sorting-order for custom sorting |
||
| 423 | * |
||
| 424 | * @NoAdminRequired |
||
| 425 | * |
||
| 426 | * @param String |
||
| 427 | * @return String |
||
| 428 | */ |
||
| 429 | public function getNodeType($folderpath) { |
||
| 430 | $node = $this->userFolder->get($folderpath); |
||
| 431 | return $node->getType(); |
||
| 432 | } |
||
| 433 | |||
| 434 | |||
| 435 | } |
||
| 436 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.