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 |
||
19 | class FlysystemStreamWrapper |
||
20 | { |
||
21 | /** |
||
22 | * A flag to tell FlysystemStreamWrapper::url_stat() to ignore the size. |
||
23 | * |
||
24 | * @var int |
||
25 | */ |
||
26 | const STREAM_URL_IGNORE_SIZE = 8; |
||
27 | |||
28 | /** |
||
29 | * The registered filesystems. |
||
30 | * |
||
31 | * @var \League\Flysystem\FilesystemInterface[] |
||
32 | */ |
||
33 | protected static $filesystems = []; |
||
34 | |||
35 | /** |
||
36 | * Optional configuration. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected static $config = []; |
||
41 | |||
42 | /** |
||
43 | * The default configuration. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected static $defaultConfiguration = [ |
||
48 | 'permissions' => [ |
||
49 | 'dir' => [ |
||
50 | 'private' => 0700, |
||
51 | 'public' => 0755, |
||
52 | ], |
||
53 | 'file' => [ |
||
54 | 'private' => 0600, |
||
55 | 'public' => 0644, |
||
56 | ], |
||
57 | ], |
||
58 | 'metadata' => ['timestamp', 'size', 'visibility'], |
||
59 | 'public_mask' => 0044, |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * The number of bytes that have been written since the last flush. |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $bytesWritten = 0; |
||
68 | |||
69 | /** |
||
70 | * The filesystem of the current stream wrapper. |
||
71 | * |
||
72 | * @var \League\Flysystem\FilesystemInterface |
||
73 | */ |
||
74 | protected $filesystem; |
||
75 | |||
76 | /** |
||
77 | * A generic resource handle. |
||
78 | * |
||
79 | * @var resource|bool |
||
80 | */ |
||
81 | protected $handle; |
||
82 | |||
83 | /** |
||
84 | * Whether the handle is in append mode. |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | protected $isAppendMode = false; |
||
89 | |||
90 | /** |
||
91 | * Whether the handle is read-only. |
||
92 | * |
||
93 | * The stream returned from Flysystem may not actually be read-only, This |
||
94 | * ensures read-only behavior. |
||
95 | * |
||
96 | * @var bool |
||
97 | */ |
||
98 | protected $isReadOnly = false; |
||
99 | |||
100 | /** |
||
101 | * Whether the handle is write-only. |
||
102 | * |
||
103 | * @var bool |
||
104 | */ |
||
105 | protected $isWriteOnly = false; |
||
106 | |||
107 | /** |
||
108 | * A directory listing. |
||
109 | * |
||
110 | * @var array |
||
111 | */ |
||
112 | protected $listing; |
||
113 | |||
114 | /** |
||
115 | * Whether this handle has been verified writable. |
||
116 | * |
||
117 | * @var bool |
||
118 | */ |
||
119 | protected $needsCowCheck = false; |
||
120 | |||
121 | /** |
||
122 | * Whether the handle should be flushed. |
||
123 | * |
||
124 | * @var bool |
||
125 | */ |
||
126 | protected $needsFlush = false; |
||
127 | |||
128 | /** |
||
129 | * The handle used for calls to stream_lock. |
||
130 | * |
||
131 | * @var resource |
||
132 | */ |
||
133 | protected $lockHandle; |
||
134 | |||
135 | /** |
||
136 | * If stream_set_write_buffer() is called, the arguments. |
||
137 | * |
||
138 | * @var int |
||
139 | */ |
||
140 | protected $streamWriteBuffer; |
||
141 | |||
142 | /** |
||
143 | * Instance URI (stream). |
||
144 | * |
||
145 | * A stream is referenced as "protocol://target". |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | protected $uri; |
||
150 | |||
151 | /** |
||
152 | * Registers the stream wrapper protocol if not already registered. |
||
153 | * |
||
154 | * @param string $protocol The protocol. |
||
155 | * @param FilesystemInterface $filesystem The filesystem. |
||
156 | * @param array|null $configuration Optional configuration. |
||
157 | * @param int $flags Should be set to STREAM_IS_URL if protocol is a URL protocol. Default is 0, local stream. |
||
158 | * |
||
159 | * @return bool True if the protocol was registered, false if not. |
||
160 | */ |
||
161 | 210 | public static function register($protocol, FilesystemInterface $filesystem, array $configuration = null, $flags = 0) |
|
162 | { |
||
163 | 210 | if (static::streamWrapperExists($protocol)) { |
|
164 | 3 | return false; |
|
165 | } |
||
166 | |||
167 | 210 | static::$config[$protocol] = $configuration ?: static::$defaultConfiguration; |
|
168 | 210 | static::registerPlugins($protocol, $filesystem); |
|
169 | 210 | static::$filesystems[$protocol] = $filesystem; |
|
170 | |||
171 | 210 | return stream_wrapper_register($protocol, __CLASS__, $flags); |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Unregisters a stream wrapper. |
||
176 | * |
||
177 | * @param string $protocol The protocol. |
||
178 | * |
||
179 | * @return bool True if the protocol was unregistered, false if not. |
||
180 | */ |
||
181 | 210 | public static function unregister($protocol) |
|
182 | { |
||
183 | 210 | if ( ! static::streamWrapperExists($protocol)) { |
|
184 | 3 | return false; |
|
185 | } |
||
186 | |||
187 | 210 | unset(static::$filesystems[$protocol]); |
|
188 | |||
189 | 210 | return stream_wrapper_unregister($protocol); |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Unregisters all controlled stream wrappers. |
||
194 | */ |
||
195 | 210 | public static function unregisterAll() |
|
196 | { |
||
197 | 210 | foreach (static::getRegisteredProtocols() as $protocol) { |
|
198 | 207 | static::unregister($protocol); |
|
199 | 70 | } |
|
200 | 210 | } |
|
201 | |||
202 | /** |
||
203 | * @return array The list of registered protocols. |
||
204 | */ |
||
205 | 210 | public static function getRegisteredProtocols() |
|
206 | { |
||
207 | 210 | return array_keys(static::$filesystems); |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Determines if a protocol is registered. |
||
212 | * |
||
213 | * @param string $protocol The protocol to check. |
||
214 | * |
||
215 | * @return bool True if it is registered, false if not. |
||
216 | */ |
||
217 | 210 | protected static function streamWrapperExists($protocol) |
|
218 | { |
||
219 | 210 | return in_array($protocol, stream_get_wrappers(), true); |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * Registers plugins on the filesystem. |
||
224 | * @param string $protocol |
||
225 | * @param FilesystemInterface $filesystem |
||
226 | */ |
||
227 | 210 | protected static function registerPlugins($protocol, FilesystemInterface $filesystem) |
|
228 | { |
||
229 | 210 | $filesystem->addPlugin(new ForcedRename()); |
|
230 | 210 | $filesystem->addPlugin(new Mkdir()); |
|
231 | 210 | $filesystem->addPlugin(new Rmdir()); |
|
232 | |||
233 | 210 | $stat = new Stat( |
|
234 | 210 | static::$config[$protocol]['permissions'], |
|
235 | 210 | static::$config[$protocol]['metadata'] |
|
236 | 70 | ); |
|
237 | |||
238 | 210 | $filesystem->addPlugin($stat); |
|
239 | 210 | $filesystem->addPlugin(new Touch()); |
|
240 | 210 | } |
|
241 | |||
242 | /** |
||
243 | * Closes the directory handle. |
||
244 | * |
||
245 | * @return bool True on success, false on failure. |
||
246 | */ |
||
247 | 12 | public function dir_closedir() |
|
248 | { |
||
249 | 12 | unset($this->listing); |
|
250 | |||
251 | 12 | return true; |
|
252 | } |
||
253 | |||
254 | /** |
||
255 | * Opens a directory handle. |
||
256 | * |
||
257 | * @param string $uri The URL that was passed to opendir(). |
||
258 | * @param int $options Whether or not to enforce safe_mode (0x04). |
||
259 | * |
||
260 | * @return bool True on success, false on failure. |
||
261 | */ |
||
262 | 18 | public function dir_opendir($uri, $options) |
|
|
|||
263 | { |
||
264 | 18 | $this->uri = $uri; |
|
265 | |||
266 | 18 | $path = Util::normalizePath($this->getTarget()); |
|
267 | |||
268 | 18 | $this->listing = $this->invoke($this->getFilesystem(), 'listContents', [$path], 'opendir'); |
|
269 | |||
270 | 18 | if ($this->listing === false) { |
|
271 | 6 | return false; |
|
272 | } |
||
273 | |||
274 | 12 | if ( ! $dirlen = strlen($path)) { |
|
275 | 6 | return true; |
|
276 | } |
||
277 | |||
278 | // Remove the separator /. |
||
279 | 6 | $dirlen++; |
|
280 | |||
281 | // Remove directory prefix. |
||
282 | 6 | foreach ($this->listing as $delta => $item) { |
|
283 | 6 | $this->listing[$delta]['path'] = substr($item['path'], $dirlen); |
|
284 | 2 | } |
|
285 | |||
286 | 6 | reset($this->listing); |
|
287 | |||
288 | 6 | return true; |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * Reads an entry from directory handle. |
||
293 | * |
||
294 | * @return string|bool The next filename, or false if there is no next file. |
||
295 | */ |
||
296 | 12 | public function dir_readdir() |
|
297 | { |
||
298 | 12 | $current = current($this->listing); |
|
299 | 12 | next($this->listing); |
|
300 | |||
301 | 12 | return $current ? $current['path'] : false; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * Rewinds the directory handle. |
||
306 | * |
||
307 | * @return bool True on success, false on failure. |
||
308 | */ |
||
309 | 12 | public function dir_rewinddir() |
|
310 | { |
||
311 | 12 | reset($this->listing); |
|
312 | |||
313 | 12 | return true; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * Creates a directory. |
||
318 | * |
||
319 | * @param string $uri |
||
320 | * @param int $mode |
||
321 | * @param int $options |
||
322 | * |
||
323 | * @return bool True on success, false on failure. |
||
324 | */ |
||
325 | 60 | public function mkdir($uri, $mode, $options) |
|
326 | { |
||
327 | 60 | $this->uri = $uri; |
|
328 | |||
329 | 60 | return $this->invoke($this->getFilesystem(), 'mkdir', [$this->getTarget(), $mode, $options]); |
|
330 | } |
||
331 | |||
332 | /** |
||
333 | * Renames a file or directory. |
||
334 | * |
||
335 | * @param string $uri_from |
||
336 | * @param string $uri_to |
||
337 | * |
||
338 | * @return bool True on success, false on failure. |
||
339 | */ |
||
340 | 36 | public function rename($uri_from, $uri_to) |
|
341 | { |
||
342 | 36 | $this->uri = $uri_from; |
|
343 | 36 | $args = [$this->getTarget($uri_from), $this->getTarget($uri_to)]; |
|
344 | |||
345 | 36 | return $this->invoke($this->getFilesystem(), 'forcedRename', $args, 'rename'); |
|
346 | } |
||
347 | |||
348 | /** |
||
349 | * Removes a directory. |
||
350 | * |
||
351 | * @param string $uri |
||
352 | * @param int $options |
||
353 | * |
||
354 | * @return bool True on success, false on failure. |
||
355 | */ |
||
356 | 18 | public function rmdir($uri, $options) |
|
357 | { |
||
358 | 18 | $this->uri = $uri; |
|
359 | |||
360 | 18 | return $this->invoke($this->getFilesystem(), 'rmdir', [$this->getTarget(), $options]); |
|
361 | } |
||
362 | |||
363 | /** |
||
364 | * Retrieves the underlying resource. |
||
365 | * |
||
366 | * @param int $cast_as |
||
367 | * |
||
368 | * @return resource|bool The stream resource used by the wrapper, or false. |
||
369 | */ |
||
370 | 6 | public function stream_cast($cast_as) |
|
371 | { |
||
372 | 6 | return $this->handle; |
|
373 | } |
||
374 | |||
375 | /** |
||
376 | * Closes the resource. |
||
377 | */ |
||
378 | 84 | public function stream_close() |
|
379 | { |
||
380 | // PHP 7 doesn't call flush automatically anymore for truncate() or when |
||
381 | // writing an empty file. We need to ensure that the handle gets pushed |
||
382 | // as needed in that case. This will be a no-op for php 5. |
||
383 | 84 | $this->stream_flush(); |
|
384 | |||
385 | 84 | if (is_resource($this->handle)) { |
|
386 | 84 | fclose($this->handle); |
|
387 | 28 | } |
|
388 | 84 | } |
|
389 | |||
390 | /** |
||
391 | * Tests for end-of-file on a file pointer. |
||
392 | * |
||
393 | * @return bool True if the file is at the end, false if not. |
||
394 | */ |
||
395 | 60 | public function stream_eof() |
|
399 | |||
400 | /** |
||
401 | * Flushes the output. |
||
402 | * |
||
403 | * @return bool True on success, false on failure. |
||
404 | */ |
||
405 | 84 | public function stream_flush() |
|
406 | { |
||
407 | 84 | if ( ! $this->needsFlush) { |
|
408 | 76 | return true; |
|
409 | } |
||
410 | |||
411 | 84 | $this->needsFlush = false; |
|
412 | 84 | $this->bytesWritten = 0; |
|
413 | |||
414 | // Calling putStream() will rewind our handle. flush() shouldn't change |
||
427 | |||
428 | /** |
||
429 | * Advisory file locking. |
||
430 | * |
||
431 | * @param int $operation |
||
432 | * |
||
433 | * @return bool True on success, false on failure. |
||
434 | */ |
||
435 | 6 | public function stream_lock($operation) |
|
453 | |||
454 | /** |
||
455 | * Changes stream options. |
||
456 | * |
||
457 | * @param string $uri |
||
458 | * @param int $option |
||
459 | * @param mixed $value |
||
460 | * |
||
461 | * @return bool True on success, false on failure. |
||
462 | */ |
||
463 | 39 | public function stream_metadata($uri, $option, $value) |
|
492 | |||
493 | /** |
||
494 | * Opens file or URL. |
||
495 | * |
||
496 | * @param string $uri |
||
497 | * @param string $mode |
||
498 | * @param int $options |
||
499 | * @param string &$opened_path |
||
500 | * |
||
501 | * @return bool True on success, false on failure. |
||
502 | */ |
||
503 | 102 | public function stream_open($uri, $mode, $options, &$opened_path) |
|
520 | |||
521 | /** |
||
522 | * Reads from stream. |
||
523 | * |
||
524 | * @param int $count |
||
525 | * |
||
526 | * @return string The bytes read. |
||
527 | */ |
||
528 | 60 | public function stream_read($count) |
|
536 | |||
537 | /** |
||
538 | * Seeks to specific location in a stream. |
||
539 | * |
||
540 | * @param int $offset |
||
541 | * @param int $whence |
||
542 | * |
||
543 | * @return bool True on success, false on failure. |
||
544 | */ |
||
545 | 24 | public function stream_seek($offset, $whence = SEEK_SET) |
|
549 | |||
550 | /** |
||
551 | * Changes stream options. |
||
552 | * |
||
553 | * @param int $option |
||
554 | * @param int $arg1 |
||
555 | * @param int $arg2 |
||
556 | * |
||
557 | * @return bool True on success, false on failure. |
||
558 | */ |
||
559 | 6 | public function stream_set_option($option, $arg1, $arg2) |
|
585 | |||
586 | /** |
||
587 | * Retrieves information about a file resource. |
||
588 | * |
||
589 | * @return array A similar array to fstat(). |
||
590 | * |
||
591 | * @see fstat() |
||
592 | */ |
||
593 | 66 | public function stream_stat() |
|
610 | |||
611 | /** |
||
612 | * Retrieves the current position of a stream. |
||
613 | * |
||
614 | * @return int The current position of the stream. |
||
615 | */ |
||
616 | 24 | public function stream_tell() |
|
624 | |||
625 | /** |
||
626 | * Truncates the stream. |
||
627 | * |
||
628 | * @param int $new_size |
||
629 | * |
||
630 | * @return bool True on success, false on failure. |
||
631 | */ |
||
632 | 12 | public function stream_truncate($new_size) |
|
642 | |||
643 | /** |
||
644 | * Writes to the stream. |
||
645 | * |
||
646 | * @param string $data |
||
647 | * |
||
648 | * @return int The number of bytes that were successfully stored. |
||
649 | */ |
||
650 | 66 | public function stream_write($data) |
|
672 | |||
673 | /** |
||
674 | * Deletes a file. |
||
675 | * |
||
676 | * @param string $uri |
||
677 | * |
||
678 | * @return bool True on success, false on failure. |
||
679 | */ |
||
680 | 12 | public function unlink($uri) |
|
686 | |||
687 | /** |
||
688 | * Retrieves information about a file. |
||
689 | * |
||
690 | * @param string $uri |
||
691 | * @param int $flags |
||
692 | * |
||
693 | * @return array|false Output similar to stat(). |
||
694 | * |
||
695 | * @see stat() |
||
696 | */ |
||
697 | 90 | public function url_stat($uri, $flags) |
|
714 | |||
715 | /** |
||
716 | * Returns a stream for a given path and mode. |
||
717 | * |
||
718 | * @param string $path The path to open. |
||
719 | * @param string $mode The mode to open the stream in. |
||
720 | * |
||
721 | * @return resource|bool The file handle, or false. |
||
722 | * |
||
723 | * @throws \League\Flysystem\FileNotFoundException |
||
724 | */ |
||
725 | 102 | protected function getStream($path, $mode) |
|
750 | |||
751 | /** |
||
752 | * Returns a writable stream for a given path and mode. |
||
753 | * |
||
754 | * @param string $path The path to open. |
||
755 | * |
||
756 | * @return resource|bool The file handle, or false. |
||
757 | */ |
||
758 | 6 | protected function getWritableStream($path) |
|
770 | |||
771 | /** |
||
772 | * Returns an appendable stream for a given path and mode. |
||
773 | * |
||
774 | * @param string $path The path to open. |
||
775 | * |
||
776 | * @return resource|bool The file handle, or false. |
||
777 | */ |
||
778 | 6 | protected function getAppendStream($path) |
|
786 | |||
787 | /** |
||
788 | * Returns a writable stream for a given path and mode. |
||
789 | * |
||
790 | * Triggers a warning if the file exists. |
||
791 | * |
||
792 | * @param string $path The path to open. |
||
793 | * |
||
794 | * @return resource|bool The file handle, or false. |
||
795 | */ |
||
796 | 12 | protected function getXStream($path) |
|
808 | |||
809 | /** |
||
810 | * Guarantees that the handle is writable. |
||
811 | */ |
||
812 | 66 | protected function ensureWritableHandle() |
|
826 | |||
827 | /** |
||
828 | * Returns the protocol from the internal URI. |
||
829 | * |
||
830 | * @return string The protocol. |
||
831 | */ |
||
832 | 195 | protected function getProtocol() |
|
836 | |||
837 | /** |
||
838 | * Returns the local writable target of the resource within the stream. |
||
839 | * |
||
840 | * @param string|null $uri The URI. |
||
841 | * |
||
842 | * @return string The path appropriate for use with Flysystem. |
||
843 | */ |
||
844 | 201 | protected function getTarget($uri = null) |
|
854 | |||
855 | /** |
||
856 | * Returns the configuration. |
||
857 | * |
||
858 | * @param string|null $key The optional configuration key. |
||
859 | * |
||
860 | * @return array The requested configuration. |
||
861 | */ |
||
862 | 21 | protected function getConfiguration($key = null) |
|
866 | |||
867 | /** |
||
868 | * Returns the filesystem. |
||
869 | * |
||
870 | * @return \League\Flysystem\FilesystemInterface The filesystem object. |
||
871 | */ |
||
872 | 195 | protected function getFilesystem() |
|
882 | |||
883 | /** |
||
884 | * Calls a method on an object, catching any exceptions. |
||
885 | * |
||
886 | * @param object $object The object to call the method on. |
||
887 | * @param string $method The method name. |
||
888 | * @param array $args The arguments to the method. |
||
889 | * @param string|null $errorname The name of the calling function. |
||
890 | * |
||
891 | * @return mixed|false The return value of the call, or false on failure. |
||
892 | */ |
||
893 | 183 | protected function invoke($object, $method, array $args, $errorname = null) |
|
904 | |||
905 | /** |
||
906 | * Calls trigger_error(), printing the appropriate message. |
||
907 | * |
||
908 | * @param string $function |
||
909 | * @param \Exception $e |
||
910 | */ |
||
911 | 96 | protected function triggerError($function, \Exception $e) |
|
934 | |||
935 | /** |
||
936 | * Creates an advisory lock handle. |
||
937 | * |
||
938 | * @return resource|false |
||
939 | */ |
||
940 | 6 | protected function openLockHandle() |
|
966 | |||
967 | /** |
||
968 | * Releases the advisory lock. |
||
969 | * |
||
970 | * @param int $operation |
||
971 | * |
||
972 | * @return bool |
||
973 | * |
||
974 | * @see FlysystemStreamWrapper::stream_lock() |
||
975 | */ |
||
976 | 6 | protected function releaseLock($operation) |
|
987 | } |
||
988 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.