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 |
||
17 | class Adapter extends AbstractAdapter |
||
18 | { |
||
19 | /** |
||
20 | * @var Api |
||
21 | */ |
||
22 | protected $cosApi; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $bucket; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $debug; |
||
33 | |||
34 | /** |
||
35 | * Adapter constructor. |
||
36 | * |
||
37 | * @param Api $cosApi |
||
38 | * @param array $config |
||
39 | */ |
||
40 | public function __construct(Api $cosApi, array $config) |
||
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | public function getBucket() |
||
57 | |||
58 | /** |
||
59 | * @param string $path |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getUrl($path) |
||
67 | |||
68 | /** |
||
69 | * @param string $path |
||
70 | * @param \DateTimeInterface $expiration |
||
71 | * @param array $options |
||
72 | * |
||
73 | * @return string|bool |
||
74 | */ |
||
75 | public function getTemporaryUrl($path, DateTimeInterface $expiration, array $options = []) |
||
90 | |||
91 | /** |
||
92 | * @param string $path |
||
93 | * @param string $contents |
||
94 | * @param Config $config |
||
95 | * |
||
96 | * @throws RuntimeException |
||
97 | * |
||
98 | * @return array|bool |
||
99 | */ |
||
100 | View Code Duplication | public function write($path, $contents, Config $config) |
|
115 | |||
116 | /** |
||
117 | * @param string $path |
||
118 | * @param resource $resource |
||
119 | * @param Config $config |
||
120 | * |
||
121 | * @throws RuntimeException |
||
122 | * |
||
123 | * @return array|bool |
||
124 | */ |
||
125 | View Code Duplication | public function writeStream($path, $resource, Config $config) |
|
140 | |||
141 | /** |
||
142 | * @param string $path |
||
143 | * @param string $contents |
||
144 | * @param Config $config |
||
145 | * |
||
146 | * @throws RuntimeException |
||
147 | * |
||
148 | * @return array|bool |
||
149 | */ |
||
150 | View Code Duplication | public function update($path, $contents, Config $config) |
|
165 | |||
166 | /** |
||
167 | * @param string $path |
||
168 | * @param resource $resource |
||
169 | * @param Config $config |
||
170 | * |
||
171 | * @throws RuntimeException |
||
172 | * |
||
173 | * @return array|bool |
||
174 | */ |
||
175 | View Code Duplication | public function updateStream($path, $resource, Config $config) |
|
190 | |||
191 | /** |
||
192 | * @param string $path |
||
193 | * @param string $newpath |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function rename($path, $newpath) |
||
203 | |||
204 | /** |
||
205 | * @param string $path |
||
206 | * @param string $newpath |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function copy($path, $newpath) |
||
216 | |||
217 | /** |
||
218 | * @param string $path |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function delete($path) |
||
228 | |||
229 | /** |
||
230 | * @param string $dirname |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function deleteDir($dirname) |
||
240 | |||
241 | /** |
||
242 | * @param string $dirname |
||
243 | * @param Config $config |
||
244 | * |
||
245 | * @return array|bool |
||
246 | */ |
||
247 | public function createDir($dirname, Config $config) |
||
253 | |||
254 | /** |
||
255 | * @param string $path |
||
256 | * @param string $visibility |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | public function setVisibility($path, $visibility) |
||
269 | |||
270 | /** |
||
271 | * @param string $path |
||
272 | * |
||
273 | * @return bool |
||
274 | */ |
||
275 | public function has($path) |
||
283 | |||
284 | /** |
||
285 | * @param string $path |
||
286 | * |
||
287 | * @return array|bool |
||
288 | */ |
||
289 | public function read($path) |
||
295 | |||
296 | /** |
||
297 | * @param string $path |
||
298 | * |
||
299 | * @return array|bool |
||
300 | */ |
||
301 | public function readStream($path) |
||
307 | |||
308 | /** |
||
309 | * @param string $directory |
||
310 | * @param bool $recursive |
||
311 | * |
||
312 | * @return array|bool |
||
313 | */ |
||
314 | public function listContents($directory = '', $recursive = false) |
||
320 | |||
321 | /** |
||
322 | * @param string $path |
||
323 | * |
||
324 | * @return array|bool |
||
325 | */ |
||
326 | public function getMetadata($path) |
||
332 | |||
333 | /** |
||
334 | * @param string $path |
||
335 | * |
||
336 | * @return array|bool |
||
337 | */ |
||
338 | public function getSize($path) |
||
344 | |||
345 | /** |
||
346 | * @param string $path |
||
347 | * |
||
348 | * @return array|bool |
||
349 | */ |
||
350 | public function getMimetype($path) |
||
357 | |||
358 | /** |
||
359 | * @param string $path |
||
360 | * |
||
361 | * @return array|bool |
||
362 | */ |
||
363 | public function getTimestamp($path) |
||
369 | |||
370 | /** |
||
371 | * @param string $path |
||
372 | * |
||
373 | * @return array|bool |
||
374 | */ |
||
375 | public function getVisibility($path) |
||
389 | |||
390 | /** |
||
391 | * Creates a temporary file. |
||
392 | * |
||
393 | * @param string $content |
||
394 | * |
||
395 | * @throws RuntimeException |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function createTemporaryFile($content) |
||
416 | |||
417 | /** |
||
418 | * Gets a temporary file path. |
||
419 | * |
||
420 | * @return bool|string |
||
421 | */ |
||
422 | protected function getTemporaryPath() |
||
426 | |||
427 | /** |
||
428 | * @param string $path |
||
429 | * @param string $content |
||
430 | * |
||
431 | * @return bool |
||
432 | */ |
||
433 | protected function setContentType($path, $content) |
||
443 | |||
444 | /** |
||
445 | * @param $response |
||
446 | * |
||
447 | * @throws RuntimeException |
||
448 | * |
||
449 | * @return mixed |
||
450 | */ |
||
451 | protected function normalizeResponse($response) |
||
463 | } |
||
464 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.