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 |
||
| 52 | class FilesService { |
||
| 53 | |||
| 54 | const MIMETYPE_TEXT = 'files_text'; |
||
| 55 | const MIMETYPE_PDF = 'files_pdf'; |
||
| 56 | const MIMETYPE_OFFICE = 'files_office'; |
||
| 57 | const MIMETYPE_IMAGE = 'files_image'; |
||
| 58 | const MIMETYPE_AUDIO = 'files_audio'; |
||
| 59 | |||
| 60 | |||
| 61 | /** @var IRootFolder */ |
||
| 62 | private $rootFolder; |
||
| 63 | |||
| 64 | /** @var IUserManager */ |
||
| 65 | private $userManager; |
||
| 66 | |||
| 67 | /** @var IManager */ |
||
| 68 | private $shareManager; |
||
| 69 | |||
| 70 | /** @var ConfigService */ |
||
| 71 | private $configService; |
||
| 72 | |||
| 73 | /** @var LocalFilesService */ |
||
| 74 | private $localFilesService; |
||
| 75 | |||
| 76 | /** @var ExternalFilesService */ |
||
| 77 | private $externalFilesService; |
||
| 78 | |||
| 79 | /** @var GroupFoldersService */ |
||
| 80 | private $groupFoldersService; |
||
| 81 | |||
| 82 | /** @var MiscService */ |
||
| 83 | private $miscService; |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * FilesService constructor. |
||
| 88 | * |
||
| 89 | * @param IRootFolder $rootFolder |
||
| 90 | * @param IUserManager $userManager |
||
| 91 | * @param IManager $shareManager |
||
| 92 | * @param ConfigService $configService |
||
| 93 | * @param LocalFilesService $localFilesService |
||
| 94 | * @param ExternalFilesService $externalFilesService |
||
| 95 | * @param GroupFoldersService $groupFoldersService |
||
| 96 | * @param MiscService $miscService |
||
| 97 | * |
||
| 98 | * @internal param IProviderFactory $factory |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function __construct( |
|
|
|
|||
| 101 | IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager, |
||
| 102 | ConfigService $configService, |
||
| 103 | LocalFilesService $localFilesService, |
||
| 104 | ExternalFilesService $externalFilesService, |
||
| 105 | GroupFoldersService $groupFoldersService, |
||
| 106 | MiscService $miscService |
||
| 107 | ) { |
||
| 108 | $this->rootFolder = $rootFolder; |
||
| 109 | $this->userManager = $userManager; |
||
| 110 | $this->shareManager = $shareManager; |
||
| 111 | |||
| 112 | $this->configService = $configService; |
||
| 113 | $this->localFilesService = $localFilesService; |
||
| 114 | $this->externalFilesService = $externalFilesService; |
||
| 115 | $this->groupFoldersService = $groupFoldersService; |
||
| 116 | |||
| 117 | $this->miscService = $miscService; |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * @param Runner $runner |
||
| 123 | * @param string $userId |
||
| 124 | * |
||
| 125 | * @return FilesDocument[] |
||
| 126 | * @throws InterruptException |
||
| 127 | * @throws InvalidPathException |
||
| 128 | * @throws NotFoundException |
||
| 129 | * @throws TickDoesNotExistException |
||
| 130 | */ |
||
| 131 | public function getFilesFromUser(Runner $runner, $userId) { |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $userId |
||
| 146 | */ |
||
| 147 | private function initFileSystems($userId) { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @param Runner $runner |
||
| 159 | * @param string $userId |
||
| 160 | * @param Folder $node |
||
| 161 | * |
||
| 162 | * @return FilesDocument[] |
||
| 163 | * @throws InterruptException |
||
| 164 | * @throws InvalidPathException |
||
| 165 | * @throws NotFoundException |
||
| 166 | * @throws TickDoesNotExistException |
||
| 167 | */ |
||
| 168 | public function getFilesFromDirectory(Runner $runner, $userId, Folder $node) { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @param Node $file |
||
| 202 | * |
||
| 203 | * @param string $viewerId |
||
| 204 | * |
||
| 205 | * @return FilesDocument |
||
| 206 | * @throws FileIsNotIndexableException |
||
| 207 | * @throws InvalidPathException |
||
| 208 | * @throws NotFoundException |
||
| 209 | * @throws Exception |
||
| 210 | */ |
||
| 211 | private function generateFilesDocumentFromFile(Node $file, $viewerId) { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param Node $file |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | * @throws FileIsNotIndexableException |
||
| 239 | * @throws NotFoundException |
||
| 240 | */ |
||
| 241 | private function getFileSource(Node $file) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $userId |
||
| 258 | * @param string $path |
||
| 259 | * |
||
| 260 | * @return Node |
||
| 261 | * @throws NotFoundException |
||
| 262 | */ |
||
| 263 | public function getFileFromPath($userId, $path) { |
||
| 267 | |||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string $userId |
||
| 271 | * @param int $fileId |
||
| 272 | * |
||
| 273 | * @return Node |
||
| 274 | */ |
||
| 275 | public function getFileFromId($userId, $fileId) { |
||
| 296 | |||
| 297 | |||
| 298 | /** |
||
| 299 | * @param int $fileId |
||
| 300 | * @param string $viewerId |
||
| 301 | * |
||
| 302 | * @throws Exception |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | private function getPathFromViewerId($fileId, $viewerId) { |
||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * @param FilesDocument $document |
||
| 325 | */ |
||
| 326 | public function setDocumentInfo(FilesDocument $document) { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * @param FilesDocument $document |
||
| 350 | */ |
||
| 351 | public function setDocumentTitle(FilesDocument $document) { |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param FilesDocument $document |
||
| 358 | */ |
||
| 359 | public function setDocumentLink(FilesDocument $document) { |
||
| 376 | |||
| 377 | |||
| 378 | /** |
||
| 379 | * @param FilesDocument $document |
||
| 380 | * |
||
| 381 | * @throws InvalidPathException |
||
| 382 | * @throws NotFoundException |
||
| 383 | */ |
||
| 384 | public function setDocumentMore(FilesDocument $document) { |
||
| 411 | |||
| 412 | |||
| 413 | /** |
||
| 414 | * @param FilesDocument[] $documents |
||
| 415 | * |
||
| 416 | * @return FilesDocument[] |
||
| 417 | */ |
||
| 418 | public function generateDocuments($documents) { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * @param Index $index |
||
| 445 | * |
||
| 446 | * @return FilesDocument |
||
| 447 | * @throws FileIsNotIndexableException |
||
| 448 | * @throws InvalidPathException |
||
| 449 | * @throws NotFoundException |
||
| 450 | * @throws NotPermittedException |
||
| 451 | */ |
||
| 452 | private function generateDocumentFromIndex(Index $index) { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @param IndexDocument $document |
||
| 474 | * |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function isDocumentUpToDate($document) { |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * @param Index $index |
||
| 501 | * |
||
| 502 | * @return FilesDocument |
||
| 503 | * @throws InvalidPathException |
||
| 504 | * @throws NotFoundException |
||
| 505 | * @throws NotPermittedException |
||
| 506 | */ |
||
| 507 | public function updateDocument(Index $index) { |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * @param FilesDocument $document |
||
| 523 | * |
||
| 524 | * @throws InvalidPathException |
||
| 525 | * @throws NotFoundException |
||
| 526 | * @throws NotPermittedException |
||
| 527 | */ |
||
| 528 | private function updateFilesDocument(FilesDocument $document) { |
||
| 539 | |||
| 540 | |||
| 541 | /** |
||
| 542 | * @param FilesDocument $document |
||
| 543 | * @param Node $file |
||
| 544 | * |
||
| 545 | * @throws InvalidPathException |
||
| 546 | * @throws NotFoundException |
||
| 547 | * @throws NotPermittedException |
||
| 548 | */ |
||
| 549 | private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) { |
||
| 559 | |||
| 560 | |||
| 561 | /** |
||
| 562 | * @param FilesDocument $document |
||
| 563 | * @param Node $file |
||
| 564 | */ |
||
| 565 | private function updateDocumentAccess(FilesDocument $document, Node $file) { |
||
| 580 | |||
| 581 | |||
| 582 | /** |
||
| 583 | * @param FilesDocument $document |
||
| 584 | * @param Node $file |
||
| 585 | * |
||
| 586 | * @throws InvalidPathException |
||
| 587 | * @throws NotFoundException |
||
| 588 | * @throws NotPermittedException |
||
| 589 | */ |
||
| 590 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * @param FilesDocument $document |
||
| 617 | * @param Node $file |
||
| 618 | * |
||
| 619 | * @return array |
||
| 620 | */ |
||
| 621 | private function updateShareNames(FilesDocument $document, Node $file) { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param int $fileId |
||
| 660 | * |
||
| 661 | * @return string |
||
| 662 | */ |
||
| 663 | private function getWebdavId($fileId) { |
||
| 668 | |||
| 669 | |||
| 670 | /** |
||
| 671 | * @param string $mimeType |
||
| 672 | * |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | private function parseMimeType($mimeType) { |
||
| 707 | |||
| 708 | |||
| 709 | /** |
||
| 710 | * @param FilesDocument $document |
||
| 711 | * @param File $file |
||
| 712 | * |
||
| 713 | * @throws NotPermittedException |
||
| 714 | */ |
||
| 715 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
| 732 | |||
| 733 | |||
| 734 | /** |
||
| 735 | * @param FilesDocument $document |
||
| 736 | * @param File $file |
||
| 737 | * |
||
| 738 | * @throws NotPermittedException |
||
| 739 | */ |
||
| 740 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
| 758 | |||
| 759 | |||
| 760 | /** |
||
| 761 | * @param FilesDocument $document |
||
| 762 | * @param File $file |
||
| 763 | * |
||
| 764 | * @throws NotPermittedException |
||
| 765 | */ |
||
| 766 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
| 784 | |||
| 785 | |||
| 786 | /** |
||
| 787 | * @param FilesDocument $document |
||
| 788 | * |
||
| 789 | * @return bool |
||
| 790 | */ |
||
| 791 | private function isSourceIndexable(FilesDocument $document) { |
||
| 801 | |||
| 802 | |||
| 803 | private function impersonateOwner(Index $index) { |
||
| 810 | |||
| 811 | } |
||
| 812 | |||
| 813 |
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.