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; |
||
25 | |||
26 | public function __construct(Config $config = null) |
||
35 | |||
36 | 6 | /** |
|
37 | * @inheritdoc |
||
38 | 6 | */ |
|
39 | public function copy($path, $newpath) |
||
50 | 6 | ||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | public function createDir($dirname, Config $config) |
||
62 | 3 | ||
63 | /** |
||
64 | 3 | * @inheritdoc |
|
65 | */ |
||
66 | public function delete($path) |
||
76 | 3 | ||
77 | 3 | /** |
|
78 | 3 | * @inheritdoc |
|
79 | */ |
||
80 | 3 | public function deleteDir($dirname) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | public function getMetadata($path) |
||
105 | 3 | ||
106 | 3 | /** |
|
107 | * @inheritdoc |
||
108 | */ |
||
109 | public function getMimetype($path) |
||
118 | |||
119 | /** |
||
120 | 6 | * @inheritdoc |
|
121 | */ |
||
122 | 6 | public function getSize($path) |
|
126 | |||
127 | /** |
||
128 | 6 | * @inheritdoc |
|
129 | */ |
||
130 | 6 | public function getTimestamp($path) |
|
134 | |||
135 | /** |
||
136 | 54 | * @inheritdoc |
|
137 | */ |
||
138 | 54 | public function getVisibility($path) |
|
142 | |||
143 | /** |
||
144 | 3 | * @inheritdoc |
|
145 | */ |
||
146 | 3 | public function has($path) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 9 | public function listContents($directory = '', $recursive = false) |
|
160 | 2 | ||
161 | /** |
||
162 | * @inheritdoc |
||
163 | */ |
||
164 | public function read($path) |
||
171 | |||
172 | 3 | /** |
|
173 | * @inheritdoc |
||
174 | */ |
||
175 | public function readStream($path) |
||
184 | |||
185 | 3 | /** |
|
186 | * @inheritdoc |
||
187 | */ |
||
188 | public function rename($path, $newpath) |
||
197 | 3 | ||
198 | /** |
||
199 | 3 | * @inheritdoc |
|
200 | */ |
||
201 | public function setVisibility($path, $visibility) |
||
211 | 54 | ||
212 | 54 | /** |
|
213 | 54 | * @inheritdoc |
|
214 | 54 | */ |
|
215 | public function update($path, $contents, Config $config) |
||
228 | |||
229 | 54 | /** |
|
230 | 54 | * @inheritdoc |
|
231 | */ |
||
232 | 54 | public function write($path, $contents, Config $config) |
|
244 | 54 | ||
245 | 54 | /** |
|
246 | * Creates a directory. |
||
247 | * |
||
248 | 18 | * @param string $dirname |
|
249 | 12 | * @param Config $config |
|
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | 15 | protected function doCreateDir($dirname, Config $config) |
|
273 | |||
274 | 6 | /** |
|
275 | 6 | * Filters the file system returning paths inside the directory. |
|
276 | * |
||
277 | * @param string $directory |
||
278 | 6 | * @param bool $recursive |
|
279 | 6 | * |
|
280 | * @return string[] |
||
281 | */ |
||
282 | 6 | protected function doListContents($directory, $recursive) |
|
299 | |||
300 | /** |
||
301 | * Checks whether a directory exists. |
||
302 | * |
||
303 | * @param string $path The directory. |
||
304 | * |
||
305 | * @return bool True if it exists, and is a directory, false if not. |
||
306 | */ |
||
307 | 54 | protected function hasDirectory($path) |
|
311 | |||
312 | /** |
||
313 | * Checks whether a file exists. |
||
314 | * |
||
315 | * @param string $path The file. |
||
316 | * |
||
317 | * @return bool True if it exists, and is a file, false if not. |
||
318 | */ |
||
319 | protected function hasFile($path) |
||
323 | |||
324 | /** |
||
325 | * Determines if the path is inside the directory. |
||
326 | * |
||
327 | * @param string $path |
||
328 | * @param string $directory |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | protected function pathIsInDirectory($path, $directory) |
||
336 | } |
||
337 |