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 |
||
| 56 | class FilesService { |
||
| 57 | |||
| 58 | const MIMETYPE_TEXT = 'files_text'; |
||
| 59 | const MIMETYPE_PDF = 'files_pdf'; |
||
| 60 | const MIMETYPE_OFFICE = 'files_office'; |
||
| 61 | const MIMETYPE_OCR = 'files_ocr'; |
||
| 62 | const MIMETYPE_IMAGE = 'files_image'; |
||
| 63 | const MIMETYPE_AUDIO = 'files_audio'; |
||
| 64 | |||
| 65 | |||
| 66 | /** @var IAppContainer */ |
||
| 67 | private $container; |
||
| 68 | |||
| 69 | /** @var IRootFolder */ |
||
| 70 | private $rootFolder; |
||
| 71 | |||
| 72 | /** @var IUserManager */ |
||
| 73 | private $userManager; |
||
| 74 | |||
| 75 | /** @var AppManager */ |
||
| 76 | private $appManager; |
||
| 77 | |||
| 78 | /** @var IManager */ |
||
| 79 | private $shareManager; |
||
| 80 | |||
| 81 | /** @var ConfigService */ |
||
| 82 | private $configService; |
||
| 83 | |||
| 84 | /** @var LocalFilesService */ |
||
| 85 | private $localFilesService; |
||
| 86 | |||
| 87 | /** @var ExternalFilesService */ |
||
| 88 | private $externalFilesService; |
||
| 89 | |||
| 90 | /** @var GroupFoldersService */ |
||
| 91 | private $groupFoldersService; |
||
| 92 | |||
| 93 | /** @var MiscService */ |
||
| 94 | private $miscService; |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * FilesService constructor. |
||
| 99 | * |
||
| 100 | * @param IAppContainer $container |
||
| 101 | * @param IRootFolder $rootFolder |
||
| 102 | * @param AppManager $appManager |
||
| 103 | * @param IUserManager $userManager |
||
| 104 | * @param IManager $shareManager |
||
| 105 | * @param ConfigService $configService |
||
| 106 | * @param LocalFilesService $localFilesService |
||
| 107 | * @param ExternalFilesService $externalFilesService |
||
| 108 | * @param GroupFoldersService $groupFoldersService |
||
| 109 | * @param MiscService $miscService |
||
| 110 | * |
||
| 111 | * @internal param IProviderFactory $factory |
||
| 112 | */ |
||
| 113 | public function __construct( |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * @param Runner $runner |
||
| 139 | * @param string $userId |
||
| 140 | * |
||
| 141 | * @return FilesDocument[] |
||
| 142 | * @throws InterruptException |
||
| 143 | * @throws InvalidPathException |
||
| 144 | * @throws NotFoundException |
||
| 145 | * @throws TickDoesNotExistException |
||
| 146 | */ |
||
| 147 | public function getFilesFromUser(Runner $runner, $userId) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $userId |
||
| 162 | */ |
||
| 163 | private function initFileSystems($userId) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * @param Runner $runner |
||
| 175 | * @param string $userId |
||
| 176 | * @param Folder $node |
||
| 177 | * |
||
| 178 | * @return FilesDocument[] |
||
| 179 | * @throws InterruptException |
||
| 180 | * @throws InvalidPathException |
||
| 181 | * @throws NotFoundException |
||
| 182 | * @throws TickDoesNotExistException |
||
| 183 | */ |
||
| 184 | public function getFilesFromDirectory(Runner $runner, $userId, Folder $node) { |
||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * @param Node $file |
||
| 218 | * |
||
| 219 | * @param string $viewerId |
||
| 220 | * |
||
| 221 | * @return FilesDocument |
||
| 222 | * @throws FileIsNotIndexableException |
||
| 223 | * @throws InvalidPathException |
||
| 224 | * @throws NotFoundException |
||
| 225 | * @throws Exception |
||
| 226 | */ |
||
| 227 | private function generateFilesDocumentFromFile(Node $file, $viewerId) { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @param Node $file |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | * @throws FileIsNotIndexableException |
||
| 255 | * @throws NotFoundException |
||
| 256 | */ |
||
| 257 | private function getFileSource(Node $file) { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $userId |
||
| 274 | * @param string $path |
||
| 275 | * |
||
| 276 | * @return Node |
||
| 277 | * @throws NotFoundException |
||
| 278 | */ |
||
| 279 | public function getFileFromPath($userId, $path) { |
||
| 283 | |||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $userId |
||
| 287 | * @param int $fileId |
||
| 288 | * |
||
| 289 | * @return Node |
||
| 290 | */ |
||
| 291 | public function getFileFromId($userId, $fileId) { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param int $fileId |
||
| 316 | * @param string $viewerId |
||
| 317 | * |
||
| 318 | * @throws Exception |
||
| 319 | * @return string |
||
| 320 | */ |
||
| 321 | private function getPathFromViewerId($fileId, $viewerId) { |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * @param FilesDocument $document |
||
| 341 | */ |
||
| 342 | public function setDocumentInfo(FilesDocument $document) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param FilesDocument $document |
||
| 366 | */ |
||
| 367 | public function setDocumentTitle(FilesDocument $document) { |
||
| 370 | |||
| 371 | |||
| 372 | /** |
||
| 373 | * @param FilesDocument $document |
||
| 374 | */ |
||
| 375 | public function setDocumentLink(FilesDocument $document) { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * @param FilesDocument $document |
||
| 396 | * |
||
| 397 | * @throws InvalidPathException |
||
| 398 | * @throws NotFoundException |
||
| 399 | */ |
||
| 400 | public function setDocumentMore(FilesDocument $document) { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param FilesDocument[] $documents |
||
| 431 | * |
||
| 432 | * @return FilesDocument[] |
||
| 433 | */ |
||
| 434 | public function generateDocuments($documents) { |
||
| 457 | |||
| 458 | |||
| 459 | /** |
||
| 460 | * @param Index $index |
||
| 461 | * |
||
| 462 | * @return FilesDocument |
||
| 463 | * @throws FileIsNotIndexableException |
||
| 464 | * @throws InvalidPathException |
||
| 465 | * @throws NotFoundException |
||
| 466 | * @throws NotPermittedException |
||
| 467 | */ |
||
| 468 | private function generateDocumentFromIndex(Index $index) { |
||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * @param IndexDocument $document |
||
| 490 | * |
||
| 491 | * @return bool |
||
| 492 | */ |
||
| 493 | public function isDocumentUpToDate($document) { |
||
| 513 | |||
| 514 | |||
| 515 | /** |
||
| 516 | * @param Index $index |
||
| 517 | * |
||
| 518 | * @return FilesDocument |
||
| 519 | * @throws InvalidPathException |
||
| 520 | * @throws NotFoundException |
||
| 521 | * @throws NotPermittedException |
||
| 522 | */ |
||
| 523 | public function updateDocument(Index $index) { |
||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * @param FilesDocument $document |
||
| 539 | * |
||
| 540 | * @throws InvalidPathException |
||
| 541 | * @throws NotFoundException |
||
| 542 | * @throws NotPermittedException |
||
| 543 | */ |
||
| 544 | private function updateFilesDocument(FilesDocument $document) { |
||
| 555 | |||
| 556 | |||
| 557 | /** |
||
| 558 | * @param FilesDocument $document |
||
| 559 | * @param Node $file |
||
| 560 | * |
||
| 561 | * @throws InvalidPathException |
||
| 562 | * @throws NotFoundException |
||
| 563 | * @throws NotPermittedException |
||
| 564 | */ |
||
| 565 | private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) { |
||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * @param FilesDocument $document |
||
| 579 | * @param Node $file |
||
| 580 | */ |
||
| 581 | private function updateDocumentAccess(FilesDocument $document, Node $file) { |
||
| 596 | |||
| 597 | |||
| 598 | /** |
||
| 599 | * @param FilesDocument $document |
||
| 600 | * @param Node $file |
||
| 601 | * |
||
| 602 | * @throws InvalidPathException |
||
| 603 | * @throws NotFoundException |
||
| 604 | * @throws NotPermittedException |
||
| 605 | */ |
||
| 606 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * @param FilesDocument $document |
||
| 634 | * @param Node $file |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | private function updateShareNames(FilesDocument $document, Node $file) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @param int $fileId |
||
| 677 | * |
||
| 678 | * @return string |
||
| 679 | */ |
||
| 680 | private function getWebdavId($fileId) { |
||
| 685 | |||
| 686 | |||
| 687 | /** |
||
| 688 | * @param string $mimeType |
||
| 689 | * |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | private function parseMimeType($mimeType) { |
||
| 704 | |||
| 705 | |||
| 706 | /** |
||
| 707 | * @param string $mimeType |
||
| 708 | * @param string $parsed |
||
| 709 | * |
||
| 710 | * @throws KnownFileMimeTypeException |
||
| 711 | */ |
||
| 712 | private function parseMimeTypeText($mimeType, &$parsed) { |
||
| 730 | |||
| 731 | |||
| 732 | /** |
||
| 733 | * @param string $mimeType |
||
| 734 | * @param string $parsed |
||
| 735 | * |
||
| 736 | * @throws KnownFileMimeTypeException |
||
| 737 | */ |
||
| 738 | private function parseMimeTypePDF($mimeType, &$parsed) { |
||
| 745 | |||
| 746 | |||
| 747 | /** |
||
| 748 | * @param string $mimeType |
||
| 749 | * @param string $parsed |
||
| 750 | * |
||
| 751 | * @throws KnownFileMimeTypeException |
||
| 752 | */ |
||
| 753 | private function parseMimeTypeOffice($mimeType, &$parsed) { |
||
| 772 | |||
| 773 | |||
| 774 | /** |
||
| 775 | * @param FilesDocument $document |
||
| 776 | * @param File $file |
||
| 777 | * |
||
| 778 | * @throws NotPermittedException |
||
| 779 | */ |
||
| 780 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
| 792 | |||
| 793 | |||
| 794 | /** |
||
| 795 | * @param FilesDocument $document |
||
| 796 | * @param File $file |
||
| 797 | * |
||
| 798 | * @throws NotPermittedException |
||
| 799 | */ |
||
| 800 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
| 818 | |||
| 819 | |||
| 820 | /** |
||
| 821 | * @param FilesDocument $document |
||
| 822 | * @param File $file |
||
| 823 | * |
||
| 824 | * @throws NotPermittedException |
||
| 825 | */ |
||
| 826 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
| 844 | |||
| 845 | |||
| 846 | /** |
||
| 847 | * @param FilesDocument $document |
||
| 848 | * @param File $file |
||
| 849 | */ |
||
| 850 | private function extractContentFromFileOCR(FilesDocument $document, File $file) { |
||
| 862 | |||
| 863 | |||
| 864 | /** |
||
| 865 | * @param FilesDocument $document |
||
| 866 | * @param File $file |
||
| 867 | */ |
||
| 868 | private function extractContentUsingTesseractOCR(FilesDocument $document, File $file) { |
||
| 889 | |||
| 890 | |||
| 891 | /** |
||
| 892 | * @param FilesDocument $document |
||
| 893 | * |
||
| 894 | * @return bool |
||
| 895 | */ |
||
| 896 | private function isSourceIndexable(FilesDocument $document) { |
||
| 906 | |||
| 907 | |||
| 908 | private function impersonateOwner(Index $index) { |
||
| 915 | |||
| 916 | } |
||
| 917 | |||
| 918 |
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.