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 Adapter 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 Adapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Adapter extends AbstractAdapter |
||
17 | { |
||
18 | /** |
||
19 | * @var |
||
20 | */ |
||
21 | protected $bucket; |
||
22 | |||
23 | /** |
||
24 | * @var |
||
25 | */ |
||
26 | protected $debug; |
||
27 | |||
28 | /** |
||
29 | * Adapter constructor. |
||
30 | * |
||
31 | * @param $config |
||
32 | */ |
||
33 | public function __construct($config) |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | 15 | public function getBucket() |
|
55 | |||
56 | /** |
||
57 | * @param string $path |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 1 | public function getUrl($path) |
|
65 | |||
66 | /** |
||
67 | * @param string $path |
||
68 | * @param string $contents |
||
69 | * @param Config $config |
||
70 | * |
||
71 | * @throws RuntimeException |
||
72 | * |
||
73 | * @return array|bool |
||
74 | */ |
||
75 | View Code Duplication | public function write($path, $contents, Config $config) |
|
100 | |||
101 | /** |
||
102 | * @param string $path |
||
103 | * @param resource $resource |
||
104 | * @param Config $config |
||
105 | * |
||
106 | * @throws RuntimeException |
||
107 | * |
||
108 | * @return array|bool |
||
109 | */ |
||
110 | 1 | View Code Duplication | public function writeStream($path, $resource, Config $config) |
127 | |||
128 | /** |
||
129 | * @param string $path |
||
130 | * @param string $contents |
||
131 | * @param Config $config |
||
132 | * |
||
133 | * @throws RuntimeException |
||
134 | * |
||
135 | * @return array|bool |
||
136 | */ |
||
137 | View Code Duplication | public function update($path, $contents, Config $config) |
|
162 | |||
163 | /** |
||
164 | * @param string $path |
||
165 | * @param resource $resource |
||
166 | * @param Config $config |
||
167 | * |
||
168 | * @throws RuntimeException |
||
169 | * |
||
170 | * @return array|bool |
||
171 | */ |
||
172 | 1 | View Code Duplication | public function updateStream($path, $resource, Config $config) |
189 | |||
190 | /** |
||
191 | * @param string $path |
||
192 | * @param string $newpath |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 1 | public function rename($path, $newpath) |
|
202 | |||
203 | /** |
||
204 | * @param string $path |
||
205 | * @param string $newpath |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | 1 | public function copy($path, $newpath) |
|
215 | |||
216 | /** |
||
217 | * @param string $path |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | 1 | public function delete($path) |
|
227 | |||
228 | /** |
||
229 | * @param string $dirname |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | 1 | public function deleteDir($dirname) |
|
239 | |||
240 | /** |
||
241 | * @param string $dirname |
||
242 | * @param Config $config |
||
243 | * |
||
244 | * @return array|bool |
||
245 | */ |
||
246 | 1 | public function createDir($dirname, Config $config) |
|
252 | |||
253 | /** |
||
254 | * @param string $path |
||
255 | * @param string $visibility |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | 1 | public function setVisibility($path, $visibility) |
|
267 | |||
268 | /** |
||
269 | * @param string $path |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | 1 | public function has($path) |
|
281 | |||
282 | /** |
||
283 | * @param string $path |
||
284 | * |
||
285 | * @return array |
||
286 | */ |
||
287 | public function read($path) |
||
291 | |||
292 | /** |
||
293 | * @param string $path |
||
294 | * |
||
295 | * @return array |
||
296 | */ |
||
297 | public function readStream($path) |
||
301 | |||
302 | /** |
||
303 | * @param string $directory |
||
304 | * @param bool $recursive |
||
305 | * |
||
306 | * @return array|bool |
||
307 | */ |
||
308 | 1 | public function listContents($directory = '', $recursive = false) |
|
314 | |||
315 | /** |
||
316 | * @param string $path |
||
317 | * |
||
318 | * @return array|bool |
||
319 | */ |
||
320 | 6 | public function getMetadata($path) |
|
326 | |||
327 | /** |
||
328 | * @param string $path |
||
329 | * |
||
330 | * @return array|bool |
||
331 | */ |
||
332 | 1 | public function getSize($path) |
|
333 | { |
||
334 | 1 | $stat = $this->getMetadata($path); |
|
335 | |||
336 | if (isset($stat['filesize'])) { |
||
337 | return ['size' => $stat['filesize']]; |
||
338 | } |
||
339 | |||
340 | return false; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param string $path |
||
345 | * |
||
346 | * @return array|bool |
||
347 | */ |
||
348 | 1 | public function getMimetype($path) |
|
349 | { |
||
350 | 1 | $stat = $this->getMetadata($path); |
|
351 | |||
352 | if (isset($stat['custom_headers']['Content-Type'])) { |
||
353 | return ['mimetype' => $stat['custom_headers']['Content-Type']]; |
||
354 | } |
||
355 | |||
356 | return false; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param string $path |
||
361 | * |
||
362 | * @return array|bool |
||
363 | */ |
||
364 | 1 | public function getTimestamp($path) |
|
374 | |||
375 | /** |
||
376 | * @param string $path |
||
377 | * |
||
378 | * @return array|bool |
||
379 | */ |
||
380 | 1 | public function getVisibility($path) |
|
381 | { |
||
382 | 1 | $stat = $this->getMetadata($path); |
|
383 | |||
384 | View Code Duplication | if (isset($stat['authority']) && $stat['authority'] === 'eWPrivateRPublic') { |
|
385 | return ['visibility' => AdapterInterface::VISIBILITY_PUBLIC]; |
||
386 | } |
||
387 | |||
388 | View Code Duplication | if (isset($stat['authority']) && $stat['authority'] === 'eWRPrivate') { |
|
389 | return ['visibility' => AdapterInterface::VISIBILITY_PRIVATE]; |
||
390 | } |
||
391 | |||
392 | return false; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @param string $content |
||
397 | * |
||
398 | * @return string|bool |
||
399 | */ |
||
400 | private function writeTempFile($content) |
||
410 | |||
411 | /** |
||
412 | * @param string|bool $tmpfname |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | private function deleteTempFile($tmpfname) |
||
424 | |||
425 | /** |
||
426 | * @param string $path |
||
427 | * @param string $content |
||
428 | * |
||
429 | * @return bool |
||
430 | */ |
||
431 | protected function setContentType($path, $content) |
||
441 | |||
442 | /** |
||
443 | * @param $response |
||
444 | * |
||
445 | * @throws RuntimeException |
||
446 | * |
||
447 | * @return mixed |
||
448 | */ |
||
449 | 15 | protected function normalizeResponse($response) |
|
461 | } |
||
462 |
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.