Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
12 | class DropboxAdapter extends AbstractAdapter |
||
13 | { |
||
14 | use NotSupportingVisibilityTrait; |
||
15 | |||
16 | /** @var \Spatie\Dropbox\Client */ |
||
17 | protected $client; |
||
18 | |||
19 | public function __construct(Client $client, string $prefix = '') |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | public function write($path, $contents, Config $config) |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | public function writeStream($path, $resource, Config $config) |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function update($path, $contents, Config $config) |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function updateStream($path, $resource, Config $config) |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | public function rename($path, $newPath): bool |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | View Code Duplication | public function copy($path, $newpath): bool |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function delete($path): bool |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function deleteDir($dirname): bool |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | View Code Duplication | public function createDir($dirname, Config $config) |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function has($path) |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function read($path) |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | View Code Duplication | public function readStream($path) |
|
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function listContents($directory = '', $recursive = false): array |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | View Code Duplication | public function getMetadata($path) |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function getSize($path) |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function getMimetype($path) |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function getTimestamp($path) |
||
242 | |||
243 | public function getTemporaryLink(string $path): string |
||
247 | |||
248 | public function getTemporaryUrl(string $path): string |
||
252 | |||
253 | public function getUrl(string $path): string |
||
257 | |||
258 | public function getThumbnail(string $path, string $format = 'jpeg', string $size = 'w64h64') |
||
262 | |||
263 | public function createSharedLinkWithSettings($path, $settings) |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function applyPathPrefix($path): string |
||
277 | |||
278 | public function getClient(): Client |
||
282 | |||
283 | /** |
||
284 | * @param string $path |
||
285 | * @param resource|string $contents |
||
286 | * @param string $mode |
||
287 | * |
||
288 | * @return array|false file metadata |
||
289 | */ |
||
290 | View Code Duplication | protected function upload(string $path, $contents, string $mode) |
|
302 | |||
303 | protected function normalizeResponse(array $response): array |
||
323 | } |
||
324 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.