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 |
||
71 | class FilesService { |
||
72 | |||
73 | |||
74 | use TPathTools; |
||
75 | |||
76 | |||
77 | const MIMETYPE_TEXT = 'files_text'; |
||
78 | const MIMETYPE_PDF = 'files_pdf'; |
||
79 | const MIMETYPE_OFFICE = 'files_office'; |
||
80 | const MIMETYPE_ZIP = 'files_zip'; |
||
81 | const MIMETYPE_IMAGE = 'files_image'; |
||
82 | const MIMETYPE_AUDIO = 'files_audio'; |
||
83 | |||
84 | const CHUNK_TREE_SIZE = 2; |
||
85 | |||
86 | |||
87 | /** @var IAppContainer */ |
||
88 | private $container; |
||
89 | |||
90 | /** @var IRootFolder */ |
||
91 | private $rootFolder; |
||
92 | |||
93 | /** @var IUserManager */ |
||
94 | private $userManager; |
||
95 | |||
96 | /** @var IAppManager */ |
||
97 | private $appManager; |
||
98 | |||
99 | /** @var IShareManager */ |
||
100 | private $shareManager; |
||
101 | |||
102 | /** @var ICommentsManager */ |
||
103 | private $commentsManager; |
||
104 | |||
105 | /** @var ConfigService */ |
||
106 | private $configService; |
||
107 | |||
108 | /** @var LocalFilesService */ |
||
109 | private $localFilesService; |
||
110 | |||
111 | /** @var ExternalFilesService */ |
||
112 | private $externalFilesService; |
||
113 | |||
114 | /** @var GroupFoldersService */ |
||
115 | private $groupFoldersService; |
||
116 | |||
117 | /** @var ExtensionService */ |
||
118 | private $extensionService; |
||
119 | |||
120 | /** @var IFullTextSearchManager */ |
||
121 | private $fullTextSearchManager; |
||
122 | |||
123 | /** @var MiscService */ |
||
124 | private $miscService; |
||
125 | |||
126 | |||
127 | /** @var IRunner */ |
||
128 | private $runner; |
||
129 | |||
130 | /** @var int */ |
||
131 | private $sumDocuments; |
||
132 | |||
133 | |||
134 | /** |
||
135 | * FilesService constructor. |
||
136 | * |
||
137 | * @param IAppContainer $container |
||
138 | * @param IRootFolder $rootFolder |
||
139 | * @param IAppManager $appManager |
||
140 | * @param IUserManager $userManager |
||
141 | * @param IShareManager $shareManager |
||
142 | * @param ICommentsManager $commentsManager |
||
143 | * @param ConfigService $configService |
||
144 | * @param LocalFilesService $localFilesService |
||
145 | * @param ExternalFilesService $externalFilesService |
||
146 | * @param GroupFoldersService $groupFoldersService |
||
147 | * @param ExtensionService $extensionService |
||
148 | * @param IFullTextSearchManager $fullTextSearchManager |
||
149 | * @param MiscService $miscService |
||
150 | * |
||
151 | * @internal param IProviderFactory $factory |
||
152 | */ |
||
153 | public function __construct( |
||
177 | |||
178 | |||
179 | /** |
||
180 | * @param IRunner $runner |
||
181 | */ |
||
182 | public function setRunner(IRunner $runner) { |
||
185 | |||
186 | |||
187 | /** |
||
188 | * @param string $userId |
||
189 | * @param IIndexOptions $indexOptions |
||
190 | * |
||
191 | * @return FilesDocument[] |
||
192 | * @throws NotFoundException |
||
193 | * @throws InvalidPathException |
||
194 | */ |
||
195 | public function getChunksFromUser(string $userId, IIndexOptions $indexOptions): array { |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $userId |
||
211 | * @param Folder $node |
||
212 | * @param int $level |
||
213 | * |
||
214 | * @return FilesDocument[] |
||
215 | * @throws InvalidPathException |
||
216 | * @throws NotFoundException |
||
217 | */ |
||
218 | public function getChunksFromDirectory(string $userId, Folder $node, $level = 0): array { |
||
239 | |||
240 | |||
241 | /** |
||
242 | * @param string $userId |
||
243 | * @param string $chunk |
||
244 | * |
||
245 | * @return FilesDocument[] |
||
246 | * @throws InvalidPathException |
||
247 | * @throws NotFoundException |
||
248 | */ |
||
249 | public function getFilesFromUser(string $userId, string $chunk): array { |
||
270 | |||
271 | |||
272 | /** |
||
273 | * @param string $userId |
||
274 | * @param Folder $node |
||
275 | * |
||
276 | * @return FilesDocument[] |
||
277 | * @throws InvalidPathException |
||
278 | * @throws NotFoundException |
||
279 | * @throws Exception |
||
280 | */ |
||
281 | public function getFilesFromDirectory(string $userId, Folder $node): array { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * @param string $userId |
||
325 | */ |
||
326 | private function initFileSystems(string $userId) { |
||
338 | |||
339 | |||
340 | /** |
||
341 | * @param string $viewerId |
||
342 | * @param Node $file |
||
343 | * |
||
344 | * @return FilesDocument |
||
345 | * @throws FileIsNotIndexableException |
||
346 | * @throws InvalidPathException |
||
347 | * @throws NotFoundException |
||
348 | * @throws Exception |
||
349 | */ |
||
350 | private function generateFilesDocumentFromFile(string $viewerId, Node $file): FilesDocument { |
||
382 | |||
383 | |||
384 | /** |
||
385 | * @param Node $file |
||
386 | * |
||
387 | * @return string |
||
388 | * @throws FileIsNotIndexableException |
||
389 | */ |
||
390 | View Code Duplication | private function getFileSource(Node $file): string { |
|
403 | |||
404 | |||
405 | /** |
||
406 | * @param string $userId |
||
407 | * @param string $path |
||
408 | * |
||
409 | * @return Node |
||
410 | * @throws NotFoundException |
||
411 | */ |
||
412 | public function getFileFromPath(string $userId, string $path): Node { |
||
416 | |||
417 | |||
418 | /** |
||
419 | * @param string $userId |
||
420 | * @param int $fileId |
||
421 | * |
||
422 | * @return Node |
||
423 | * @throws FilesNotFoundException |
||
424 | * @throws EmptyUserException |
||
425 | */ |
||
426 | public function getFileFromId(string $userId, int $fileId): Node { |
||
442 | |||
443 | |||
444 | /** |
||
445 | * @param IIndex $index |
||
446 | * |
||
447 | * @return Node |
||
448 | * @throws EmptyUserException |
||
449 | * @throws FilesNotFoundException |
||
450 | */ |
||
451 | public function getFileFromIndex(IIndex $index): Node { |
||
457 | |||
458 | |||
459 | /** |
||
460 | * @param int $fileId |
||
461 | * @param string $viewerId |
||
462 | * |
||
463 | * @throws Exception |
||
464 | * @return string |
||
465 | */ |
||
466 | private function getPathFromViewerId(int $fileId, string $viewerId): string { |
||
487 | |||
488 | |||
489 | /** |
||
490 | * @param FilesDocument $document |
||
491 | */ |
||
492 | public function generateDocument(FilesDocument $document) { |
||
506 | |||
507 | |||
508 | /** |
||
509 | * @param IIndex $index |
||
510 | * |
||
511 | * @return FilesDocument |
||
512 | * @throws FileIsNotIndexableException |
||
513 | * @throws InvalidPathException |
||
514 | * @throws NotFoundException |
||
515 | */ |
||
516 | private function generateDocumentFromIndex(IIndex $index): FilesDocument { |
||
537 | |||
538 | |||
539 | /** |
||
540 | * @param IndexDocument $document |
||
541 | * |
||
542 | * @return bool |
||
543 | */ |
||
544 | public function isDocumentUpToDate(IndexDocument $document): bool { |
||
564 | |||
565 | |||
566 | /** |
||
567 | * @param IIndex $index |
||
568 | * |
||
569 | * @return FilesDocument |
||
570 | * @throws InvalidPathException |
||
571 | * @throws NotFoundException |
||
572 | * @throws FileIsNotIndexableException |
||
573 | */ |
||
574 | public function updateDocument(IIndex $index): FilesDocument { |
||
583 | |||
584 | |||
585 | /** |
||
586 | * @param FilesDocument $document |
||
587 | * |
||
588 | * @throws NotFoundException |
||
589 | */ |
||
590 | private function updateFilesDocument(FilesDocument $document) { |
||
601 | |||
602 | |||
603 | /** |
||
604 | * @param FilesDocument $document |
||
605 | * @param Node $file |
||
606 | * |
||
607 | * @throws FileIsNotIndexableException |
||
608 | */ |
||
609 | private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) { |
||
619 | |||
620 | |||
621 | /** |
||
622 | * @param FilesDocument $document |
||
623 | * @param Node $file |
||
624 | * |
||
625 | * @throws FileIsNotIndexableException |
||
626 | */ |
||
627 | View Code Duplication | private function updateDocumentAccess(FilesDocument $document, Node $file) { |
|
642 | |||
643 | |||
644 | /** |
||
645 | * @param FilesDocument $document |
||
646 | * @param Node $file |
||
647 | */ |
||
648 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
680 | |||
681 | |||
682 | /** |
||
683 | * @param FilesDocument $document |
||
684 | */ |
||
685 | private function updateCommentsFromFile(FilesDocument $document) { |
||
695 | |||
696 | |||
697 | /** |
||
698 | * @param FilesDocument $document |
||
699 | * @param Node $file |
||
700 | * |
||
701 | * @return array |
||
702 | */ |
||
703 | private function updateShareNames(FilesDocument $document, Node $file): array { |
||
734 | |||
735 | |||
736 | /** |
||
737 | * @param string $mimeType |
||
738 | * @param string $extension |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | private function parseMimeType(string $mimeType, string $extension): string { |
||
755 | |||
756 | |||
757 | /** |
||
758 | * @param string $mimeType |
||
759 | * @param string $extension |
||
760 | * @param string $parsed |
||
761 | * |
||
762 | * @throws KnownFileMimeTypeException |
||
763 | */ |
||
764 | private function parseMimeTypeText(string $mimeType, string $extension, string &$parsed) { |
||
784 | |||
785 | |||
786 | /** |
||
787 | * @param string $mimeType |
||
788 | * @param string $extension |
||
789 | * @param string $parsed |
||
790 | * |
||
791 | * @throws KnownFileMimeTypeException |
||
792 | */ |
||
793 | private function parseMimeTypeTextByExtension( |
||
812 | |||
813 | |||
814 | /** |
||
815 | * @param string $mimeType |
||
816 | * @param string $parsed |
||
817 | * |
||
818 | * @throws KnownFileMimeTypeException |
||
819 | */ |
||
820 | private function parseMimeTypePDF(string $mimeType, string &$parsed) { |
||
827 | |||
828 | |||
829 | /** |
||
830 | * @param string $mimeType |
||
831 | * @param string $parsed |
||
832 | * |
||
833 | * @throws KnownFileMimeTypeException |
||
834 | */ |
||
835 | private function parseMimeTypeZip(string $mimeType, string &$parsed) { |
||
841 | |||
842 | |||
843 | /** |
||
844 | * @param string $mimeType |
||
845 | * @param string $parsed |
||
846 | * |
||
847 | * @throws KnownFileMimeTypeException |
||
848 | */ |
||
849 | private function parseMimeTypeOffice(string $mimeType, string &$parsed) { |
||
868 | |||
869 | |||
870 | /** |
||
871 | * @param FilesDocument $document |
||
872 | * @param File $file |
||
873 | * |
||
874 | * @throws NotPermittedException |
||
875 | */ |
||
876 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
890 | |||
891 | |||
892 | /** |
||
893 | * @param FilesDocument $document |
||
894 | * @param File $file |
||
895 | * |
||
896 | * @throws NotPermittedException |
||
897 | */ |
||
898 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
919 | |||
920 | |||
921 | /** |
||
922 | * @param FilesDocument $document |
||
923 | * @param File $file |
||
924 | * |
||
925 | * @throws NotPermittedException |
||
926 | */ |
||
927 | View Code Duplication | private function extractContentFromFileZip(FilesDocument $document, File $file) { |
|
948 | |||
949 | |||
950 | /** |
||
951 | * @param FilesDocument $document |
||
952 | * @param File $file |
||
953 | * |
||
954 | * @throws NotPermittedException |
||
955 | */ |
||
956 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
977 | |||
978 | |||
979 | /** |
||
980 | * @param FilesDocument $document |
||
981 | * |
||
982 | * @return bool |
||
983 | */ |
||
984 | private function isSourceIndexable(FilesDocument $document): bool { |
||
994 | |||
995 | |||
996 | /** |
||
997 | * @param IIndex $index |
||
998 | */ |
||
999 | private function impersonateOwner(IIndex $index) { |
||
1007 | |||
1008 | |||
1009 | /** |
||
1010 | * @param $action |
||
1011 | * @param bool $force |
||
1012 | * |
||
1013 | * @throws Exception |
||
1014 | */ |
||
1015 | private function updateRunnerAction(string $action, bool $force = false) { |
||
1022 | |||
1023 | |||
1024 | /** |
||
1025 | * @param array $data |
||
1026 | */ |
||
1027 | private function updateRunnerInfo($data) { |
||
1034 | |||
1035 | /** |
||
1036 | * @param IndexDocument $document |
||
1037 | * @param Throwable $t |
||
1038 | */ |
||
1039 | private function manageContentErrorException(IndexDocument $document, Throwable $t) { |
||
1057 | |||
1058 | |||
1059 | /** |
||
1060 | * @param IIndex $index |
||
1061 | */ |
||
1062 | private function updateDirectoryContentIndex(IIndex $index) { |
||
1076 | |||
1077 | |||
1078 | /** |
||
1079 | * @param Folder $node |
||
1080 | */ |
||
1081 | private function updateDirectoryMeta(Folder $node) { |
||
1098 | |||
1099 | |||
1100 | /** |
||
1101 | * @param IIndex $index |
||
1102 | * @param string $message |
||
1103 | * @param string $exception |
||
1104 | * @param int $sev |
||
1105 | */ |
||
1106 | private function updateNewIndexError(IIndex $index, string $message, string $exception, int $sev |
||
1114 | |||
1115 | |||
1116 | /** |
||
1117 | * @param Node $file |
||
1118 | * |
||
1119 | * @throws FileIsNotIndexableException |
||
1120 | */ |
||
1121 | private function isNodeIndexable(Node $file) { |
||
1137 | |||
1138 | |||
1139 | /** |
||
1140 | * @param string $path |
||
1141 | * @param string $userId |
||
1142 | * |
||
1143 | * @param bool $entrySlash |
||
1144 | * |
||
1145 | * @return string |
||
1146 | */ |
||
1147 | private function getPathFromRoot(string $path, string $userId, bool $entrySlash = false) { |
||
1157 | |||
1158 | } |
||
1159 | |||
1160 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to 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
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.