Complex classes like MemoryAdapter 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 MemoryAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MemoryAdapter implements AdapterInterface |
||
| 14 | { |
||
| 15 | use StreamedWritingTrait; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The emulated filesystem. |
||
| 19 | * |
||
| 20 | * Start with the root directory initialized. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $storage = ['' => ['type' => 'dir']]; |
||
| 25 | |||
| 26 | 57 | public function __construct(Config $config = null) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @inheritdoc |
||
| 35 | */ |
||
| 36 | 6 | public function copy($path, $newpath) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | 9 | public function createDir($dirname, Config $config) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritdoc |
||
| 62 | */ |
||
| 63 | 3 | public function delete($path) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @inheritdoc |
||
| 76 | */ |
||
| 77 | 3 | public function deleteDir($dirname) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @inheritdoc |
||
| 94 | */ |
||
| 95 | 57 | public function getMetadata($path) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritdoc |
||
| 105 | */ |
||
| 106 | 3 | public function getMimetype($path) |
|
| 107 | { |
||
| 108 | 3 | $mimetype = isset($this->storage[$path]['mimetype']) ? $this->storage[$path]['mimetype'] : |
|
| 109 | 3 | Util::guessMimeType($path, $this->storage[$path]['contents']); |
|
| 110 | |||
| 111 | return [ |
||
| 112 | 3 | 'mimetype' => $mimetype, |
|
| 113 | 3 | 'path' => $path, |
|
| 114 | 2 | ]; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @inheritdoc |
||
| 119 | */ |
||
| 120 | 3 | public function getSize($path) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | 9 | public function getTimestamp($path) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | 6 | public function getVisibility($path) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 57 | public function has($path) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | 3 | public function listContents($directory = '', $recursive = false) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @inheritdoc |
||
| 161 | */ |
||
| 162 | 9 | public function read($path) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @inheritdoc |
||
| 172 | */ |
||
| 173 | 3 | public function readStream($path) |
|
| 174 | { |
||
| 175 | 3 | $stream = fopen('php://memory', 'w+b'); |
|
| 176 | |||
| 177 | 3 | if ( ! is_resource($stream)) { |
|
| 178 | throw new \RuntimeException('Unable to create memory stream.'); // @codeCoverageIgnore |
||
| 179 | } |
||
| 180 | |||
| 181 | 3 | fwrite($stream, $this->storage[$path]['contents']); |
|
| 182 | 3 | rewind($stream); |
|
| 183 | |||
| 184 | 3 | return compact('path', 'stream'); |
|
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | 3 | public function rename($path, $newpath) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | 3 | public function setVisibility($path, $visibility) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @inheritdoc |
||
| 216 | */ |
||
| 217 | 57 | public function update($path, $contents, Config $config) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | 57 | public function write($path, $contents, Config $config) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Creates a directory. |
||
| 252 | * |
||
| 253 | * @param string $dirname |
||
| 254 | * @param Config $config |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | 57 | protected function doCreateDir($dirname, Config $config) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Filters the file system returning paths inside the directory. |
||
| 281 | * |
||
| 282 | * @param string $directory |
||
| 283 | * @param bool $recursive |
||
| 284 | * |
||
| 285 | * @return string[] |
||
| 286 | */ |
||
| 287 | protected function doListContents($directory, $recursive) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Checks whether a directory exists. |
||
| 307 | * |
||
| 308 | * @param string $path The directory. |
||
| 309 | * |
||
| 310 | * @return bool True if it exists, and is a directory, false if not. |
||
| 311 | */ |
||
| 312 | 57 | protected function hasDirectory($path) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Checks whether a file exists. |
||
| 319 | * |
||
| 320 | * @param string $path The file. |
||
| 321 | * |
||
| 322 | * @return bool True if it exists, and is a file, false if not. |
||
| 323 | */ |
||
| 324 | 57 | protected function hasFile($path) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Determines if the path is inside the directory. |
||
| 331 | * |
||
| 332 | * @param string $path |
||
| 333 | * @param string $directory |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 6 | protected function pathIsInDirectory($path, $directory) |
|
| 341 | } |
||
| 342 |