Complex classes like CachedAdapter 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 CachedAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class CachedAdapter implements AdapterInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var AdapterInterface |
||
12 | */ |
||
13 | private $adapter; |
||
14 | |||
15 | /** |
||
16 | * @var CacheInterface |
||
17 | */ |
||
18 | private $cache; |
||
19 | |||
20 | /** |
||
21 | * Constructor. |
||
22 | * |
||
23 | * @param AdapterInterface $adapter |
||
24 | * @param CacheInterface $cache |
||
25 | */ |
||
26 | 144 | public function __construct(AdapterInterface $adapter, CacheInterface $cache) |
|
32 | |||
33 | /** |
||
34 | * Get the underlying Adapter implementation. |
||
35 | * |
||
36 | * @return AdapterInterface |
||
37 | */ |
||
38 | 3 | public function getAdapter() |
|
42 | |||
43 | /** |
||
44 | * Get the used Cache implementation. |
||
45 | * |
||
46 | * @return CacheInterface |
||
47 | */ |
||
48 | public function getCache() |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | 6 | public function write($path, $contents, Config $config) |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 6 | public function writeStream($path, $resource, Config $config) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 6 | public function update($path, $contents, Config $config) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 6 | public function updateStream($path, $resource, Config $config) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 6 | public function rename($path, $newPath) |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 6 | public function copy($path, $newpath) |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 6 | public function delete($path) |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 6 | public function deleteDir($dirname) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 6 | public function createDir($dirname, Config $config) |
|
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | 6 | public function setVisibility($path, $visibility) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 12 | public function has($path) |
|
223 | |||
224 | /** |
||
225 | * {@inheritdoc} |
||
226 | */ |
||
227 | 9 | public function read($path) |
|
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | 3 | public function readStream($path) |
|
239 | |||
240 | /** |
||
241 | * Get the path prefix. |
||
242 | * |
||
243 | 9 | * @return string|null path prefix or null if pathPrefix is empty |
|
244 | */ |
||
245 | 9 | public function getPathPrefix() |
|
249 | 6 | ||
250 | /** |
||
251 | 6 | * Prefix a path. |
|
252 | 3 | * |
|
253 | 3 | * @param string $path |
|
254 | * |
||
255 | 6 | * @return string prefixed path |
|
256 | */ |
||
257 | public function applyPathPrefix($path) |
||
261 | 9 | ||
262 | /** |
||
263 | 9 | * {@inheritdoc} |
|
264 | */ |
||
265 | public function listContents($directory = '', $recursive = false) |
||
279 | 9 | ||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function getMetadata($path) |
||
287 | 9 | ||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function getSize($path) |
||
295 | 9 | ||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function getMimetype($path) |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | 54 | */ |
|
307 | public function getTimestamp($path) |
||
311 | 18 | ||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | 36 | */ |
|
315 | public function getVisibility($path) |
||
319 | 18 | ||
320 | /** |
||
321 | 36 | * Call a method and cache the response. |
|
322 | * |
||
323 | * @param string $property |
||
324 | * @param string $path |
||
325 | * @param string $method |
||
326 | * |
||
327 | * @return mixed |
||
328 | */ |
||
329 | protected function callWithFallback($property, $path, $method) |
||
346 | } |
||
347 |