Complex classes like FlysystemStreamWrapper 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 FlysystemStreamWrapper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class FlysystemStreamWrapper |
||
21 | { |
||
22 | /** |
||
23 | * A flag to tell FlysystemStreamWrapper::url_stat() to ignore the size. |
||
24 | * |
||
25 | * @var int |
||
26 | */ |
||
27 | const STREAM_URL_IGNORE_SIZE = 8; |
||
28 | |||
29 | /** |
||
30 | * The registered filesystems. |
||
31 | * |
||
32 | * @var \League\Flysystem\FilesystemInterface[] |
||
33 | */ |
||
34 | protected static $filesystems = []; |
||
35 | |||
36 | /** |
||
37 | * Optional configuration. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected static $config = []; |
||
42 | |||
43 | /** |
||
44 | * The default configuration. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected static $defaultConfiguration = [ |
||
49 | 'permissions' =>[ |
||
50 | 'dir' => [ |
||
51 | 'private' => 0700, |
||
52 | 'public' => 0755, |
||
53 | ], |
||
54 | 'file' => [ |
||
55 | 'private' => 0600, |
||
56 | 'public' => 0644, |
||
57 | ], |
||
58 | ], |
||
59 | 'metadata' => ['timestamp', 'size', 'visibility'], |
||
60 | 'public_mask' => 0044, |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * The number of bytes that have been written since the last flush. |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $bytesWritten = 0; |
||
69 | |||
70 | /** |
||
71 | * The filesystem of the current stream wrapper. |
||
72 | * |
||
73 | * @var \League\Flysystem\FilesystemInterface |
||
74 | */ |
||
75 | protected $filesystem; |
||
76 | |||
77 | /** |
||
78 | * A generic resource handle. |
||
79 | * |
||
80 | * @var resource|bool |
||
81 | */ |
||
82 | protected $handle; |
||
83 | |||
84 | /** |
||
85 | * Whether the handle is in append mode. |
||
86 | * |
||
87 | * @var bool |
||
88 | */ |
||
89 | protected $isAppendMode = false; |
||
90 | |||
91 | /** |
||
92 | * Whether the handle is read-only. |
||
93 | * |
||
94 | * The stream returned from Flysystem may not actually be read-only, This |
||
95 | * ensures read-only behavior. |
||
96 | * |
||
97 | * @var bool |
||
98 | */ |
||
99 | protected $isReadOnly = false; |
||
100 | |||
101 | /** |
||
102 | * Whether the handle is write-only. |
||
103 | * |
||
104 | * @var bool |
||
105 | */ |
||
106 | protected $isWriteOnly = false; |
||
107 | |||
108 | /** |
||
109 | * A directory listing. |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | protected $listing; |
||
114 | |||
115 | /** |
||
116 | * Whether this handle has been verified writable. |
||
117 | * |
||
118 | * @var bool |
||
119 | */ |
||
120 | protected $needsCowCheck = false; |
||
121 | |||
122 | /** |
||
123 | * Whether the handle should be flushed. |
||
124 | * |
||
125 | * @var bool |
||
126 | */ |
||
127 | protected $needsFlush = false; |
||
128 | |||
129 | /** |
||
130 | * The handle used for calls to stream_lock. |
||
131 | * |
||
132 | * @var resource |
||
133 | */ |
||
134 | protected $lockHandle; |
||
135 | |||
136 | /** |
||
137 | * If stream_set_write_buffer() is called, the arguments. |
||
138 | * |
||
139 | * @var int |
||
140 | */ |
||
141 | protected $streamWriteBuffer; |
||
142 | |||
143 | /** |
||
144 | * Instance URI (stream). |
||
145 | * |
||
146 | * A stream is referenced as "protocol://target". |
||
147 | * |
||
148 | * @var string |
||
149 | */ |
||
150 | protected $uri; |
||
151 | |||
152 | /** |
||
153 | * Registers the stream wrapper protocol if not already registered. |
||
154 | * |
||
155 | * @param string $protocol The protocol. |
||
156 | * @param FilesystemInterface $filesystem The filesystem. |
||
157 | * @param array|null $configuration Optional configuration. |
||
158 | * |
||
159 | * @return bool True if the protocal was registered, false if not. |
||
160 | */ |
||
161 | 198 | public static function register($protocol, FilesystemInterface $filesystem, array $configuration = null) |
|
173 | |||
174 | /** |
||
175 | * Unegisters a stream wrapper. |
||
176 | * |
||
177 | * @param string $protocol The protocol. |
||
178 | * |
||
179 | * @return bool True if the protocal was unregistered, false if not. |
||
180 | */ |
||
181 | 195 | public static function unregister($protocol) |
|
191 | |||
192 | /** |
||
193 | * Determines if a protocol is registered. |
||
194 | * |
||
195 | * @param string $protocol The protocol to check. |
||
196 | * |
||
197 | * @return bool True if it is registered, false if not. |
||
198 | */ |
||
199 | 198 | protected static function streamWrapperExists($protocol) |
|
203 | |||
204 | /** |
||
205 | * Registers plugins on the filesystem. |
||
206 | * |
||
207 | * @param string $protocol |
||
208 | * @param FilesystemInterface $filesystem |
||
209 | */ |
||
210 | 198 | protected static function registerPlugins($protocol, FilesystemInterface $filesystem) |
|
224 | |||
225 | /** |
||
226 | * Closes the directory handle. |
||
227 | * |
||
228 | * @return bool True on success, false on failure. |
||
229 | */ |
||
230 | 12 | public function dir_closedir() |
|
236 | |||
237 | /** |
||
238 | * Opens a directory handle. |
||
239 | * |
||
240 | * @param string $uri The URL that was passed to opendir(). |
||
241 | * @param int $options Whether or not to enforce safe_mode (0x04). |
||
242 | * |
||
243 | * @return bool True on success, false on failure. |
||
244 | */ |
||
245 | 18 | public function dir_opendir($uri, $options) |
|
273 | |||
274 | /** |
||
275 | * Reads an entry from directory handle. |
||
276 | * |
||
277 | * @return string|bool The next filename, or false if there is no next file. |
||
278 | */ |
||
279 | 12 | public function dir_readdir() |
|
286 | |||
287 | /** |
||
288 | * Rewinds the directory handle. |
||
289 | * |
||
290 | * @return bool True on success, false on failure. |
||
291 | */ |
||
292 | 12 | public function dir_rewinddir() |
|
298 | |||
299 | /** |
||
300 | * Creates a directory. |
||
301 | * |
||
302 | * @param string $uri |
||
303 | * @param int $mode |
||
304 | * @param int $options |
||
305 | * |
||
306 | * @return bool True on success, false on failure. |
||
307 | */ |
||
308 | 60 | public function mkdir($uri, $mode, $options) |
|
314 | |||
315 | /** |
||
316 | * Renames a file or directory. |
||
317 | * |
||
318 | * @param string $uri_from |
||
319 | * @param string $uri_to |
||
320 | * |
||
321 | * @return bool True on success, false on failure. |
||
322 | */ |
||
323 | 36 | public function rename($uri_from, $uri_to) |
|
330 | |||
331 | /** |
||
332 | * Removes a directory. |
||
333 | * |
||
334 | * @param string $uri |
||
335 | * @param int $options |
||
336 | * |
||
337 | * @return bool True on success, false on failure. |
||
338 | */ |
||
339 | 18 | public function rmdir($uri, $options) |
|
345 | |||
346 | /** |
||
347 | * Retrieves the underlaying resource. |
||
348 | * |
||
349 | * @param int $cast_as |
||
350 | * |
||
351 | * @return resource|bool The stream resource used by the wrapper, or false. |
||
352 | */ |
||
353 | 6 | public function stream_cast($cast_as) |
|
357 | |||
358 | /** |
||
359 | * Closes the resource. |
||
360 | */ |
||
361 | 78 | public function stream_close() |
|
370 | |||
371 | /** |
||
372 | * Tests for end-of-file on a file pointer. |
||
373 | * |
||
374 | * @return bool True if the file is at the end, false if not. |
||
375 | */ |
||
376 | 60 | public function stream_eof() |
|
380 | |||
381 | /** |
||
382 | * Flushes the output. |
||
383 | * |
||
384 | * @return bool True on success, false on failure. |
||
385 | */ |
||
386 | 78 | public function stream_flush() |
|
406 | |||
407 | /** |
||
408 | * Advisory file locking. |
||
409 | * |
||
410 | * @param int $operation |
||
411 | * |
||
412 | * @return bool True on success, false on failure. |
||
413 | */ |
||
414 | 6 | public function stream_lock($operation) |
|
432 | |||
433 | /** |
||
434 | * Changes stream options. |
||
435 | * |
||
436 | * @param string $uri |
||
437 | * @param int $option |
||
438 | * @param mixed $value |
||
439 | * |
||
440 | * @return bool True on success, false on failure. |
||
441 | */ |
||
442 | 39 | public function stream_metadata($uri, $option, $value) |
|
472 | |||
473 | /** |
||
474 | * Opens file or URL. |
||
475 | * |
||
476 | * @param string $uri |
||
477 | * @param string $mode |
||
478 | * @param int $options |
||
479 | * @param string &$opened_path |
||
480 | * |
||
481 | * @return bool True on success, false on failure. |
||
482 | */ |
||
483 | 96 | public function stream_open($uri, $mode, $options, &$opened_path) |
|
500 | |||
501 | /** |
||
502 | * Reads from stream. |
||
503 | * |
||
504 | * @param int $count |
||
505 | * |
||
506 | * @return string The bytes read. |
||
507 | */ |
||
508 | 60 | public function stream_read($count) |
|
516 | |||
517 | /** |
||
518 | * Seeks to specific location in a stream. |
||
519 | * |
||
520 | * @param int $offset |
||
521 | * @param int $whence |
||
522 | * |
||
523 | * @return bool True on success, false on failure. |
||
524 | */ |
||
525 | 24 | public function stream_seek($offset, $whence = SEEK_SET) |
|
529 | |||
530 | /** |
||
531 | * Changes stream options. |
||
532 | * |
||
533 | * @param int $option |
||
534 | * @param int $arg1 |
||
535 | * @param int $arg2 |
||
536 | * |
||
537 | * @return bool True on success, false on failure. |
||
538 | */ |
||
539 | 6 | public function stream_set_option($option, $arg1, $arg2) |
|
565 | |||
566 | /** |
||
567 | * Retrieves information about a file resource. |
||
568 | * |
||
569 | * @return array A similar array to fstat(). |
||
570 | * |
||
571 | * @see fstat() |
||
572 | */ |
||
573 | 66 | public function stream_stat() |
|
590 | |||
591 | /** |
||
592 | * Retrieves the current position of a stream. |
||
593 | * |
||
594 | * @return int The current position of the stream. |
||
595 | */ |
||
596 | 24 | public function stream_tell() |
|
603 | |||
604 | /** |
||
605 | * Truncates the stream. |
||
606 | * |
||
607 | * @param int $new_size |
||
608 | * |
||
609 | * @return bool True on success, false on failure. |
||
610 | */ |
||
611 | 12 | public function stream_truncate($new_size) |
|
621 | |||
622 | /** |
||
623 | * Writes to the stream. |
||
624 | * |
||
625 | * @param string $data |
||
626 | * |
||
627 | * @return int The number of bytes that were successfully stored. |
||
628 | */ |
||
629 | 60 | public function stream_write($data) |
|
651 | |||
652 | /** |
||
653 | * Deletes a file. |
||
654 | * |
||
655 | * @param string $uri |
||
656 | * |
||
657 | * @return bool True on success, false on failure. |
||
658 | */ |
||
659 | 12 | public function unlink($uri) |
|
665 | |||
666 | /** |
||
667 | * Retrieves information about a file. |
||
668 | * |
||
669 | * @param string $uri |
||
670 | * @param int $flags |
||
671 | * |
||
672 | * @return array Output similar to stat(). |
||
673 | * |
||
674 | * @see stat() |
||
675 | */ |
||
676 | 84 | public function url_stat($uri, $flags) |
|
695 | |||
696 | /** |
||
697 | * Returns a stream for a given path and mode. |
||
698 | * |
||
699 | * @param string $path The path to open. |
||
700 | * @param string $mode The mode to open the stream in. |
||
701 | * |
||
702 | * @return resource|bool The file handle, or false. |
||
703 | */ |
||
704 | 96 | protected function getStream($path, $mode) |
|
727 | |||
728 | /** |
||
729 | * Returns a writable stream for a given path and mode. |
||
730 | * |
||
731 | * @param string $path The path to open. |
||
732 | * |
||
733 | * @return resource|bool The file handle, or false. |
||
734 | */ |
||
735 | 6 | protected function getWritableStream($path) |
|
748 | |||
749 | /** |
||
750 | * Returns an appendable stream for a given path and mode. |
||
751 | * |
||
752 | * @param string $path The path to open. |
||
753 | * |
||
754 | * @return resource|bool The file handle, or false. |
||
755 | */ |
||
756 | 6 | protected function getAppendStream($path) |
|
764 | |||
765 | /** |
||
766 | * Returns a writable stream for a given path and mode. |
||
767 | * |
||
768 | * Triggers a warning if the file exists. |
||
769 | * |
||
770 | * @param string $path The path to open. |
||
771 | * |
||
772 | * @return resource|bool The file handle, or false. |
||
773 | */ |
||
774 | 12 | protected function getXStream($path) |
|
786 | |||
787 | /** |
||
788 | * Guarantees that the handle is writable. |
||
789 | */ |
||
790 | 60 | protected function ensureWritableHandle() |
|
804 | |||
805 | /** |
||
806 | * Returns the protocol from the internal URI. |
||
807 | * |
||
808 | * @return string The protocol. |
||
809 | */ |
||
810 | 189 | protected function getProtocol() |
|
814 | |||
815 | /** |
||
816 | * Returns the local writable target of the resource within the stream. |
||
817 | * |
||
818 | * @param string|null $uri The URI. |
||
819 | * |
||
820 | * @return string The path appropriate for use with Flysystem. |
||
821 | */ |
||
822 | 195 | protected function getTarget($uri = null) |
|
832 | |||
833 | /** |
||
834 | * Returns the configuration. |
||
835 | * |
||
836 | * @param string|null $key The optional configuration key. |
||
837 | * |
||
838 | * @return array The requested configuration. |
||
839 | */ |
||
840 | 21 | protected function getConfiguration($key = null) |
|
844 | |||
845 | /** |
||
846 | * Returns the filesystem. |
||
847 | * |
||
848 | * @return \League\Flysystem\FilesystemInterface The filesystem object. |
||
849 | */ |
||
850 | 189 | protected function getFilesystem() |
|
860 | |||
861 | /** |
||
862 | * Calls a method on an object, catching any exceptions. |
||
863 | * |
||
864 | * @param object $objet The object to call the method on. |
||
865 | * @param string $method The method name. |
||
866 | * @param array $args The arguments to the method. |
||
867 | * @param string|null $errorname The name of the calling function. |
||
868 | * |
||
869 | * @return mixed|false The return value of the call, or false on failure. |
||
870 | */ |
||
871 | 177 | protected function invoke($objet, $method, array $args, $errorname = null) |
|
883 | |||
884 | /** |
||
885 | * Calls trigger_error(), printing the appropriate message. |
||
886 | * |
||
887 | * @param string $function |
||
888 | * @param \Exception $e |
||
889 | */ |
||
890 | 96 | protected function triggerError($function, \Exception $e) |
|
910 | |||
911 | /** |
||
912 | * Creates an advisory lock handle. |
||
913 | * |
||
914 | * @return resource|false |
||
915 | */ |
||
916 | 6 | protected function openLockHandle() |
|
940 | |||
941 | /** |
||
942 | * Releases the advisory lock. |
||
943 | * |
||
944 | * @param int $operation |
||
945 | * |
||
946 | * @return bool |
||
947 | * |
||
948 | * @see FlysystemStreamWrapper::stream_lock() |
||
949 | */ |
||
950 | 6 | protected function releaseLock($operation) |
|
961 | } |
||
962 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.