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 |
||
| 68 | class FilesService { |
||
| 69 | |||
| 70 | |||
| 71 | use TPathTools; |
||
| 72 | |||
| 73 | |||
| 74 | const MIMETYPE_TEXT = 'files_text'; |
||
| 75 | const MIMETYPE_PDF = 'files_pdf'; |
||
| 76 | const MIMETYPE_OFFICE = 'files_office'; |
||
| 77 | const MIMETYPE_IMAGE = 'files_image'; |
||
| 78 | const MIMETYPE_AUDIO = 'files_audio'; |
||
| 79 | |||
| 80 | |||
| 81 | /** @var IAppContainer */ |
||
| 82 | private $container; |
||
| 83 | |||
| 84 | /** @var IRootFolder */ |
||
| 85 | private $rootFolder; |
||
| 86 | |||
| 87 | /** @var IUserManager */ |
||
| 88 | private $userManager; |
||
| 89 | |||
| 90 | /** @var IAppManager */ |
||
| 91 | private $appManager; |
||
| 92 | |||
| 93 | /** @var IManager */ |
||
| 94 | private $shareManager; |
||
| 95 | |||
| 96 | /** @var ConfigService */ |
||
| 97 | private $configService; |
||
| 98 | |||
| 99 | /** @var LocalFilesService */ |
||
| 100 | private $localFilesService; |
||
| 101 | |||
| 102 | /** @var ExternalFilesService */ |
||
| 103 | private $externalFilesService; |
||
| 104 | |||
| 105 | /** @var GroupFoldersService */ |
||
| 106 | private $groupFoldersService; |
||
| 107 | |||
| 108 | /** @var ExtensionService */ |
||
| 109 | private $extensionService; |
||
| 110 | |||
| 111 | /** @var MiscService */ |
||
| 112 | private $miscService; |
||
| 113 | |||
| 114 | |||
| 115 | /** @var IRunner */ |
||
| 116 | private $runner; |
||
| 117 | |||
| 118 | /** @var int */ |
||
| 119 | private $sumDocuments; |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * FilesService constructor. |
||
| 124 | * |
||
| 125 | * @param IAppContainer $container |
||
| 126 | * @param IRootFolder $rootFolder |
||
| 127 | * @param IAppManager $appManager |
||
| 128 | * @param IUserManager $userManager |
||
| 129 | * @param IManager $shareManager |
||
| 130 | * @param ConfigService $configService |
||
| 131 | * @param LocalFilesService $localFilesService |
||
| 132 | * @param ExternalFilesService $externalFilesService |
||
| 133 | * @param GroupFoldersService $groupFoldersService |
||
| 134 | * @param ExtensionService $extensionService |
||
| 135 | * @param MiscService $miscService |
||
| 136 | * |
||
| 137 | * @internal param IProviderFactory $factory |
||
| 138 | */ |
||
| 139 | public function __construct( |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @param IRunner $runner |
||
| 164 | */ |
||
| 165 | public function setRunner(IRunner $runner) { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $userId |
||
| 172 | * @param IIndexOptions $indexOptions |
||
| 173 | * |
||
| 174 | * @return FilesDocument[] |
||
| 175 | * @throws InvalidPathException |
||
| 176 | * @throws NotFoundException |
||
| 177 | */ |
||
| 178 | public function getFilesFromUser(string $userId, IIndexOptions $indexOptions): array { |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $userId |
||
| 203 | */ |
||
| 204 | private function initFileSystems(string $userId) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $userId |
||
| 216 | * @param Folder $node |
||
| 217 | * |
||
| 218 | * @return FilesDocument[] |
||
| 219 | * @throws InvalidPathException |
||
| 220 | * @throws NotFoundException |
||
| 221 | * @throws Exception |
||
| 222 | */ |
||
| 223 | public function getFilesFromDirectory(string $userId, Folder $node): array { |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $viewerId |
||
| 265 | * @param Node $file |
||
| 266 | * |
||
| 267 | * @return FilesDocument |
||
| 268 | * @throws FileIsNotIndexableException |
||
| 269 | * @throws InvalidPathException |
||
| 270 | * @throws NotFoundException |
||
| 271 | * @throws Exception |
||
| 272 | */ |
||
| 273 | private function generateFilesDocumentFromFile(string $viewerId, Node $file): FilesDocument { |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * @param Node $file |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | * @throws FileIsNotIndexableException |
||
| 304 | */ |
||
| 305 | private function getFileSource(Node $file): string { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $userId |
||
| 322 | * @param string $path |
||
| 323 | * |
||
| 324 | * @return Node |
||
| 325 | * @throws NotFoundException |
||
| 326 | */ |
||
| 327 | public function getFileFromPath(string $userId, string $path): Node { |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $userId |
||
| 335 | * @param int $fileId |
||
| 336 | * |
||
| 337 | * @return Node |
||
| 338 | * @throws FilesNotFoundException |
||
| 339 | * @throws EmptyUserException |
||
| 340 | */ |
||
| 341 | public function getFileFromId(string $userId, int $fileId): Node { |
||
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * @param IIndex $index |
||
| 361 | * |
||
| 362 | * @return Node |
||
| 363 | * @throws EmptyUserException |
||
| 364 | * @throws FilesNotFoundException |
||
| 365 | */ |
||
| 366 | public function getFileFromIndex(IIndex $index): Node { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * @param int $fileId |
||
| 375 | * @param string $viewerId |
||
| 376 | * |
||
| 377 | * @throws Exception |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function getPathFromViewerId(int $fileId, string $viewerId): string { |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * @param FilesDocument $document |
||
| 400 | */ |
||
| 401 | public function generateDocument(FilesDocument $document) { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * @param IIndex $index |
||
| 417 | * |
||
| 418 | * @return FilesDocument |
||
| 419 | * @throws FileIsNotIndexableException |
||
| 420 | * @throws InvalidPathException |
||
| 421 | * @throws NotFoundException |
||
| 422 | */ |
||
| 423 | private function generateDocumentFromIndex(IIndex $index): FilesDocument { |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * @param IndexDocument $document |
||
| 446 | * |
||
| 447 | * @return bool |
||
| 448 | */ |
||
| 449 | public function isDocumentUpToDate(IndexDocument $document): bool { |
||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * @param IIndex $index |
||
| 473 | * |
||
| 474 | * @return FilesDocument |
||
| 475 | * @throws InvalidPathException |
||
| 476 | * @throws NotFoundException |
||
| 477 | * @throws NotPermittedException |
||
| 478 | * @throws FileIsNotIndexableException |
||
| 479 | */ |
||
| 480 | public function updateDocument(IIndex $index): FilesDocument { |
||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * @param FilesDocument $document |
||
| 490 | * |
||
| 491 | * @throws NotFoundException |
||
| 492 | */ |
||
| 493 | private function updateFilesDocument(FilesDocument $document) { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * @param FilesDocument $document |
||
| 508 | * @param Node $file |
||
| 509 | */ |
||
| 510 | private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) { |
||
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * @param FilesDocument $document |
||
| 524 | * @param Node $file |
||
| 525 | */ |
||
| 526 | private function updateDocumentAccess(FilesDocument $document, Node $file) { |
||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * @param FilesDocument $document |
||
| 545 | * @param Node $file |
||
| 546 | */ |
||
| 547 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
| 576 | |||
| 577 | |||
| 578 | /** |
||
| 579 | * @param FilesDocument $document |
||
| 580 | * @param Node $file |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | private function updateShareNames(FilesDocument $document, Node $file): array { |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * @param string $mimeType |
||
| 619 | * |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | private function parseMimeType(string $mimeType): string { |
||
| 634 | |||
| 635 | |||
| 636 | /** |
||
| 637 | * @param string $mimeType |
||
| 638 | * @param string $parsed |
||
| 639 | * |
||
| 640 | * @throws KnownFileMimeTypeException |
||
| 641 | */ |
||
| 642 | private function parseMimeTypeText(string $mimeType, string &$parsed) { |
||
| 660 | |||
| 661 | |||
| 662 | /** |
||
| 663 | * @param string $mimeType |
||
| 664 | * @param string $parsed |
||
| 665 | * |
||
| 666 | * @throws KnownFileMimeTypeException |
||
| 667 | */ |
||
| 668 | private function parseMimeTypePDF(string $mimeType, string &$parsed) { |
||
| 675 | |||
| 676 | |||
| 677 | /** |
||
| 678 | * @param string $mimeType |
||
| 679 | * @param string $parsed |
||
| 680 | * |
||
| 681 | * @throws KnownFileMimeTypeException |
||
| 682 | */ |
||
| 683 | private function parseMimeTypeOffice(string $mimeType, string &$parsed) { |
||
| 702 | |||
| 703 | |||
| 704 | /** |
||
| 705 | * @param FilesDocument $document |
||
| 706 | * @param File $file |
||
| 707 | * |
||
| 708 | * @throws NotPermittedException |
||
| 709 | */ |
||
| 710 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
| 724 | |||
| 725 | |||
| 726 | /** |
||
| 727 | * @param FilesDocument $document |
||
| 728 | * @param File $file |
||
| 729 | * |
||
| 730 | * @throws NotPermittedException |
||
| 731 | */ |
||
| 732 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
| 752 | |||
| 753 | |||
| 754 | /** |
||
| 755 | * @param FilesDocument $document |
||
| 756 | * @param File $file |
||
| 757 | * |
||
| 758 | * @throws NotPermittedException |
||
| 759 | */ |
||
| 760 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
| 780 | |||
| 781 | |||
| 782 | /** |
||
| 783 | * @param FilesDocument $document |
||
| 784 | * |
||
| 785 | * @return bool |
||
| 786 | */ |
||
| 787 | private function isSourceIndexable(FilesDocument $document): bool { |
||
| 797 | |||
| 798 | |||
| 799 | /** |
||
| 800 | * @param IIndex $index |
||
| 801 | */ |
||
| 802 | private function impersonateOwner(IIndex $index) { |
||
| 810 | |||
| 811 | |||
| 812 | /** |
||
| 813 | * @param $action |
||
| 814 | * @param bool $force |
||
| 815 | * |
||
| 816 | * @throws Exception |
||
| 817 | */ |
||
| 818 | private function updateRunnerAction(string $action, bool $force = false) { |
||
| 825 | |||
| 826 | |||
| 827 | /** |
||
| 828 | * @param array $data |
||
| 829 | */ |
||
| 830 | private function updateRunnerInfo($data) { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * @param IndexDocument $document |
||
| 840 | * @param Throwable $t |
||
| 841 | */ |
||
| 842 | private function manageContentErrorException(IndexDocument $document, Throwable $t) { |
||
| 853 | |||
| 854 | |||
| 855 | /** |
||
| 856 | * @param IIndex $index |
||
| 857 | * @param string $message |
||
| 858 | * @param string $exception |
||
| 859 | * @param int $sev |
||
| 860 | */ |
||
| 861 | private function updateNewIndexError(IIndex $index, string $message, string $exception, int $sev |
||
| 869 | } |
||
| 870 | |||
| 871 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.