Complex classes like DropboxAdapter 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 DropboxAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class DropboxAdapter extends AbstractAdapter |
||
15 | { |
||
16 | use NotSupportingVisibilityTrait; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected static $resultMap = [ |
||
22 | 'bytes' => 'size', |
||
23 | 'mime_type' => 'mimetype', |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @var Client |
||
28 | */ |
||
29 | protected $client; |
||
30 | |||
31 | /** |
||
32 | * Constructor. |
||
33 | * |
||
34 | * @param Client $client |
||
35 | * @param string $prefix |
||
36 | */ |
||
37 | 21 | public function __construct(Client $client, $prefix = null) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 9 | public function has($path) |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 3 | public function write($path, $contents, Config $config) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 3 | public function writeStream($path, $resource, Config $config) |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 3 | public function update($path, $contents, Config $config) |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 3 | public function updateStream($path, $resource, Config $config) |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 3 | public function read($path) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 6 | public function readStream($path) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 6 | public function rename($path, $newpath) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 6 | public function copy($path, $newpath) |
|
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | 3 | public function delete($path) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 3 | public function deleteDir($path) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 3 | public function createDir($path, Config $config) |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 21 | public function getMetadata($path) |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 3 | public function getMimetype($path) |
|
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | 3 | public function getSize($path) |
|
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | 3 | public function getTimestamp($path) |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 3 | public function getClient() |
|
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | 3 | public function listContents($directory = '', $recursive = false) |
|
266 | |||
267 | /** |
||
268 | * Apply the path prefix. |
||
269 | * |
||
270 | * @param string $path |
||
271 | * |
||
272 | * @return string prefixed path |
||
273 | */ |
||
274 | 60 | public function applyPathPrefix($path) |
|
281 | |||
282 | /** |
||
283 | * Do the actual upload of a string file. |
||
284 | * |
||
285 | * @param string $path |
||
286 | * @param string $contents |
||
287 | * @param WriteMode $mode |
||
288 | * |
||
289 | * @return array|false file metadata |
||
290 | */ |
||
291 | 6 | protected function upload($path, $contents, WriteMode $mode) |
|
301 | |||
302 | /** |
||
303 | * Do the actual upload of a file resource. |
||
304 | * |
||
305 | * @param string $path |
||
306 | * @param resource $resource |
||
307 | * @param WriteMode $mode |
||
308 | * |
||
309 | * @return array|false file metadata |
||
310 | */ |
||
311 | 6 | protected function uploadStream($path, $resource, WriteMode $mode) |
|
324 | |||
325 | /** |
||
326 | * Normalize a Dropbox response. |
||
327 | * |
||
328 | * @param array $response |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | 33 | protected function normalizeResponse(array $response) |
|
345 | } |
||
346 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.