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 GCS 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 GCS, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | View Code Duplication | class GCS implements Adapter, Adapter\MetadataSupporter, Adapter\ListKeysAware |
|
|
|||
13 | { |
||
14 | protected $service; |
||
15 | protected $bucket; |
||
16 | protected $options; |
||
17 | protected $bucketExists; |
||
18 | protected $metadata = array(); |
||
19 | protected $detectContentType; |
||
20 | |||
21 | /** |
||
22 | * @param \Google_Service_Storage $service The storage service class with authenticated |
||
23 | * client and full access scope |
||
24 | * @param string $bucket The bucket name |
||
25 | * @param array $options Options can be directory and acl |
||
26 | * @param bool $detectContentType Whether to detect the content type or not |
||
27 | */ |
||
28 | public function __construct( |
||
46 | |||
47 | /** |
||
48 | * @return array The actual options |
||
49 | */ |
||
50 | public function getOptions() |
||
54 | |||
55 | /** |
||
56 | * @param array $options The new options |
||
57 | */ |
||
58 | public function setOptions($options) |
||
62 | |||
63 | /** |
||
64 | * @return string The current bucket name |
||
65 | */ |
||
66 | public function getBucket() |
||
70 | |||
71 | /** |
||
72 | * Sets a new bucket name. |
||
73 | * |
||
74 | * @param string $bucket The new bucket name |
||
75 | */ |
||
76 | public function setBucket($bucket) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function read($key) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function write($key, $content) |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function exists($key) |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function keys() |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function mtime($key) |
||
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | public function delete($key) |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | */ |
||
235 | public function rename($sourceKey, $targetKey) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function isDirectory($key) |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | public function listKeys($prefix = '') |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function setMetadata($key, $content) |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function getMetadata($key) |
||
320 | |||
321 | /** |
||
322 | * Ensures the specified bucket exists. |
||
323 | * |
||
324 | * @throws \RuntimeException if the bucket does not exists |
||
325 | */ |
||
326 | protected function ensureBucketExists() |
||
348 | |||
349 | protected function computePath($key) |
||
357 | |||
358 | /** |
||
359 | * @param string $path |
||
360 | * @param array $options |
||
361 | * |
||
362 | * @return bool|\Google_Service_Storage_StorageObject |
||
363 | */ |
||
364 | private function getObjectData($path, $options = array()) |
||
372 | } |
||
373 |
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.