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 GoogleCloudStorage 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 GoogleCloudStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GoogleCloudStorage implements Adapter, |
||
|
|
|||
| 14 | MetadataSupporter, |
||
| 15 | ListKeysAware |
||
| 16 | { |
||
| 17 | protected $service; |
||
| 18 | protected $bucket; |
||
| 19 | protected $options; |
||
| 20 | protected $bucketExists; |
||
| 21 | protected $metadata = array(); |
||
| 22 | protected $detectContentType; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param \Google_Service_Storage $service The storage service class with authenticated |
||
| 26 | * client and full access scope |
||
| 27 | * @param string $bucket The bucket name |
||
| 28 | * @param array $options Options can be directory and acl |
||
| 29 | * @param bool $detectContentType Whether to detect the content type or not |
||
| 30 | */ |
||
| 31 | View Code Duplication | public function __construct( |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @return array The actual options |
||
| 52 | */ |
||
| 53 | public function getOptions() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param array $options The new options |
||
| 60 | */ |
||
| 61 | public function setOptions($options) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return string The current bucket name |
||
| 68 | */ |
||
| 69 | public function getBucket() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Sets a new bucket name. |
||
| 76 | * |
||
| 77 | * @param string $bucket The new bucket name |
||
| 78 | */ |
||
| 79 | public function setBucket($bucket) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | public function read($key) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function write($key, $content) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function exists($key) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function keys() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | public function mtime($key) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function delete($key) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function rename($sourceKey, $targetKey) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | public function isDirectory($key) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function listKeys($prefix = '') |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function setMetadata($key, $content) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | */ |
||
| 326 | public function getMetadata($key) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Ensures the specified bucket exists. |
||
| 335 | * |
||
| 336 | * @throws \RuntimeException if the bucket does not exists |
||
| 337 | */ |
||
| 338 | protected function ensureBucketExists() |
||
| 360 | |||
| 361 | View Code Duplication | protected function computePath($key) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $path |
||
| 372 | * @param array $options |
||
| 373 | * |
||
| 374 | * @return bool|\Google_Service_Storage_StorageObject |
||
| 375 | */ |
||
| 376 | private function getObjectData($path, $options = array()) |
||
| 384 | } |
||
| 385 |