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:
Complex classes like FilesService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FilesService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class FilesService { |
||
| 54 | |||
| 55 | const DOCUMENT_TYPE = 'files'; |
||
| 56 | |||
| 57 | const MIMETYPE_TEXT = 'files_text'; |
||
| 58 | const MIMETYPE_PDF = 'files_pdf'; |
||
| 59 | const MIMETYPE_OFFICE = 'files_office'; |
||
| 60 | const MIMETYPE_IMAGE = 'files_image'; |
||
| 61 | const MIMETYPE_AUDIO = 'files_audio'; |
||
| 62 | |||
| 63 | |||
| 64 | /** @var IRootFolder */ |
||
| 65 | private $rootFolder; |
||
| 66 | |||
| 67 | /** @var IUserManager */ |
||
| 68 | private $userManager; |
||
| 69 | |||
| 70 | /** @var IManager */ |
||
| 71 | private $shareManager; |
||
| 72 | |||
| 73 | /** @var ConfigService */ |
||
| 74 | private $configService; |
||
| 75 | |||
| 76 | /** @var ExternalFilesService */ |
||
| 77 | private $externalFilesService; |
||
| 78 | |||
| 79 | /** @var MiscService */ |
||
| 80 | private $miscService; |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * FilesService constructor. |
||
| 85 | * |
||
| 86 | * @param IRootFolder $rootFolder |
||
| 87 | * @param IUserManager $userManager |
||
| 88 | * @param IManager $shareManager |
||
| 89 | * @param ConfigService $configService |
||
| 90 | * @param ExternalFilesService $externalFilesService |
||
| 91 | * @param MiscService $miscService |
||
| 92 | * |
||
| 93 | * @internal param IProviderFactory $factory |
||
| 94 | */ |
||
| 95 | public function __construct( |
||
| 96 | IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager, |
||
| 97 | ConfigService $configService, ExternalFilesService $externalFilesService, |
||
| 98 | MiscService $miscService |
||
| 99 | ) { |
||
| 100 | $this->rootFolder = $rootFolder; |
||
| 101 | $this->userManager = $userManager; |
||
| 102 | $this->shareManager = $shareManager; |
||
| 103 | |||
| 104 | $this->configService = $configService; |
||
| 105 | $this->externalFilesService = $externalFilesService; |
||
| 106 | $this->miscService = $miscService; |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * @param Runner $runner |
||
| 112 | * @param string $userId |
||
| 113 | * |
||
| 114 | * @return FilesDocument[] |
||
| 115 | * @throws InterruptException |
||
| 116 | * @throws InvalidPathException |
||
| 117 | * @throws NotFoundException |
||
| 118 | * @throws TickDoesNotExistException |
||
| 119 | */ |
||
| 120 | public function getFilesFromUser(Runner $runner, $userId) { |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * @param Runner $runner |
||
| 135 | * @param string $userId |
||
| 136 | * @param Folder $node |
||
| 137 | * |
||
| 138 | * @return FilesDocument[] |
||
| 139 | * @throws InterruptException |
||
| 140 | * @throws InvalidPathException |
||
| 141 | * @throws NotFoundException |
||
| 142 | * @throws TickDoesNotExistException |
||
| 143 | */ |
||
| 144 | public function getFilesFromDirectory(Runner $runner, $userId, Folder $node) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * @param Node $file |
||
| 174 | * |
||
| 175 | * @param string $viewerId |
||
| 176 | * |
||
| 177 | * @return FilesDocument |
||
| 178 | * @throws FileIsNotIndexableException |
||
| 179 | * @throws InvalidPathException |
||
| 180 | * @throws NotFoundException |
||
| 181 | */ |
||
| 182 | private function generateFilesDocumentFromFile(Node $file, $viewerId = '') { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param Node $file |
||
| 202 | * |
||
| 203 | * @throws FileIsNotIndexableException |
||
| 204 | * @throws NotFoundException |
||
| 205 | */ |
||
| 206 | private function fileMustBeIndexable(Node $file) { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $userId |
||
| 213 | * @param string $path |
||
| 214 | * |
||
| 215 | * @return Node |
||
| 216 | * @throws NotFoundException |
||
| 217 | */ |
||
| 218 | public function getFileFromPath($userId, $path) { |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $userId |
||
| 226 | * @param int $fileId |
||
| 227 | * |
||
| 228 | * @return Node |
||
| 229 | */ |
||
| 230 | public function getFileFromId($userId, $fileId) { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * @param int $fileId |
||
| 247 | * @param string $viewerId |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | private function getPathFromViewerId($fileId, $viewerId) { |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * @param IndexDocument $document |
||
| 271 | */ |
||
| 272 | public function setDocumentInfo(IndexDocument $document) { |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param IndexDocument $document |
||
| 295 | */ |
||
| 296 | public function setDocumentTitle(IndexDocument $document) { |
||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * @param IndexDocument $document |
||
| 303 | */ |
||
| 304 | public function setDocumentLink(IndexDocument $document) { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param int $fileId |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | private function getWebdavId($fileId) { |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * @param IndexDocument $document |
||
| 328 | * |
||
| 329 | * @throws InvalidPathException |
||
| 330 | * @throws NotFoundException |
||
| 331 | */ |
||
| 332 | public function setDocumentMore(IndexDocument $document) { |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * @param FilesDocument[] $documents |
||
| 362 | * |
||
| 363 | * @return FilesDocument[] |
||
| 364 | */ |
||
| 365 | public function generateDocuments($documents) { |
||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @param IndexDocument $document |
||
| 394 | * |
||
| 395 | * @return bool |
||
| 396 | */ |
||
| 397 | public function isDocumentUpToDate($document) { |
||
| 398 | $index = $document->getIndex(); |
||
| 399 | |||
| 400 | if ($index->getStatus() !== Index::INDEX_OK) { |
||
| 401 | return false; |
||
| 402 | } |
||
| 403 | |||
| 404 | if ($index->getLastIndex() >= $document->getModifiedTime()) { |
||
|
|
|||
| 405 | return true; |
||
| 406 | } |
||
| 407 | |||
| 408 | return false; |
||
| 409 | } |
||
| 410 | |||
| 411 | |||
| 412 | /** |
||
| 413 | * @param Index $index |
||
| 414 | * |
||
| 415 | * @return FilesDocument |
||
| 416 | * @throws InvalidPathException |
||
| 417 | * @throws NotFoundException |
||
| 418 | * @throws NotPermittedException |
||
| 419 | */ |
||
| 420 | public function updateDocument(Index $index) { |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * @param FilesDocument $document |
||
| 433 | * |
||
| 434 | * @throws InvalidPathException |
||
| 435 | * @throws NotFoundException |
||
| 436 | * @throws NotPermittedException |
||
| 437 | */ |
||
| 438 | private function updateDocumentFromFilesDocument(FilesDocument $document) { |
||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * @param FilesDocument $document |
||
| 454 | * @param Node $file |
||
| 455 | * |
||
| 456 | * @throws FileIsNotIndexableException |
||
| 457 | * @throws InvalidPathException |
||
| 458 | * @throws NotFoundException |
||
| 459 | * @throws NotPermittedException |
||
| 460 | */ |
||
| 461 | private function updateDocumentFromFile(FilesDocument $document, Node $file) { |
||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * @param Index $index |
||
| 469 | * |
||
| 470 | * @return FilesDocument |
||
| 471 | * @throws FileIsNotIndexableException |
||
| 472 | * @throws InvalidPathException |
||
| 473 | * @throws NotFoundException |
||
| 474 | * @throws NotPermittedException |
||
| 475 | */ |
||
| 476 | private function generateDocumentFromIndex(Index $index) { |
||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * @param FilesDocument $document |
||
| 498 | * @param Node $file |
||
| 499 | * |
||
| 500 | * @throws FileIsNotIndexableException |
||
| 501 | * @throws InvalidPathException |
||
| 502 | * @throws NotFoundException |
||
| 503 | */ |
||
| 504 | private function updateAccessFromFile(FilesDocument $document, Node $file) { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * @param FilesDocument $document |
||
| 529 | * @param Node $file |
||
| 530 | * |
||
| 531 | * @throws NotFoundException |
||
| 532 | */ |
||
| 533 | private function updateDocumentWithLocalFiles(FilesDocument $document, Node $file) { |
||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * @param FilesDocument $document |
||
| 546 | * @param Node $file |
||
| 547 | * |
||
| 548 | * @throws InvalidPathException |
||
| 549 | * @throws NotFoundException |
||
| 550 | * @throws NotPermittedException |
||
| 551 | */ |
||
| 552 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
| 577 | |||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $mimeType |
||
| 581 | * |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | private function parseMimeType($mimeType) { |
||
| 612 | |||
| 613 | |||
| 614 | /** |
||
| 615 | * @param FilesDocument $document |
||
| 616 | * @param File $file |
||
| 617 | * |
||
| 618 | * @throws NotPermittedException |
||
| 619 | */ |
||
| 620 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
| 632 | |||
| 633 | |||
| 634 | /** |
||
| 635 | * @param FilesDocument $document |
||
| 636 | * @param File $file |
||
| 637 | * |
||
| 638 | * @throws NotPermittedException |
||
| 639 | */ |
||
| 640 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
| 652 | |||
| 653 | |||
| 654 | /** |
||
| 655 | * @param FilesDocument $document |
||
| 656 | * @param File $file |
||
| 657 | * |
||
| 658 | * @throws NotPermittedException |
||
| 659 | */ |
||
| 660 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
| 672 | |||
| 673 | |||
| 674 | /** |
||
| 675 | * @param Node $file |
||
| 676 | * |
||
| 677 | * @return DocumentAccess |
||
| 678 | * @throws InvalidPathException |
||
| 679 | * @throws NotFoundException |
||
| 680 | */ |
||
| 681 | private function getDocumentAccessFromFile(Node $file) { |
||
| 696 | |||
| 697 | |||
| 698 | /** |
||
| 699 | * @param Node $file |
||
| 700 | * @param DocumentAccess $access |
||
| 701 | * |
||
| 702 | * @return array |
||
| 703 | * @throws InvalidPathException |
||
| 704 | * @throws NotFoundException |
||
| 705 | */ |
||
| 706 | private function getShareNamesFromFile(Node $file, DocumentAccess $access) { |
||
| 722 | |||
| 723 | |||
| 724 | /** |
||
| 725 | * @param Node $file |
||
| 726 | * |
||
| 727 | * @return array |
||
| 728 | */ |
||
| 729 | private function getAllSharesFromFile(Node $file) { |
||
| 747 | |||
| 748 | |||
| 749 | /** |
||
| 750 | * @param $fileId |
||
| 751 | * |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | private function getSharesFromFileId($fileId) { |
||
| 772 | |||
| 773 | |||
| 774 | /** |
||
| 775 | * @param array $share |
||
| 776 | * @param array $users |
||
| 777 | */ |
||
| 778 | View Code Duplication | private function parseUsersShares($share, &$users) { |
|
| 787 | |||
| 788 | |||
| 789 | /** |
||
| 790 | * @param array $share |
||
| 791 | * @param array $groups |
||
| 792 | */ |
||
| 793 | View Code Duplication | private function parseUsersGroups($share, &$groups) { |
|
| 802 | |||
| 803 | |||
| 804 | /** |
||
| 805 | * @param array $share |
||
| 806 | * @param array $circles |
||
| 807 | */ |
||
| 808 | View Code Duplication | private function parseUsersCircles($share, &$circles) { |
|
| 817 | |||
| 818 | |||
| 819 | /** |
||
| 820 | * @param array $share |
||
| 821 | * @param array $links |
||
| 822 | */ |
||
| 823 | View Code Duplication | private function parseUsersLinks($share, &$links) { |
|
| 832 | |||
| 833 | |||
| 834 | } |