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_IMAGE = 'files_image'; |
||
| 62 | const MIMETYPE_AUDIO = 'files_audio'; |
||
| 63 | |||
| 64 | |||
| 65 | /** @var IAppContainer */ |
||
| 66 | private $container; |
||
| 67 | |||
| 68 | /** @var IRootFolder */ |
||
| 69 | private $rootFolder; |
||
| 70 | |||
| 71 | /** @var IUserManager */ |
||
| 72 | private $userManager; |
||
| 73 | |||
| 74 | /** @var AppManager */ |
||
| 75 | private $appManager; |
||
| 76 | |||
| 77 | /** @var IManager */ |
||
| 78 | private $shareManager; |
||
| 79 | |||
| 80 | /** @var ConfigService */ |
||
| 81 | private $configService; |
||
| 82 | |||
| 83 | /** @var LocalFilesService */ |
||
| 84 | private $localFilesService; |
||
| 85 | |||
| 86 | /** @var ExternalFilesService */ |
||
| 87 | private $externalFilesService; |
||
| 88 | |||
| 89 | /** @var GroupFoldersService */ |
||
| 90 | private $groupFoldersService; |
||
| 91 | |||
| 92 | /** @var ExtensionService */ |
||
| 93 | private $extensionService; |
||
| 94 | |||
| 95 | /** @var MiscService */ |
||
| 96 | private $miscService; |
||
| 97 | |||
| 98 | |||
| 99 | /** @var Runner */ |
||
| 100 | private $runner; |
||
| 101 | |||
| 102 | /** @var int */ |
||
| 103 | private $sumDocuments; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * FilesService constructor. |
||
| 107 | * |
||
| 108 | * @param IAppContainer $container |
||
| 109 | * @param IRootFolder $rootFolder |
||
| 110 | * @param AppManager $appManager |
||
| 111 | * @param IUserManager $userManager |
||
| 112 | * @param IManager $shareManager |
||
| 113 | * @param ConfigService $configService |
||
| 114 | * @param LocalFilesService $localFilesService |
||
| 115 | * @param ExternalFilesService $externalFilesService |
||
| 116 | * @param GroupFoldersService $groupFoldersService |
||
| 117 | * @param ExtensionService $extensionService |
||
| 118 | * @param MiscService $miscService |
||
| 119 | * |
||
| 120 | * @internal param IProviderFactory $factory |
||
| 121 | */ |
||
| 122 | public function __construct( |
||
| 123 | IAppContainer $container, IRootFolder $rootFolder, AppManager $appManager, |
||
| 124 | IUserManager $userManager, IManager $shareManager, |
||
| 125 | ConfigService $configService, LocalFilesService $localFilesService, |
||
| 126 | ExternalFilesService $externalFilesService, GroupFoldersService $groupFoldersService, |
||
| 127 | ExtensionService $extensionService, MiscService $miscService |
||
| 128 | ) { |
||
| 129 | $this->container = $container; |
||
| 130 | $this->rootFolder = $rootFolder; |
||
| 131 | $this->appManager = $appManager; |
||
| 132 | $this->userManager = $userManager; |
||
| 133 | $this->shareManager = $shareManager; |
||
| 134 | |||
| 135 | $this->configService = $configService; |
||
| 136 | $this->localFilesService = $localFilesService; |
||
| 137 | $this->externalFilesService = $externalFilesService; |
||
| 138 | $this->groupFoldersService = $groupFoldersService; |
||
| 139 | $this->extensionService = $extensionService; |
||
| 140 | |||
| 141 | $this->miscService = $miscService; |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | public function setRunner(Runner $runner) { |
||
| 146 | $this->runner = $runner; |
||
| 147 | } |
||
| 148 | |||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $userId |
||
| 152 | * @param IndexOptions $indexOptions |
||
| 153 | * |
||
| 154 | * @return FilesDocument[] |
||
| 155 | * @throws InvalidPathException |
||
| 156 | * @throws NotFoundException |
||
| 157 | */ |
||
| 158 | public function getFilesFromUser($userId, $indexOptions) { |
||
| 159 | |||
| 160 | $this->initFileSystems($userId); |
||
| 161 | $this->sumDocuments = 0; |
||
| 162 | |||
| 163 | /** @var Folder $files */ |
||
| 164 | $files = $this->rootFolder->getUserFolder($userId) |
||
| 165 | ->get($indexOptions->getOption('path', '/')); |
||
| 166 | |||
| 167 | if ($files instanceof Folder) { |
||
|
|
|||
| 168 | $result = $this->getFilesFromDirectory($userId, $files); |
||
| 169 | } else { |
||
| 170 | $result = []; |
||
| 171 | try { |
||
| 172 | $result[] = $this->generateFilesDocumentFromFile($userId, $files); |
||
| 173 | } catch (FileIsNotIndexableException $e) { |
||
| 174 | /** we do nothin' */ |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return $result; |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $userId |
||
| 184 | */ |
||
| 185 | private function initFileSystems($userId) { |
||
| 186 | if ($userId === '') { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->externalFilesService->initExternalFilesForUser($userId); |
||
| 191 | $this->groupFoldersService->initGroupSharesForUser($userId); |
||
| 192 | } |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $userId |
||
| 197 | * @param Folder $node |
||
| 198 | * |
||
| 199 | * @return FilesDocument[] |
||
| 200 | * @throws InvalidPathException |
||
| 201 | * @throws NotFoundException |
||
| 202 | * @throws Exception |
||
| 203 | */ |
||
| 204 | public function getFilesFromDirectory($userId, Folder $node) { |
||
| 205 | $documents = []; |
||
| 206 | |||
| 207 | $this->updateRunnerAction('generateIndexFiles'); |
||
| 208 | $this->updateRunnerInfo( |
||
| 209 | [ |
||
| 210 | 'info' => $node->getPath(), |
||
| 211 | 'documentTotal' => $this->sumDocuments |
||
| 212 | ] |
||
| 213 | ); |
||
| 214 | |||
| 215 | try { |
||
| 216 | if ($node->nodeExists('.noindex')) { |
||
| 217 | return $documents; |
||
| 218 | } |
||
| 219 | } catch (StorageNotAvailableException $e) { |
||
| 220 | return $documents; |
||
| 221 | } |
||
| 222 | |||
| 223 | $files = $node->getDirectoryListing(); |
||
| 224 | foreach ($files as $file) { |
||
| 225 | |||
| 226 | try { |
||
| 227 | $documents[] = $this->generateFilesDocumentFromFile($userId, $file); |
||
| 228 | $this->sumDocuments++; |
||
| 229 | } catch (FileIsNotIndexableException $e) { |
||
| 230 | continue; |
||
| 231 | } |
||
| 232 | |||
| 233 | if ($file->getType() === FileInfo::TYPE_FOLDER) { |
||
| 234 | /** @var $file Folder */ |
||
| 235 | $documents = |
||
| 236 | array_merge($documents, $this->getFilesFromDirectory($userId, $file)); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | return $documents; |
||
| 241 | } |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * @param Node $file |
||
| 246 | * |
||
| 247 | * @param string $viewerId |
||
| 248 | * |
||
| 249 | * @return FilesDocument |
||
| 250 | * @throws FileIsNotIndexableException |
||
| 251 | * @throws InvalidPathException |
||
| 252 | * @throws NotFoundException |
||
| 253 | * @throws Exception |
||
| 254 | */ |
||
| 255 | private function generateFilesDocumentFromFile($viewerId, Node $file) { |
||
| 256 | |||
| 257 | $source = $this->getFileSource($file); |
||
| 258 | $document = new FilesDocument(FilesProvider::FILES_PROVIDER_ID, $file->getId()); |
||
| 259 | |||
| 260 | if ($file->getId() === -1) { |
||
| 261 | throw new FileIsNotIndexableException(); |
||
| 262 | } |
||
| 263 | |||
| 264 | $ownerId = ''; |
||
| 265 | if ($file->getOwner() !== null) { |
||
| 266 | $ownerId = $file->getOwner() |
||
| 267 | ->getUID(); |
||
| 268 | } |
||
| 269 | $document->setType($file->getType()) |
||
| 270 | ->setSource($source) |
||
| 271 | ->setOwnerId($ownerId) |
||
| 272 | ->setPath($this->getPathFromViewerId($file->getId(), $viewerId)) |
||
| 273 | ->setViewerId($viewerId) |
||
| 274 | ->setModifiedTime($file->getMTime()) |
||
| 275 | ->setMimetype($file->getMimetype()); |
||
| 276 | |||
| 277 | return $document; |
||
| 278 | } |
||
| 279 | |||
| 280 | |||
| 281 | /** |
||
| 282 | * @param Node $file |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | * @throws FileIsNotIndexableException |
||
| 286 | * @throws NotFoundException |
||
| 287 | */ |
||
| 288 | private function getFileSource(Node $file) { |
||
| 289 | $source = ''; |
||
| 290 | |||
| 291 | try { |
||
| 292 | $this->localFilesService->getFileSource($file, $source); |
||
| 293 | $this->externalFilesService->getFileSource($file, $source); |
||
| 294 | $this->groupFoldersService->getFileSource($file, $source); |
||
| 295 | } catch (KnownFileSourceException $e) { |
||
| 296 | /** we know the source, just leave. */ |
||
| 297 | } |
||
| 298 | |||
| 299 | return $source; |
||
| 300 | } |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $userId |
||
| 305 | * @param string $path |
||
| 306 | * |
||
| 307 | * @return Node |
||
| 308 | * @throws NotFoundException |
||
| 309 | */ |
||
| 310 | public function getFileFromPath($userId, $path) { |
||
| 311 | return $this->rootFolder->getUserFolder($userId) |
||
| 312 | ->get($path); |
||
| 313 | } |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $userId |
||
| 318 | * @param int $fileId |
||
| 319 | * |
||
| 320 | * @return Node |
||
| 321 | * @throws FilesNotFoundException |
||
| 322 | * @throws EmptyUserException |
||
| 323 | */ |
||
| 324 | public function getFileFromId($userId, $fileId) { |
||
| 325 | |||
| 326 | if ($userId === '') { |
||
| 327 | throw new EmptyUserException(); |
||
| 328 | } |
||
| 329 | |||
| 330 | $files = $this->rootFolder->getUserFolder($userId) |
||
| 331 | ->getById($fileId); |
||
| 332 | if (sizeof($files) === 0) { |
||
| 333 | throw new FilesNotFoundException(); |
||
| 334 | } |
||
| 335 | |||
| 336 | $file = array_shift($files); |
||
| 337 | |||
| 338 | return $file; |
||
| 339 | } |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @param Index $index |
||
| 344 | * |
||
| 345 | * @return Node |
||
| 346 | * @throws EmptyUserException |
||
| 347 | * @throws FilesNotFoundException |
||
| 348 | */ |
||
| 349 | public function getFileFromIndex(Index $index) { |
||
| 350 | $this->impersonateOwner($index); |
||
| 351 | |||
| 352 | return $this->getFileFromId($index->getOwnerId(), $index->getDocumentId()); |
||
| 353 | } |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * @param int $fileId |
||
| 358 | * @param string $viewerId |
||
| 359 | * |
||
| 360 | * @throws Exception |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | private function getPathFromViewerId($fileId, $viewerId) { |
||
| 364 | |||
| 365 | $viewerFiles = $this->rootFolder->getUserFolder($viewerId) |
||
| 366 | ->getById($fileId); |
||
| 367 | |||
| 368 | if (sizeof($viewerFiles) === 0) { |
||
| 369 | return ''; |
||
| 370 | } |
||
| 371 | |||
| 372 | $file = array_shift($viewerFiles); |
||
| 373 | |||
| 374 | // TODO: better way to do this : we remove the '/userid/files/' |
||
| 375 | $path = MiscService::noEndSlash(substr($file->getPath(), 8 + strlen($viewerId))); |
||
| 376 | |||
| 377 | return $path; |
||
| 378 | } |
||
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * @param FilesDocument $document |
||
| 383 | */ |
||
| 384 | public function generateDocument(FilesDocument $document) { |
||
| 385 | |||
| 386 | try { |
||
| 387 | $this->updateFilesDocument($document); |
||
| 388 | } catch (Exception $e) { |
||
| 389 | // TODO - update $document with a error status instead of just ignore ! |
||
| 390 | $document->getIndex() |
||
| 391 | ->setStatus(Index::INDEX_IGNORE); |
||
| 392 | echo 'Exception: ' . json_encode($e->getTrace()) . ' - ' . $e->getMessage() |
||
| 393 | . "\n"; |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * @param Index $index |
||
| 400 | * |
||
| 401 | * @return FilesDocument |
||
| 402 | * @throws FileIsNotIndexableException |
||
| 403 | * @throws InvalidPathException |
||
| 404 | * @throws NotFoundException |
||
| 405 | * @throws NotPermittedException |
||
| 406 | */ |
||
| 407 | private function generateDocumentFromIndex(Index $index) { |
||
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * @param IndexDocument $document |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | */ |
||
| 433 | public function isDocumentUpToDate($document) { |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * @param Index $index |
||
| 457 | * |
||
| 458 | * @return FilesDocument |
||
| 459 | * @throws InvalidPathException |
||
| 460 | * @throws NotFoundException |
||
| 461 | * @throws NotPermittedException |
||
| 462 | * @throws FileIsNotIndexableException |
||
| 463 | */ |
||
| 464 | public function updateDocument(Index $index) { |
||
| 465 | $this->impersonateOwner($index); |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @param FilesDocument $document |
||
| 474 | * |
||
| 475 | * @throws InvalidPathException |
||
| 476 | * @throws NotFoundException |
||
| 477 | * @throws NotPermittedException |
||
| 478 | */ |
||
| 479 | private function updateFilesDocument(FilesDocument $document) { |
||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * @param FilesDocument $document |
||
| 494 | * @param Node $file |
||
| 495 | * |
||
| 496 | * @throws InvalidPathException |
||
| 497 | * @throws NotFoundException |
||
| 498 | * @throws NotPermittedException |
||
| 499 | */ |
||
| 500 | private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) { |
||
| 510 | |||
| 511 | |||
| 512 | /** |
||
| 513 | * @param FilesDocument $document |
||
| 514 | * @param Node $file |
||
| 515 | */ |
||
| 516 | private function updateDocumentAccess(FilesDocument $document, Node $file) { |
||
| 531 | |||
| 532 | |||
| 533 | /** |
||
| 534 | * @param FilesDocument $document |
||
| 535 | * @param Node $file |
||
| 536 | * |
||
| 537 | * @throws InvalidPathException |
||
| 538 | * @throws NotFoundException |
||
| 539 | * @throws NotPermittedException |
||
| 540 | */ |
||
| 541 | private function updateContentFromFile(FilesDocument $document, Node $file) { |
||
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * @param FilesDocument $document |
||
| 570 | * @param Node $file |
||
| 571 | * |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | private function updateShareNames(FilesDocument $document, Node $file) { |
||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * @param string $mimeType |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | private function parseMimeType($mimeType) { |
||
| 624 | |||
| 625 | |||
| 626 | /** |
||
| 627 | * @param string $mimeType |
||
| 628 | * @param string $parsed |
||
| 629 | * |
||
| 630 | * @throws KnownFileMimeTypeException |
||
| 631 | */ |
||
| 632 | private function parseMimeTypeText($mimeType, &$parsed) { |
||
| 650 | |||
| 651 | |||
| 652 | /** |
||
| 653 | * @param string $mimeType |
||
| 654 | * @param string $parsed |
||
| 655 | * |
||
| 656 | * @throws KnownFileMimeTypeException |
||
| 657 | */ |
||
| 658 | private function parseMimeTypePDF($mimeType, &$parsed) { |
||
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * @param string $mimeType |
||
| 669 | * @param string $parsed |
||
| 670 | * |
||
| 671 | * @throws KnownFileMimeTypeException |
||
| 672 | */ |
||
| 673 | private function parseMimeTypeOffice($mimeType, &$parsed) { |
||
| 692 | |||
| 693 | |||
| 694 | /** |
||
| 695 | * @param FilesDocument $document |
||
| 696 | * @param File $file |
||
| 697 | * |
||
| 698 | * @throws NotPermittedException |
||
| 699 | */ |
||
| 700 | private function extractContentFromFileText(FilesDocument $document, File $file) { |
||
| 714 | |||
| 715 | |||
| 716 | /** |
||
| 717 | * @param FilesDocument $document |
||
| 718 | * @param File $file |
||
| 719 | * |
||
| 720 | * @throws NotPermittedException |
||
| 721 | */ |
||
| 722 | View Code Duplication | private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
| 742 | |||
| 743 | |||
| 744 | /** |
||
| 745 | * @param FilesDocument $document |
||
| 746 | * @param File $file |
||
| 747 | * |
||
| 748 | * @throws NotPermittedException |
||
| 749 | */ |
||
| 750 | View Code Duplication | private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
| 770 | |||
| 771 | |||
| 772 | /** |
||
| 773 | * @param FilesDocument $document |
||
| 774 | * |
||
| 775 | * @return bool |
||
| 776 | */ |
||
| 777 | private function isSourceIndexable(FilesDocument $document) { |
||
| 787 | |||
| 788 | |||
| 789 | private function impersonateOwner(Index $index) { |
||
| 797 | |||
| 798 | |||
| 799 | /** |
||
| 800 | * @param $action |
||
| 801 | * @param bool $force |
||
| 802 | * |
||
| 803 | * @throws Exception |
||
| 804 | */ |
||
| 805 | private function updateRunnerAction($action, $force = false) { |
||
| 812 | |||
| 813 | |||
| 814 | /** |
||
| 815 | * @param array $data |
||
| 816 | */ |
||
| 817 | private function updateRunnerInfo($data) { |
||
| 824 | |||
| 825 | |||
| 826 | } |
||
| 827 | |||
| 828 |
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.