Complex classes like FlysystemDriver 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 FlysystemDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | abstract class FlysystemDriver extends AbstractHierarchicalFilesystemDriver |
||
47 | { |
||
48 | /** |
||
49 | * @var FilesystemInterface |
||
50 | */ |
||
51 | protected $filesystem; |
||
52 | |||
53 | /** |
||
54 | * @var AdapterInterface |
||
55 | */ |
||
56 | protected $adapter; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $entryPath; |
||
62 | |||
63 | /** |
||
64 | * FlysystemDriver constructor. |
||
65 | * @param array $configuration |
||
66 | */ |
||
67 | 60 | public function __construct(array $configuration = []) |
|
76 | |||
77 | /** |
||
78 | * Processes the configuration for this driver. |
||
79 | * @return void |
||
80 | */ |
||
81 | public function processConfiguration() |
||
85 | |||
86 | /** |
||
87 | * Merges the capabilities merged by the user at the storage |
||
88 | * configuration into the actual capabilities of the driver |
||
89 | * and returns the result. |
||
90 | * |
||
91 | * @param int $capabilities |
||
92 | * @return int |
||
93 | */ |
||
94 | public function mergeConfigurationCapabilities($capabilities) |
||
99 | |||
100 | /** |
||
101 | * Returns the identifier of the root level folder of the storage. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getRootLevelFolder() |
||
109 | |||
110 | /** |
||
111 | * Returns the identifier of the default folder new files should be put into. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 3 | public function getDefaultFolder() |
|
124 | |||
125 | /** |
||
126 | * Checks if a folder exists. |
||
127 | * |
||
128 | * @param string $folderIdentifier |
||
129 | * @return bool |
||
130 | */ |
||
131 | 9 | public function folderExists($folderIdentifier) |
|
145 | |||
146 | /** |
||
147 | * Creates a folder, within a parent folder. |
||
148 | * If no parent folder is given, a root level folder will be created |
||
149 | * |
||
150 | * @param string $newFolderName |
||
151 | * @param string $parentFolderIdentifier |
||
152 | * @param bool $recursive |
||
153 | * @return string the Identifier of the new folder |
||
154 | */ |
||
155 | 9 | public function createFolder($newFolderName, $parentFolderIdentifier = '', $recursive = false) |
|
166 | |||
167 | /** |
||
168 | * Returns the public URL to a file. |
||
169 | * Either fully qualified URL or relative to PATH_site (rawurlencoded). |
||
170 | * |
||
171 | * @param string $identifier |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getPublicUrl($identifier) |
||
178 | |||
179 | /** |
||
180 | * Renames a folder in this storage. |
||
181 | * |
||
182 | * @param string $folderIdentifier |
||
183 | * @param string $newName |
||
184 | * @return array A map of old to new file identifiers of all affected resources |
||
185 | */ |
||
186 | 3 | public function renameFolder($folderIdentifier, $newName) |
|
196 | |||
197 | /** |
||
198 | * Removes a folder in filesystem. |
||
199 | * |
||
200 | * @param string $folderIdentifier |
||
201 | * @param bool $deleteRecursively |
||
202 | * @return bool |
||
203 | * @throws FileOperationErrorException |
||
204 | */ |
||
205 | public function deleteFolder($folderIdentifier, $deleteRecursively = false) |
||
217 | |||
218 | /** |
||
219 | * Checks if a file exists. |
||
220 | * |
||
221 | * @param string $fileIdentifier |
||
222 | * @return bool |
||
223 | */ |
||
224 | 12 | public function fileExists($fileIdentifier) |
|
231 | |||
232 | /** |
||
233 | * Checks if a folder contains files and (if supported) other folders. |
||
234 | * |
||
235 | * @param string $folderIdentifier |
||
236 | * @return bool TRUE if there are no files and folders within $folder |
||
237 | */ |
||
238 | 3 | public function isFolderEmpty($folderIdentifier) |
|
242 | |||
243 | /** |
||
244 | * Adds a file from the local server hard disk to a given path in TYPO3s |
||
245 | * virtual file system. This assumes that the local file exists, so no |
||
246 | * further check is done here! After a successful the original file must |
||
247 | * not exist anymore. |
||
248 | * |
||
249 | * @param string $localFilePath (within PATH_site) |
||
250 | * @param string $targetFolderIdentifier |
||
251 | * @param string $newFileName optional, if not given original name is used |
||
252 | * @param bool $removeOriginal if set the original file will be removed |
||
253 | * after successful operation |
||
254 | * @return string the identifier of the new file |
||
255 | */ |
||
256 | 3 | public function addFile($localFilePath, $targetFolderIdentifier, $newFileName = '', $removeOriginal = true) |
|
282 | |||
283 | /** |
||
284 | * Creates a new (empty) file and returns the identifier. |
||
285 | * |
||
286 | * @param string $fileName |
||
287 | * @param string $parentFolderIdentifier |
||
288 | * @return string |
||
289 | * @throws InvalidFileNameException |
||
290 | */ |
||
291 | 3 | public function createFile($fileName, $parentFolderIdentifier) |
|
314 | |||
315 | /** |
||
316 | * Copies a file *within* the current storage. |
||
317 | * Note that this is only about an inner storage copy action, |
||
318 | * where a file is just copied to another folder in the same storage. |
||
319 | * |
||
320 | * @param string $fileIdentifier |
||
321 | * @param string $targetFolderIdentifier |
||
322 | * @param string $fileName |
||
323 | * @return string the Identifier of the new file |
||
324 | */ |
||
325 | public function copyFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $fileName) |
||
334 | |||
335 | /** |
||
336 | * Renames a file in this storage. |
||
337 | * |
||
338 | * @param string $fileIdentifier |
||
339 | * @param string $newName The target path (including the file name!) |
||
340 | * @return string The identifier of the file after renaming |
||
341 | */ |
||
342 | public function renameFile($fileIdentifier, $newName) |
||
350 | |||
351 | /** |
||
352 | * Replaces a file with file in local file system. |
||
353 | * |
||
354 | * @param string $fileIdentifier |
||
355 | * @param string $localFilePath |
||
356 | * @return bool TRUE if the operation succeeded |
||
357 | */ |
||
358 | 2 | public function replaceFile($fileIdentifier, $localFilePath) |
|
366 | |||
367 | /** |
||
368 | * Removes a file from the filesystem. This does not check if the file is |
||
369 | * still used or if it is a bad idea to delete it for some other reason |
||
370 | * this has to be taken care of in the upper layers (e.g. the Storage)! |
||
371 | * |
||
372 | * @param string $fileIdentifier |
||
373 | * @return bool TRUE if deleting the file succeeded |
||
374 | */ |
||
375 | 3 | public function deleteFile($fileIdentifier) |
|
379 | |||
380 | /** |
||
381 | * Creates a hash for a file. |
||
382 | * |
||
383 | * @param string $fileIdentifier |
||
384 | * @param string $hashAlgorithm The hash algorithm to use |
||
385 | * @return string |
||
386 | */ |
||
387 | 9 | public function hash($fileIdentifier, $hashAlgorithm) |
|
408 | |||
409 | /** |
||
410 | * Moves a file *within* the current storage. |
||
411 | * Note that this is only about an inner-storage move action, |
||
412 | * where a file is just moved to another folder in the same storage. |
||
413 | * |
||
414 | * @param string $fileIdentifier |
||
415 | * @param string $targetFolderIdentifier |
||
416 | * @param string $newFileName |
||
417 | * @return string |
||
418 | */ |
||
419 | public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $newFileName) |
||
423 | |||
424 | /** |
||
425 | * Folder equivalent to moveFileWithinStorage(). |
||
426 | * |
||
427 | * @param string $sourceFolderIdentifier |
||
428 | * @param string $targetFolderIdentifier |
||
429 | * @param string $newFolderName |
||
430 | * @return array All files which are affected, map of old => new file identifiers |
||
431 | */ |
||
432 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
436 | |||
437 | /** |
||
438 | * Folder equivalent to copyFileWithinStorage(). |
||
439 | * |
||
440 | * @param string $sourceFolderIdentifier |
||
441 | * @param string $targetFolderIdentifier |
||
442 | * @param string $newFolderName |
||
443 | * @return bool |
||
444 | */ |
||
445 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
449 | |||
450 | /** |
||
451 | * Returns the contents of a file. Beware that this requires to load the |
||
452 | * complete file into memory and also may require fetching the file from an |
||
453 | * external location. So this might be an expensive operation (both in terms |
||
454 | * of processing resources and money) for large files. |
||
455 | * |
||
456 | * @param string $fileIdentifier |
||
457 | * @return string The file contents |
||
458 | */ |
||
459 | 3 | public function getFileContents($fileIdentifier) |
|
463 | |||
464 | /** |
||
465 | * Sets the contents of a file to the specified value. |
||
466 | * |
||
467 | * @param string $fileIdentifier |
||
468 | * @param string $contents |
||
469 | * @return int The number of bytes written to the file |
||
470 | */ |
||
471 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
477 | |||
478 | /** |
||
479 | * Checks if a file inside a folder exists |
||
480 | * |
||
481 | * @param string $fileName |
||
482 | * @param string $folderIdentifier |
||
483 | * @return bool |
||
484 | */ |
||
485 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
491 | |||
492 | /** |
||
493 | * Checks if a folder inside a folder exists. |
||
494 | * |
||
495 | * @param string $folderName |
||
496 | * @param string $folderIdentifier |
||
497 | * @return bool |
||
498 | */ |
||
499 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
505 | |||
506 | /** |
||
507 | * Returns a path to a local copy of a file for processing it. When changing the |
||
508 | * file, you have to take care of replacing the current version yourself! |
||
509 | * |
||
510 | * @param string $fileIdentifier |
||
511 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
512 | * operations. This might speed up things, e.g. by using |
||
513 | * a cached local version. Never modify the file if you |
||
514 | * have set this flag! |
||
515 | * @return string The path to the file on the local disk |
||
516 | */ |
||
517 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
525 | |||
526 | /** |
||
527 | * Returns the permissions of a file/folder as an array |
||
528 | * (keys r, w) of boolean flags |
||
529 | * |
||
530 | * @param string $identifier |
||
531 | * @return array |
||
532 | */ |
||
533 | public function getPermissions($identifier) |
||
540 | |||
541 | /** |
||
542 | * Directly output the contents of the file to the output |
||
543 | * buffer. Should not take care of header files or flushing |
||
544 | * buffer before. Will be taken care of by the Storage. |
||
545 | * |
||
546 | * @param string $identifier |
||
547 | * @return void |
||
548 | */ |
||
549 | public function dumpFileContents($identifier) |
||
556 | |||
557 | /** |
||
558 | * Checks if a given identifier is within a container, e.g. if |
||
559 | * a file or folder is within another folder. |
||
560 | * This can e.g. be used to check for web-mounts. |
||
561 | * |
||
562 | * Hint: this also needs to return TRUE if the given identifier |
||
563 | * matches the container identifier to allow access to the root |
||
564 | * folder of a filemount. |
||
565 | * |
||
566 | * @param string $folderIdentifier |
||
567 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
568 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
569 | */ |
||
570 | public function isWithin($folderIdentifier, $identifier) |
||
584 | |||
585 | /** |
||
586 | * Returns information about a file. |
||
587 | * |
||
588 | * @param string $fileIdentifier |
||
589 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
590 | * If empty all will be extracted |
||
591 | * @return array |
||
592 | * @throws FileDoesNotExistException |
||
593 | */ |
||
594 | 6 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
|
604 | |||
605 | /** |
||
606 | * Returns information about a file. |
||
607 | * |
||
608 | * @param string $folderIdentifier |
||
609 | * @return array |
||
610 | * @throws FolderDoesNotExistException |
||
611 | */ |
||
612 | 3 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
628 | |||
629 | /** |
||
630 | * Returns the identifier of a file inside the folder |
||
631 | * |
||
632 | * @param string $fileName |
||
633 | * @param string $folderIdentifier |
||
634 | * @return string file identifier |
||
635 | */ |
||
636 | public function getFileInFolder($fileName, $folderIdentifier) |
||
640 | |||
641 | /** |
||
642 | * Returns a list of files inside the specified path |
||
643 | * |
||
644 | * @param string $folderIdentifier |
||
645 | * @param int $start |
||
646 | * @param int $numberOfItems |
||
647 | * @param bool $recursive |
||
648 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
649 | * @param string $sort Property name used to sort the items. |
||
650 | * Among them may be: '' (empty, no sorting), name, |
||
651 | * fileext, size, tstamp and rw. |
||
652 | * If a driver does not support the given property, it |
||
653 | * should fall back to "name". |
||
654 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
655 | * @return array of FileIdentifiers |
||
656 | */ |
||
657 | 3 | public function getFilesInFolder( |
|
681 | |||
682 | /** |
||
683 | * Returns the identifier of a folder inside the folder |
||
684 | * |
||
685 | * @param string $folderName The name of the target folder |
||
686 | * @param string $folderIdentifier |
||
687 | * @return string folder identifier |
||
688 | */ |
||
689 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
694 | |||
695 | /** |
||
696 | * Returns a list of folders inside the specified path |
||
697 | * |
||
698 | * @param string $folderIdentifier |
||
699 | * @param int $start |
||
700 | * @param int $numberOfItems |
||
701 | * @param bool $recursive |
||
702 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
703 | * @param string $sort Property name used to sort the items. |
||
704 | * Among them may be: '' (empty, no sorting), name, |
||
705 | * fileext, size, tstamp and rw. |
||
706 | * If a driver does not support the given property, it |
||
707 | * should fall back to "name". |
||
708 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
709 | * @return array of Folder Identifier |
||
710 | * @TODO: Implement pagination with $start and $numberOfItems |
||
711 | * @TODO: Implement directory filter callbacks |
||
712 | * @TODO: Implement sorting |
||
713 | */ |
||
714 | 3 | public function getFoldersInFolder( |
|
739 | |||
740 | /** |
||
741 | * Returns the number of files inside the specified path |
||
742 | * |
||
743 | * @param string $folderIdentifier |
||
744 | * @param bool $recursive |
||
745 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
746 | * @return int Number of files in folder |
||
747 | * @TODO: Implement recursive count |
||
748 | * @TODO: Implement filename filtering |
||
749 | */ |
||
750 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
755 | |||
756 | /** |
||
757 | * Returns the number of folders inside the specified path |
||
758 | * |
||
759 | * @param string $folderIdentifier |
||
760 | * @param bool $recursive |
||
761 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
762 | * @return int Number of folders in folder |
||
763 | */ |
||
764 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
777 | |||
778 | /** |
||
779 | * Extracts information about a file from the filesystem. |
||
780 | * |
||
781 | * @param string $filePath The absolute path to the file |
||
782 | * @param string $containerPath The relative path to the file's container |
||
783 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
784 | * @return array |
||
785 | */ |
||
786 | 6 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
|
809 | |||
810 | /** |
||
811 | * Extracts a specific FileInformation from the FileSystems. |
||
812 | * |
||
813 | * @param string $fileIdentifier |
||
814 | * @param string $containerPath |
||
815 | * @param string $property |
||
816 | * |
||
817 | * @return bool|int|string |
||
818 | * @throws \InvalidArgumentException |
||
819 | */ |
||
820 | 6 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
|
850 | } |
||
851 |