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 | View Code Duplication | public function __construct( |
|
| 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) { |
||
| 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 NotPermittedException |
||
| 549 | */ |
||
| 550 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * @param string $mimeType |
||
| 574 | * |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | private function parseMimeType($mimeType) { |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * @param FilesDocument $document |
||
| 609 | * @param File $file |
||
| 610 | * |
||
| 611 | * @throws NotPermittedException |
||
| 612 | */ |
||
| 613 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
| 625 | |||
| 626 | |||
| 627 | /** |
||
| 628 | * @param FilesDocument $document |
||
| 629 | * @param File $file |
||
| 630 | * |
||
| 631 | * @throws NotPermittedException |
||
| 632 | */ |
||
| 633 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
| 645 | |||
| 646 | |||
| 647 | /** |
||
| 648 | * @param FilesDocument $document |
||
| 649 | * @param File $file |
||
| 650 | * |
||
| 651 | * @throws NotPermittedException |
||
| 652 | */ |
||
| 653 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * @param Node $file |
||
| 669 | * |
||
| 670 | * @return DocumentAccess |
||
| 671 | * @throws InvalidPathException |
||
| 672 | * @throws NotFoundException |
||
| 673 | */ |
||
| 674 | private function getDocumentAccessFromFile(Node $file) { |
||
| 689 | |||
| 690 | |||
| 691 | /** |
||
| 692 | * @param Node $file |
||
| 693 | * @param DocumentAccess $access |
||
| 694 | * |
||
| 695 | * @return array |
||
| 696 | * @throws InvalidPathException |
||
| 697 | * @throws NotFoundException |
||
| 698 | */ |
||
| 699 | private function getShareNamesFromFile(Node $file, DocumentAccess $access) { |
||
| 715 | |||
| 716 | |||
| 717 | /** |
||
| 718 | * @param Node $file |
||
| 719 | * |
||
| 720 | * @return array |
||
| 721 | */ |
||
| 722 | private function getAllSharesFromFile(Node $file) { |
||
| 740 | |||
| 741 | |||
| 742 | /** |
||
| 743 | * @param $fileId |
||
| 744 | * |
||
| 745 | * @return array |
||
| 746 | */ |
||
| 747 | private function getSharesFromFileId($fileId) { |
||
| 765 | |||
| 766 | |||
| 767 | /** |
||
| 768 | * @param array $share |
||
| 769 | * @param array $users |
||
| 770 | */ |
||
| 771 | View Code Duplication | private function parseUsersShares($share, &$users) { |
|
| 780 | |||
| 781 | |||
| 782 | /** |
||
| 783 | * @param array $share |
||
| 784 | * @param array $groups |
||
| 785 | */ |
||
| 786 | View Code Duplication | private function parseUsersGroups($share, &$groups) { |
|
| 795 | |||
| 796 | |||
| 797 | /** |
||
| 798 | * @param array $share |
||
| 799 | * @param array $circles |
||
| 800 | */ |
||
| 801 | View Code Duplication | private function parseUsersCircles($share, &$circles) { |
|
| 810 | |||
| 811 | |||
| 812 | /** |
||
| 813 | * @param array $share |
||
| 814 | * @param array $links |
||
| 815 | */ |
||
| 816 | View Code Duplication | private function parseUsersLinks($share, &$links) { |
|
| 825 | |||
| 826 | |||
| 827 | } |
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.