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 GoogleCloudClientStorage 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 GoogleCloudClientStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class GoogleCloudClientStorage implements Adapter, MetadataSupporter, ResourcesSupporter, ListKeysAware |
||
| 17 | { |
||
| 18 | protected $storageClient; |
||
| 19 | protected $bucket; |
||
| 20 | protected $bucketValidated; |
||
| 21 | protected $options = array(); |
||
| 22 | protected $metadata = array(); |
||
| 23 | protected $resources = array(); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param Google\Cloud\Storage\StorageClient $service Authenticated storage client class |
||
|
|
|||
| 27 | * @param string $bucketName Name of the bucket |
||
| 28 | * @param array $options Options are: "directory" and "acl" (see https://cloud.google.com/storage/docs/access-control/lists) |
||
| 29 | */ |
||
| 30 | public function __construct(\Google\Cloud\Storage\StorageClient $storageClient, $bucketName, $options = array()) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get adapter options |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | public function getOptions() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Set adapter options |
||
| 55 | * |
||
| 56 | * @param array $options |
||
| 57 | */ |
||
| 58 | public function setOptions($options) |
||
| 62 | |||
| 63 | protected function computePath($key) |
||
| 75 | |||
| 76 | protected function isBucket() |
||
| 87 | |||
| 88 | public function setBucket($name) |
||
| 94 | |||
| 95 | public function getBucket() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function read($key) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function write($key, $content) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function exists($key) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function isDirectory($key) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function listKeys($prefix = null) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | public function keys() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function mtime($key) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function delete($key) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function rename($sourceKey, $targetKey) |
||
| 225 | { |
||
| 226 | $this->isBucket(); |
||
| 227 | |||
| 228 | $metadata = $this->getMetadata($sourceKey); |
||
| 229 | |||
| 230 | $sourceKey = $this->computePath($sourceKey); |
||
| 231 | $targetKey = $this->computePath($targetKey); |
||
| 232 | |||
| 233 | $object = $this->bucket->object($sourceKey); |
||
| 234 | |||
| 235 | $copiedObject = $object->copy($this->bucket, |
||
| 236 | array( |
||
| 237 | 'name' => $targetKey |
||
| 238 | ) |
||
| 239 | ); |
||
| 240 | |||
| 241 | if ($copiedObject->exists()) |
||
| 242 | { |
||
| 243 | $this->updateKeyProperties($targetKey, |
||
| 244 | array( |
||
| 245 | 'acl' => $this->options['acl'], |
||
| 246 | 'metadata' => $this->getMetadata($sourceKey) |
||
| 247 | ) |
||
| 248 | ); |
||
| 249 | $this->setMetadata($targetKey, $this->getMetadata($sourceKey)); |
||
| 250 | $this->setMetadata($sourceKey, null); |
||
| 251 | $object->delete(); |
||
| 252 | return true; |
||
| 253 | } |
||
| 254 | return false; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function setMetadata($key, $metadata) |
||
| 261 | { |
||
| 262 | $this->metadata[$key] = $metadata; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function getMetadata($key) |
||
| 269 | { |
||
| 270 | if (!isset($this->metadata[$key]) && $this->exists($key)) |
||
| 271 | { |
||
| 272 | $data = $this->bucket->object($key)->info(); |
||
| 273 | if (isset($data['metadata'])) |
||
| 274 | { |
||
| 275 | $this->metadata[$key] = $data['metadata']; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | return isset($this->metadata[$key]) ? $this->metadata[$key] : array(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function setResources($key, $data) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function getResources($key) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function getResourceByName($key, $resourceName) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Sets ACL and metadata information. |
||
| 307 | * |
||
| 308 | * @param string $key |
||
| 309 | * @param array $options Can contain "acl" and/or "metadata" arrays. |
||
| 310 | * @return boolean |
||
| 311 | */ |
||
| 312 | protected function updateKeyProperties($key, $options = array()) |
||
| 340 | } |
||
| 341 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.