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 FlysystemAssetStore 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 FlysystemAssetStore, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class FlysystemAssetStore implements AssetStore, AssetStoreRouter, Flushable { |
||
27 | |||
28 | /** |
||
29 | * Session key to use for user grants |
||
30 | */ |
||
31 | const GRANTS_SESSION = 'AssetStore_Grants'; |
||
32 | |||
33 | /** |
||
34 | * @var Filesystem |
||
35 | */ |
||
36 | private $publicFilesystem = null; |
||
37 | |||
38 | /** |
||
39 | * Filesystem to use for protected files |
||
40 | * |
||
41 | * @var Filesystem |
||
42 | */ |
||
43 | private $protectedFilesystem = null; |
||
44 | |||
45 | /** |
||
46 | * Enable to use legacy filename behaviour (omits hash) |
||
47 | * |
||
48 | * Note that if using legacy filenames then duplicate files will not work. |
||
49 | * |
||
50 | * @config |
||
51 | * @var bool |
||
52 | */ |
||
53 | private static $legacy_filenames = false; |
||
54 | |||
55 | /** |
||
56 | * Flag if empty folders are allowed. |
||
57 | * If false, empty folders are cleared up when their contents are deleted. |
||
58 | * |
||
59 | * @config |
||
60 | * @var bool |
||
61 | */ |
||
62 | private static $keep_empty_dirs = false; |
||
63 | |||
64 | /** |
||
65 | * Set HTTP error code for requests to secure denied assets. |
||
66 | * Note that this defaults to 404 to prevent information disclosure |
||
67 | * of secure files |
||
68 | * |
||
69 | * @config |
||
70 | * @var int |
||
71 | */ |
||
72 | private static $denied_response_code = 404; |
||
73 | |||
74 | /** |
||
75 | * Set HTTP error code to use for missing secure assets |
||
76 | * |
||
77 | * @config |
||
78 | * @var int |
||
79 | */ |
||
80 | private static $missing_response_code = 404; |
||
81 | |||
82 | /** |
||
83 | * Custom headers to add to all custom file responses |
||
84 | * |
||
85 | * @config |
||
86 | * @var array |
||
87 | */ |
||
88 | private static $file_response_headers = array( |
||
89 | 'Cache-Control' => 'private' |
||
90 | ); |
||
91 | |||
92 | /** |
||
93 | * Assign new flysystem backend |
||
94 | * |
||
95 | * @param Filesystem $filesystem |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setPublicFilesystem(Filesystem $filesystem) { |
||
105 | |||
106 | /** |
||
107 | * Get the currently assigned flysystem backend |
||
108 | * |
||
109 | * @return Filesystem |
||
110 | * @throws LogicException |
||
111 | */ |
||
112 | public function getPublicFilesystem() { |
||
118 | |||
119 | /** |
||
120 | * Assign filesystem to use for non-public files |
||
121 | * |
||
122 | * @param Filesystem $filesystem |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setProtectedFilesystem(Filesystem $filesystem) { |
||
132 | |||
133 | /** |
||
134 | * Get filesystem to use for non-public files |
||
135 | * |
||
136 | * @return Filesystem |
||
137 | * @throws Exception |
||
138 | */ |
||
139 | public function getProtectedFilesystem() { |
||
145 | |||
146 | /** |
||
147 | * Return the store that contains the given fileID |
||
148 | * |
||
149 | * @param string $fileID Internal file identifier |
||
150 | * @return Filesystem |
||
151 | */ |
||
152 | protected function getFilesystemFor($fileID) { |
||
163 | |||
164 | public function getCapabilities() { |
||
178 | |||
179 | public function getVisibility($filename, $hash) { |
||
191 | |||
192 | |||
193 | public function getAsStream($filename, $hash, $variant = null) { |
||
199 | |||
200 | public function getAsString($filename, $hash, $variant = null) { |
||
206 | |||
207 | public function getAsURL($filename, $hash, $variant = null, $grant = true) { |
||
226 | |||
227 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
258 | |||
259 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
273 | |||
274 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
296 | |||
297 | View Code Duplication | public function delete($filename, $hash) { |
|
303 | |||
304 | /** |
||
305 | * Delete the given file (and any variants) in the given {@see Filesystem} |
||
306 | * |
||
307 | * @param string $fileID |
||
308 | * @param Filesystem $filesystem |
||
309 | * @return bool True if a file was deleted |
||
310 | */ |
||
311 | protected function deleteFromFilesystem($fileID, Filesystem $filesystem) { |
||
323 | |||
324 | /** |
||
325 | * Clear directory if it's empty |
||
326 | * |
||
327 | * @param string $dirname Name of directory |
||
328 | * @param Filesystem $filesystem |
||
329 | */ |
||
330 | protected function truncateDirectory($dirname, Filesystem $filesystem) { |
||
339 | |||
340 | /** |
||
341 | * Returns an iterable {@see Generator} of all files / variants for the given $fileID in the given $filesystem |
||
342 | * This includes the empty (no) variant. |
||
343 | * |
||
344 | * @param string $fileID ID of original file to compare with. |
||
345 | * @param Filesystem $filesystem |
||
346 | * @return Generator |
||
347 | */ |
||
348 | protected function findVariants($fileID, Filesystem $filesystem) { |
||
361 | |||
362 | View Code Duplication | public function publish($filename, $hash) { |
|
368 | |||
369 | View Code Duplication | public function protect($filename, $hash) { |
|
375 | |||
376 | /** |
||
377 | * Move a file (and its associative variants) between filesystems |
||
378 | * |
||
379 | * @param string $fileID |
||
380 | * @param Filesystem $from |
||
381 | * @param Filesystem $to |
||
382 | */ |
||
383 | protected function moveBetweenFilesystems($fileID, Filesystem $from, Filesystem $to) { |
||
395 | |||
396 | View Code Duplication | public function grant($filename, $hash) { |
|
402 | |||
403 | View Code Duplication | public function revoke($filename, $hash) { |
|
413 | |||
414 | public function canView($filename, $hash) { |
||
421 | |||
422 | /** |
||
423 | * Determine if a grant exists for the given FileID |
||
424 | * |
||
425 | * @param string $fileID |
||
426 | * @return bool |
||
427 | */ |
||
428 | protected function isGranted($fileID) { |
||
435 | |||
436 | /** |
||
437 | * get sha1 hash from stream |
||
438 | * |
||
439 | * @param resource $stream |
||
440 | * @return string str1 hash |
||
441 | */ |
||
442 | protected function getStreamSHA1($stream) { |
||
448 | |||
449 | /** |
||
450 | * Get stream as a file |
||
451 | * |
||
452 | * @param resource $stream |
||
453 | * @return string Filename of resulting stream content |
||
454 | * @throws Exception |
||
455 | */ |
||
456 | protected function getStreamAsFile($stream) { |
||
473 | |||
474 | /** |
||
475 | * Determine if this stream is seekable |
||
476 | * |
||
477 | * @param resource $stream |
||
478 | * @return bool True if this stream is seekable |
||
479 | */ |
||
480 | protected function isSeekableStream($stream) { |
||
483 | |||
484 | /** |
||
485 | * Invokes the conflict resolution scheme on the given content, and invokes a callback if |
||
486 | * the storage request is approved. |
||
487 | * |
||
488 | * @param callable $callback Will be invoked and passed a fileID if the file should be stored |
||
489 | * @param string $filename Name for the resulting file |
||
490 | * @param string $hash SHA1 of the original file content |
||
491 | * @param string $variant Variant to write |
||
492 | * @param array $config Write options. {@see AssetStore} |
||
493 | * @return array Tuple associative array (Filename, Hash, Variant) |
||
494 | * @throws Exception |
||
495 | */ |
||
496 | protected function writeWithCallback($callback, $filename, $hash, $variant = null, $config = array()) { |
||
560 | |||
561 | /** |
||
562 | * Choose a default conflict resolution |
||
563 | * |
||
564 | * @param string $variant |
||
565 | * @return string |
||
566 | */ |
||
567 | protected function getDefaultConflictResolution($variant) { |
||
578 | |||
579 | /** |
||
580 | * Determine if legacy filenames should be used. These do not have hash path parts. |
||
581 | * |
||
582 | * @return bool |
||
583 | */ |
||
584 | protected function useLegacyFilenames() { |
||
587 | |||
588 | View Code Duplication | public function getMetadata($filename, $hash, $variant = null) { |
|
596 | |||
597 | View Code Duplication | public function getMimeType($filename, $hash, $variant = null) { |
|
605 | |||
606 | public function exists($filename, $hash, $variant = null) { |
||
611 | |||
612 | /** |
||
613 | * Determine the path that should be written to, given the conflict resolution scheme |
||
614 | * |
||
615 | * @param string $conflictResolution |
||
616 | * @param string $fileID |
||
617 | * @return string|false Safe filename to write to. If false, then don't write, and use existing file. |
||
618 | * @throws Exception |
||
619 | */ |
||
620 | protected function resolveConflicts($conflictResolution, $fileID) { |
||
657 | |||
658 | /** |
||
659 | * Get an asset renamer for the given filename. |
||
660 | * |
||
661 | * @param string $fileID Adapter specific identifier for this file/version |
||
662 | * @return AssetNameGenerator |
||
663 | */ |
||
664 | protected function fileGeneratorFor($fileID){ |
||
667 | |||
668 | /** |
||
669 | * Performs filename cleanup before sending it back. |
||
670 | * |
||
671 | * This name should not contain hash or variants. |
||
672 | * |
||
673 | * @param string $filename |
||
674 | * @return string |
||
675 | */ |
||
676 | protected function cleanFilename($filename) { |
||
680 | |||
681 | /** |
||
682 | * Given a FileID, map this back to the original filename, trimming variant and hash |
||
683 | * |
||
684 | * @param string $fileID Adapter specific identifier for this file/version |
||
685 | * @return string Filename for this file, omitting hash and variant |
||
686 | */ |
||
687 | protected function getOriginalFilename($fileID) { |
||
702 | |||
703 | /** |
||
704 | * Remove variant from a fileID |
||
705 | * |
||
706 | * @param string $fileID |
||
707 | * @return string FileID without variant |
||
708 | */ |
||
709 | protected function removeVariant($fileID) { |
||
717 | |||
718 | /** |
||
719 | * Map file tuple (hash, name, variant) to a filename to be used by flysystem |
||
720 | * |
||
721 | * The resulting file will look something like my/directory/EA775CB4D4/filename__variant.jpg |
||
722 | * |
||
723 | * @param string $filename Name of file |
||
724 | * @param string $hash Hash of original file |
||
725 | * @param string $variant (if given) |
||
726 | * @return string Adapter specific identifier for this file/version |
||
727 | */ |
||
728 | protected function getFileID($filename, $hash, $variant = null) { |
||
765 | |||
766 | /** |
||
767 | * Ensure each adapter re-generates its own server configuration files |
||
768 | */ |
||
769 | public static function flush() { |
||
784 | |||
785 | public function getResponseFor($asset) { |
||
805 | |||
806 | /** |
||
807 | * Generate an {@see HTTPResponse} for the given file from the source filesystem |
||
808 | * @param FilesystemInterface $flysystem |
||
809 | * @param string $fileID |
||
810 | * @return HTTPResponse |
||
811 | */ |
||
812 | protected function createResponseFor(FilesystemInterface $flysystem, $fileID) { |
||
827 | |||
828 | /** |
||
829 | * Generate a response for requests to a denied protected file |
||
830 | * |
||
831 | * @return HTTPResponse |
||
832 | */ |
||
833 | protected function createDeniedResponse() { |
||
837 | |||
838 | /** |
||
839 | * Generate a response for missing file requests |
||
840 | * |
||
841 | * @return HTTPResponse |
||
842 | */ |
||
843 | protected function createMissingResponse() { |
||
847 | |||
848 | /** |
||
849 | * Create a response with the given error code |
||
850 | * |
||
851 | * @param int $code |
||
852 | * @return HTTPResponse |
||
853 | */ |
||
854 | protected function createErrorResponse($code) { |
||
864 | } |
||
865 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: