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 RackspaceServer 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 RackspaceServer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class RackspaceServer extends StorageServer implements LoggerAwareInterface |
||
30 | { |
||
31 | /** |
||
32 | * There is few warning messages. |
||
33 | */ |
||
34 | use LoggerTrait; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $authToken = []; |
||
40 | |||
41 | /** |
||
42 | * Some operations can be performed only inside one region. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $regions = []; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $options = [ |
||
52 | 'server' => 'https://auth.api.rackspacecloud.com/v1.0', |
||
53 | 'authServer' => 'https://identity.api.rackspacecloud.com/v2.0/tokens', |
||
54 | 'username' => '', |
||
55 | 'apiKey' => '', |
||
56 | 'cache' => true, |
||
57 | 'lifetime' => 86400 |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Cache store to remember connection. |
||
62 | * |
||
63 | * @invisible |
||
64 | * @var StoreInterface |
||
65 | */ |
||
66 | protected $store = null; |
||
67 | |||
68 | /** |
||
69 | * @todo DI in constructor |
||
70 | * @var ClientInterface |
||
71 | */ |
||
72 | protected $client = null; |
||
73 | |||
74 | /** |
||
75 | * @param FilesInterface $files |
||
76 | * @param StoreInterface $store |
||
77 | * @param array $options |
||
78 | */ |
||
79 | public function __construct(FilesInterface $files, StoreInterface $store, array $options) |
||
98 | |||
99 | /** |
||
100 | * @param ClientInterface $client |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setClient(ClientInterface $client) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | * |
||
113 | * @return bool|ResponseInterface |
||
114 | */ |
||
115 | public function exists(BucketInterface $bucket, $name) |
||
140 | |||
141 | /** |
||
142 | * {@inheritdoc} |
||
143 | */ |
||
144 | View Code Duplication | public function size(BucketInterface $bucket, $name) |
|
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function put(BucketInterface $bucket, $name, $source) |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | View Code Duplication | public function allocateStream(BucketInterface $bucket, $name) |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | View Code Duplication | public function delete(BucketInterface $bucket, $name) |
|
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | public function rename(BucketInterface $bucket, $oldname, $newname) |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function copy(BucketInterface $bucket, BucketInterface $destination, $name) |
||
282 | |||
283 | /** |
||
284 | * Connect to rackspace servers using new or cached token. |
||
285 | * |
||
286 | * @throws ServerException |
||
287 | */ |
||
288 | protected function connect() |
||
354 | |||
355 | /** |
||
356 | * Reconnect. |
||
357 | * |
||
358 | * @throws ServerException |
||
359 | */ |
||
360 | protected function reconnect() |
||
365 | |||
366 | /** |
||
367 | * Create instance of UriInterface based on provided bucket options and storage object name. |
||
368 | * |
||
369 | * @param BucketInterface $bucket |
||
370 | * @param string $name |
||
371 | * @return UriInterface |
||
372 | * @throws ServerException |
||
373 | */ |
||
374 | protected function buildUri(BucketInterface $bucket, $name) |
||
389 | |||
390 | /** |
||
391 | * Create pre-configured object request. |
||
392 | * |
||
393 | * @param string $method |
||
394 | * @param BucketInterface $bucket |
||
395 | * @param string $name |
||
396 | * @param array $headers |
||
397 | * @return RequestInterface |
||
398 | */ |
||
399 | protected function buildRequest($method, BucketInterface $bucket, $name, array $headers = []) |
||
409 | } |
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.