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 | /** |
||
| 27 | * @inheritdoc |
||
| 28 | */ |
||
| 29 | 6 | public function copy($path, $newpath) |
|
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritdoc |
||
| 43 | */ |
||
| 44 | 6 | public function createDir($dirname, Config $config) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @inheritdoc |
||
| 55 | */ |
||
| 56 | 3 | public function delete($path) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | 3 | public function deleteDir($dirname) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @inheritdoc |
||
| 87 | */ |
||
| 88 | 54 | public function getMetadata($path) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritdoc |
||
| 98 | */ |
||
| 99 | 3 | public function getMimetype($path) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @inheritdoc |
||
| 111 | */ |
||
| 112 | 3 | public function getSize($path) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @inheritdoc |
||
| 119 | */ |
||
| 120 | 6 | public function getTimestamp($path) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | */ |
||
| 128 | 6 | public function getVisibility($path) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | 54 | public function has($path) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | 3 | public function listContents($directory = '', $recursive = false) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @inheritdoc |
||
| 153 | */ |
||
| 154 | 10 | public function read($path) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | 3 | public function readStream($path) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | */ |
||
| 178 | 3 | public function rename($path, $newpath) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @inheritdoc |
||
| 190 | */ |
||
| 191 | 3 | public function setVisibility($path, $visibility) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritdoc |
||
| 204 | */ |
||
| 205 | 54 | public function update($path, $contents, Config $config) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @inheritdoc |
||
| 221 | */ |
||
| 222 | 54 | public function write($path, $contents, Config $config) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Creates a directory. |
||
| 237 | * |
||
| 238 | * @param string $dirname |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | 54 | protected function doCreateDir($dirname) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Filters the file system returning paths inside the directory. |
||
| 264 | * |
||
| 265 | * @param string $directory |
||
| 266 | * @param bool $recursive |
||
| 267 | * |
||
| 268 | * @return string[] |
||
| 269 | */ |
||
| 270 | protected function doListContents($directory, $recursive) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Checks whether a directory exists. |
||
| 290 | * |
||
| 291 | * @param string $path The directory. |
||
| 292 | * |
||
| 293 | * @return bool True if it exists, and is a directory, false if not. |
||
| 294 | */ |
||
| 295 | 54 | protected function hasDirectory($path) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Checks whether a file exists. |
||
| 302 | * |
||
| 303 | * @param string $path The file. |
||
| 304 | * |
||
| 305 | * @return bool True if it exists, and is a file, false if not. |
||
| 306 | */ |
||
| 307 | 54 | protected function hasFile($path) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Determines if the path is inside the directory. |
||
| 314 | * |
||
| 315 | * @param string $path |
||
| 316 | * @param string $directory |
||
| 317 | * |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | 6 | protected function pathIsInDirectory($path, $directory) |
|
| 324 | } |
||
| 325 |